// Copyright 2013-2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use fmt; use marker; use usize; use super::{FusedIterator, TrustedLen}; /// An iterator that repeats an element endlessly. /// /// This `struct` is created by the [`repeat`] function. See its documentation for more. /// /// [`repeat`]: fn.repeat.html #[derive(Clone, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct Repeat { element: A } #[stable(feature = "rust1", since = "1.0.0")] impl Iterator for Repeat { type Item = A; #[inline] fn next(&mut self) -> Option { Some(self.element.clone()) } #[inline] fn size_hint(&self) -> (usize, Option) { (usize::MAX, None) } } #[stable(feature = "rust1", since = "1.0.0")] impl DoubleEndedIterator for Repeat { #[inline] fn next_back(&mut self) -> Option { Some(self.element.clone()) } } #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Repeat {} #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl TrustedLen for Repeat {} /// Creates a new iterator that endlessly repeats a single element. /// /// The `repeat()` function repeats a single value over and over and over and /// over and over and 🔁. /// /// Infinite iterators like `repeat()` are often used with adapters like /// [`take`], in order to make them finite. /// /// [`take`]: trait.Iterator.html#method.take /// /// If the element type of the iterator you need does not implement `Clone`, /// or if you do not want to keep the repeated element in memory, you can /// instead use the [`repeat_with`] function. /// /// [`repeat_with`]: fn.repeat_with.html /// /// # Examples /// /// Basic usage: /// /// ``` /// use std::iter; /// /// // the number four 4ever: /// let mut fours = iter::repeat(4); /// /// assert_eq!(Some(4), fours.next()); /// assert_eq!(Some(4), fours.next()); /// assert_eq!(Some(4), fours.next()); /// assert_eq!(Some(4), fours.next()); /// assert_eq!(Some(4), fours.next()); /// /// // yup, still four /// assert_eq!(Some(4), fours.next()); /// ``` /// /// Going finite with [`take`]: /// /// ``` /// use std::iter; /// /// // that last example was too many fours. Let's only have four fours. /// let mut four_fours = iter::repeat(4).take(4); /// /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// /// // ... and now we're done /// assert_eq!(None, four_fours.next()); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn repeat(elt: T) -> Repeat { Repeat{element: elt} } /// An iterator that repeats elements of type `A` endlessly by /// applying the provided closure `F: FnMut() -> A`. /// /// This `struct` is created by the [`repeat_with`] function. /// See its documentation for more. /// /// [`repeat_with`]: fn.repeat_with.html #[derive(Copy, Clone, Debug)] #[unstable(feature = "iterator_repeat_with", issue = "48169")] pub struct RepeatWith { repeater: F } #[unstable(feature = "iterator_repeat_with", issue = "48169")] impl A> Iterator for RepeatWith { type Item = A; #[inline] fn next(&mut self) -> Option { Some((self.repeater)()) } #[inline] fn size_hint(&self) -> (usize, Option) { (usize::MAX, None) } } #[unstable(feature = "iterator_repeat_with", issue = "48169")] impl A> DoubleEndedIterator for RepeatWith { #[inline] fn next_back(&mut self) -> Option { self.next() } } #[unstable(feature = "iterator_repeat_with", issue = "48169")] impl A> FusedIterator for RepeatWith {} #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl A> TrustedLen for RepeatWith {} /// Creates a new iterator that repeats elements of type `A` endlessly by /// applying the provided closure, the repeater, `F: FnMut() -> A`. /// /// The `repeat_with()` function calls the repeater over and over and over and /// over and over and 🔁. /// /// Infinite iterators like `repeat_with()` are often used with adapters like /// [`take`], in order to make them finite. /// /// [`take`]: trait.Iterator.html#method.take /// /// If the element type of the iterator you need implements `Clone`, and /// it is OK to keep the source element in memory, you should instead use /// the [`repeat`] function. /// /// [`repeat`]: fn.repeat.html /// /// An iterator produced by `repeat_with()` is a `DoubleEndedIterator`. /// It is important to note that reversing `repeat_with(f)` will produce /// the exact same sequence as the non-reversed iterator. In other words, /// `repeat_with(f).rev().collect::>()` is equivalent to /// `repeat_with(f).collect::>()`. /// /// # Examples /// /// Basic usage: /// /// ``` /// #![feature(iterator_repeat_with)] /// /// use std::iter; /// /// // let's assume we have some value of a type that is not `Clone` /// // or which don't want to have in memory just yet because it is expensive: /// #[derive(PartialEq, Debug)] /// struct Expensive; /// /// // a particular value forever: /// let mut things = iter::repeat_with(|| Expensive); /// /// assert_eq!(Some(Expensive), things.next()); /// assert_eq!(Some(Expensive), things.next()); /// assert_eq!(Some(Expensive), things.next()); /// assert_eq!(Some(Expensive), things.next()); /// assert_eq!(Some(Expensive), things.next()); /// ``` /// /// Using mutation and going finite: /// /// ```rust /// #![feature(iterator_repeat_with)] /// /// use std::iter; /// /// // From the zeroth to the third power of two: /// let mut curr = 1; /// let mut pow2 = iter::repeat_with(|| { let tmp = curr; curr *= 2; tmp }) /// .take(4); /// /// assert_eq!(Some(1), pow2.next()); /// assert_eq!(Some(2), pow2.next()); /// assert_eq!(Some(4), pow2.next()); /// assert_eq!(Some(8), pow2.next()); /// /// // ... and now we're done /// assert_eq!(None, pow2.next()); /// ``` #[inline] #[unstable(feature = "iterator_repeat_with", issue = "48169")] pub fn repeat_with A>(repeater: F) -> RepeatWith { RepeatWith { repeater } } /// An iterator that yields nothing. /// /// This `struct` is created by the [`empty`] function. See its documentation for more. /// /// [`empty`]: fn.empty.html #[stable(feature = "iter_empty", since = "1.2.0")] pub struct Empty(marker::PhantomData); #[stable(feature = "core_impl_debug", since = "1.9.0")] impl fmt::Debug for Empty { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad("Empty") } } #[stable(feature = "iter_empty", since = "1.2.0")] impl Iterator for Empty { type Item = T; fn next(&mut self) -> Option { None } fn size_hint(&self) -> (usize, Option){ (0, Some(0)) } } #[stable(feature = "iter_empty", since = "1.2.0")] impl DoubleEndedIterator for Empty { fn next_back(&mut self) -> Option { None } } #[stable(feature = "iter_empty", since = "1.2.0")] impl ExactSizeIterator for Empty { fn len(&self) -> usize { 0 } } #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl TrustedLen for Empty {} #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Empty {} // not #[derive] because that adds a Clone bound on T, // which isn't necessary. #[stable(feature = "iter_empty", since = "1.2.0")] impl Clone for Empty { fn clone(&self) -> Empty { Empty(marker::PhantomData) } } // not #[derive] because that adds a Default bound on T, // which isn't necessary. #[stable(feature = "iter_empty", since = "1.2.0")] impl Default for Empty { fn default() -> Empty { Empty(marker::PhantomData) } } /// Creates an iterator that yields nothing. /// /// # Examples /// /// Basic usage: /// /// ``` /// use std::iter; /// /// // this could have been an iterator over i32, but alas, it's just not. /// let mut nope = iter::empty::(); /// /// assert_eq!(None, nope.next()); /// ``` #[stable(feature = "iter_empty", since = "1.2.0")] pub fn empty() -> Empty { Empty(marker::PhantomData) } /// An iterator that yields an element exactly once. /// /// This `struct` is created by the [`once`] function. See its documentation for more. /// /// [`once`]: fn.once.html #[derive(Clone, Debug)] #[stable(feature = "iter_once", since = "1.2.0")] pub struct Once { inner: ::option::IntoIter } #[stable(feature = "iter_once", since = "1.2.0")] impl Iterator for Once { type Item = T; fn next(&mut self) -> Option { self.inner.next() } fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } } #[stable(feature = "iter_once", since = "1.2.0")] impl DoubleEndedIterator for Once { fn next_back(&mut self) -> Option { self.inner.next_back() } } #[stable(feature = "iter_once", since = "1.2.0")] impl ExactSizeIterator for Once { fn len(&self) -> usize { self.inner.len() } } #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl TrustedLen for Once {} #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Once {} /// Creates an iterator that yields an element exactly once. /// /// This is commonly used to adapt a single value into a [`chain`] of other /// kinds of iteration. Maybe you have an iterator that covers almost /// everything, but you need an extra special case. Maybe you have a function /// which works on iterators, but you only need to process one value. /// /// [`chain`]: trait.Iterator.html#method.chain /// /// # Examples /// /// Basic usage: /// /// ``` /// use std::iter; /// /// // one is the loneliest number /// let mut one = iter::once(1); /// /// assert_eq!(Some(1), one.next()); /// /// // just one, that's all we get /// assert_eq!(None, one.next()); /// ``` /// /// Chaining together with another iterator. Let's say that we want to iterate /// over each file of the `.foo` directory, but also a configuration file, /// `.foorc`: /// /// ```no_run /// use std::iter; /// use std::fs; /// use std::path::PathBuf; /// /// let dirs = fs::read_dir(".foo").unwrap(); /// /// // we need to convert from an iterator of DirEntry-s to an iterator of /// // PathBufs, so we use map /// let dirs = dirs.map(|file| file.unwrap().path()); /// /// // now, our iterator just for our config file /// let config = iter::once(PathBuf::from(".foorc")); /// /// // chain the two iterators together into one big iterator /// let files = dirs.chain(config); /// /// // this will give us all of the files in .foo as well as .foorc /// for f in files { /// println!("{:?}", f); /// } /// ``` #[stable(feature = "iter_once", since = "1.2.0")] pub fn once(value: T) -> Once { Once { inner: Some(value).into_iter() } }