2020-10-08 01:08:01 +03:00
|
|
|
use crate::iter::{FusedIterator, TrustedLen};
|
|
|
|
|
|
|
|
|
|
/// Creates a new iterator that endlessly repeats a single element.
|
|
|
|
|
///
|
|
|
|
|
/// The `repeat()` function repeats a single value over and over again.
|
|
|
|
|
///
|
|
|
|
|
/// Infinite iterators like `repeat()` are often used with adapters like
|
|
|
|
|
/// [`Iterator::take()`], in order to make them finite.
|
|
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
2020-10-08 14:02:28 +03:00
|
|
|
/// [`repeat_with()`]: crate::iter::repeat_with
|
|
|
|
|
///
|
2020-10-08 01:08:01 +03:00
|
|
|
/// # 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 [`Iterator::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")]
|
2021-07-15 23:47:03 +02:00
|
|
|
#[cfg_attr(not(test), rustc_diagnostic_item = "iter_repeat")]
|
2020-10-08 01:08:01 +03:00
|
|
|
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
|
|
|
|
|
Repeat { element: elt }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An iterator that repeats an element endlessly.
|
|
|
|
|
///
|
|
|
|
|
/// This `struct` is created by the [`repeat()`] function. See its documentation for more.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
pub struct Repeat<A> {
|
|
|
|
|
element: A,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A: Clone> Iterator for Repeat<A> {
|
|
|
|
|
type Item = A;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
|
Some(self.element.clone())
|
|
|
|
|
}
|
2021-05-15 10:37:05 -07:00
|
|
|
|
2020-10-08 01:08:01 +03:00
|
|
|
#[inline]
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
(usize::MAX, None)
|
|
|
|
|
}
|
2021-05-15 10:37:05 -07:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
|
|
|
|
|
// Advancing an infinite iterator of a single element is a no-op.
|
|
|
|
|
let _ = n;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn nth(&mut self, n: usize) -> Option<A> {
|
|
|
|
|
let _ = n;
|
|
|
|
|
Some(self.element.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn last(self) -> Option<A> {
|
|
|
|
|
loop {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn count(self) -> usize {
|
|
|
|
|
loop {}
|
|
|
|
|
}
|
2020-10-08 01:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl<A: Clone> DoubleEndedIterator for Repeat<A> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
|
Some(self.element.clone())
|
|
|
|
|
}
|
2021-05-15 10:37:05 -07:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
|
|
|
|
|
// Advancing an infinite iterator of a single element is a no-op.
|
|
|
|
|
let _ = n;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn nth_back(&mut self, n: usize) -> Option<A> {
|
|
|
|
|
let _ = n;
|
|
|
|
|
Some(self.element.clone())
|
|
|
|
|
}
|
2020-10-08 01:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "fused", since = "1.26.0")]
|
|
|
|
|
impl<A: Clone> FusedIterator for Repeat<A> {}
|
|
|
|
|
|
|
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
|
|
|
|
unsafe impl<A: Clone> TrustedLen for Repeat<A> {}
|