rev.rs - source
Description: Source of the Rust file `library/core/src/iter/adapters/rev.rs`.

#
core/iter/adapters/
rev.rs

    1use crate::iter::{FusedIterator, TrustedLen};
    2use crate::num::NonZero;
    3use crate::ops::Try;
    4
    5/// A double-ended iterator with the direction inverted.
    6///
    7/// This `struct` is created by the [`rev`] method on [`Iterator`]. See its
    8/// documentation for more.
    9///
    10/// [`rev`]: Iterator::rev
    11/// [`Iterator`]: trait.Iterator.html
    12#[derive(Clone, Debug)]
    13#[must_use = "iterators are lazy and do nothing unless consumed"]
    14#[stable(feature = "rust1", since = "1.0.0")]
    15pub struct Rev<T> {
    16    iter: T,
    17}
    18
    19impl<T> Rev<T> {
    20    pub(in crate::iter) fn new(iter: T) -> Rev<T> {
    21        Rev { iter }
    22    }
    23
    24    /// Consumes the `Rev`, returning the inner iterator.
    25    ///
    26    /// # Examples
    27    ///
    28    /// ```rust
    29    /// #![feature(rev_into_inner)]
    30    ///
    31    /// let s = "foobar";
    32    /// let mut rev = s.chars().rev();
    33    /// assert_eq!(rev.next(), Some('r'));
    34    /// assert_eq!(rev.next(), Some('a'));
    35    /// assert_eq!(rev.next(), Some('b'));
    36    /// assert_eq!(rev.into_inner().collect::<String>(), "foo");
    37    /// ```
    38    #[unstable(feature = "rev_into_inner", issue = "144277")]
    39    pub fn into_inner(self) -> T {
    40        self.iter
    41    }
    42}
    43
    44#[stable(feature = "rust1", since = "1.0.0")]
    45impl<I> Iterator for Rev<I>
    46where
    47    I: DoubleEndedIterator,
    48{
    49    type Item = <I as Iterator>::Item;
    50
    51    #[inline]
    52    fn next(&mut self) -> Option<<I as Iterator>::Item> {
    53        self.iter.next_back()
    54    }
    55    #[inline]
    56    fn size_hint(&self) -> (usize, Option<usize>) {
    57        self.iter.size_hint()
    58    }
    59
    60    #[inline]
    61    fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
    62        self.iter.advance_back_by(n)
    63    }
    64
    65    #[inline]
    66    fn nth(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
    67        self.iter.nth_back(n)
    68    }
    69
    70    fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
    71    where
    72        Self: Sized,
    73        F: FnMut(B, Self::Item) -> R,
    74        R: Try<Output = B>,
    75    {
    76        self.iter.try_rfold(init, f)
    77    }
    78
    79    fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
    80    where
    81        F: FnMut(Acc, Self::Item) -> Acc,
    82    {
    83        self.iter.rfold(init, f)
    84    }
    85
    86    #[inline]
    87    fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
    88    where
    89        P: FnMut(&Self::Item) -> bool,
    90    {
    91        self.iter.rfind(predicate)
    92    }
    93}
    94
    95#[stable(feature = "rust1", since = "1.0.0")]
    96impl<I> DoubleEndedIterator for Rev<I>
    97where
    98    I: DoubleEndedIterator,
    99{
    100    #[inline]
    101    fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
    102        self.iter.next()
    103    }
    104
    105    #[inline]
    106    fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
    107        self.iter.advance_by(n)
    108    }
    109
    110    #[inline]
    111    fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
    112        self.iter.nth(n)
    113    }
    114
    115    fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
    116    where
    117        Self: Sized,
    118        F: FnMut(B, Self::Item) -> R,
    119        R: Try<Output = B>,
    120    {
    121        self.iter.try_fold(init, f)
    122    }
    123
    124    fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
    125    where
    126        F: FnMut(Acc, Self::Item) -> Acc,
    127    {
    128        self.iter.fold(init, f)
    129    }
    130
    131    fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
    132    where
    133        P: FnMut(&Self::Item) -> bool,
    134    {
    135        self.iter.find(predicate)
    136    }
    137}
    138
    139#[stable(feature = "rust1", since = "1.0.0")]
    140impl<I> ExactSizeIterator for Rev<I>
    141where
    142    I: ExactSizeIterator + DoubleEndedIterator,
    143{
    144    fn len(&self) -> usize {
    145        self.iter.len()
    146    }
    147
    148    fn is_empty(&self) -> bool {
    149        self.iter.is_empty()
    150    }
    151}
    152
    153#[stable(feature = "fused", since = "1.26.0")]
    154impl<I> FusedIterator for Rev<I> where I: FusedIterator + DoubleEndedIterator {}
    155
    156#[unstable(feature = "trusted_len", issue = "37572")]
    157unsafe impl<I> TrustedLen for Rev<I> where I: TrustedLen + DoubleEndedIterator {}
    158
    159#[stable(feature = "default_iters", since = "1.70.0")]
    160impl<I: Default> Default for Rev<I> {
    161    /// Creates a `Rev` iterator from the default value of `I`
    162    /// ```
    163    /// # use core::slice;
    164    /// # use core::iter::Rev;
    165    /// let iter: Rev<slice::Iter<'_, u8>> = Default::default();
    166    /// assert_eq!(iter.len(), 0);
    167    /// ```
    168    fn default() -> Self {
    169        Rev::new(Default::default())
    170    }
    171}