[][src]Struct repeat_finite::RepeatFinite

pub struct RepeatFinite<T: Clone> { /* fields omitted */ }

An iterator that repeats an element a finite amount of times.

It is roughly equivalent to repeat(x).take(n) from std. It is slightly more efficient that Repeat + Take combo, because on last iteration it returns the element it holds.

It has the same size as repeat(x).take(n) iterator, because instead of being effectively a (T, usize) pair, it is an Option<(T, NonZeroUsize)>. This structure also allows to implement it with 100% safe code. The only unsafe line of code is the unsafe impl for TrustedLen, which is not included by default, only with trusted_len feature of this crate

Implementations

impl<T: Clone> RepeatFinite<T>[src]

#[must_use]pub fn new(x: T, n: usize) -> Self[src]

Creates an iterator that repeats x n times.
The same as function repeat_finite.

let s: String = repeat_finite("asdf", 4).collect();
assert_eq!(s, "asdf".repeat(4));

#[must_use]pub fn try_into_inner(self) -> Option<T>[src]

Returns the inner element.
Note, that this is NOT equivalent to last(), because iteration can cause side-effects.

let b = Box::new([1u8,2,3]);
let mut it = repeat_finite(b, 3);
assert_eq!(it.next(), Some(Box::new([1,2,3])));
assert_eq!(it.next(), Some(Box::new([1,2,3])));

let b2: Box<[u8]> = it.try_into_inner().unwrap();

pub fn try_set_count(&mut self, count: usize) -> usize[src]

Tries to change the number of iteration, returns the new number of iterations.

let b = Box::new([1u8,2,3]);
let mut it = repeat_finite(b, 3);
assert_eq!(it.next(), Some(Box::new([1,2,3])));
assert_eq!(it.next(), Some(Box::new([1,2,3])));

it.try_set_count(0);
assert_eq!(it.try_into_inner(), None);

pub fn take_n(self, n: usize) -> Self[src]

Use this instead of take().
Effectively sets the counter to min(counter, n) and returns modified self.

let mut it = repeat_finite("asdf", 42).take_n(1);
assert_eq!(it.next(), Some("asdf"));
assert_eq!(it.next(), None);

pub fn cycle_nearly_forever(self) -> Self[src]

Use this instead of cycle(). It won't cycle forever, but usize::MAX times, so close enough to that.
If the iterator is exhausted, it stays the same, otherwise counter is set to usize::MAX.

let it = repeat_finite("asdf", 1);
let mut it = it.cycle_nearly_forever();

assert_eq!(it.next(), Some("asdf"));
assert_eq!(it.next(), Some("asdf"));
assert_eq!(it.next(), Some("asdf"));
assert_eq!(it.next(), Some("asdf"));
// One eternity later...
assert_eq!(it.next(), Some("asdf"));
assert_eq!(it.next(), Some("asdf"));
assert_eq!(it.next(), None);

Trait Implementations

impl<T: Clone> Clone for RepeatFinite<T>[src]

impl<T: Debug + Clone> Debug for RepeatFinite<T>[src]

impl<T: Clone> Default for RepeatFinite<T>[src]

impl<T: Clone> ExactSizeIterator for RepeatFinite<T>[src]

impl<T: Clone> FusedIterator for RepeatFinite<T>[src]

impl<T: Clone> Iterator for RepeatFinite<T>[src]

type Item = T

The type of the elements being iterated over.

Auto Trait Implementations

impl<T> Send for RepeatFinite<T> where
    T: Send

impl<T> Sync for RepeatFinite<T> where
    T: Sync

impl<T> Unpin for RepeatFinite<T> where
    T: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.