pub trait InfiniteIterator: Iterator {
// Required method
fn next_infinite(&mut self) -> Self::Item;
// Provided methods
fn for_each_infinite<F: FnMut(Self::Item)>(self, f: F) -> !
where Self: Sized { ... }
fn find_infinite<P>(&mut self, predicate: P) -> Self::Item
where Self: Sized,
P: FnMut(&Self::Item) -> bool { ... }
fn find_map_infinite<B, F>(&mut self, f: F) -> B
where Self: Sized,
F: FnMut(Self::Item) -> Option<B> { ... }
fn position_infinite<P>(&mut self, predicate: P) -> usize
where Self: Sized,
P: FnMut(Self::Item) -> bool { ... }
}Expand description
An Iterator that never ends.
§Invariants
For this trait to be correctly implemented, the following invariants must be upheld:
Some(iter.next_infinite())must always give the same result asiter.next().- No default-implemented iterator methods may be overriden to have visibly different behaviour than their default implementations.
size_hint().1must always beNone.- The type must not implement
ExactSizeIterator.
If any of the above invariants are violated,
the behaviour of any code that uses the iterator is unspecified
(i.e. it may panic, abort or give wrong results) —
however, because InfiniteIterator is not an unsafe trait
it still must not invoke Undefined Behaviour.
Required Methods§
Sourcefn next_infinite(&mut self) -> Self::Item
fn next_infinite(&mut self) -> Self::Item
Like Iterator::next,
but never returning None because the iterator never ends.
Provided Methods§
Sourcefn for_each_infinite<F: FnMut(Self::Item)>(self, f: F) -> !where
Self: Sized,
fn for_each_infinite<F: FnMut(Self::Item)>(self, f: F) -> !where
Self: Sized,
Like Iterator::for_each,
but it never returns because the iterator never ends.
§Examples
use infinite_iterator::InfiniteIterator;
fn run() -> ! {
(0..).for_each_infinite(|num| {
println!("{num}");
std::thread::sleep(std::time::Duration::from_secs(5));
})
}Sourcefn find_infinite<P>(&mut self, predicate: P) -> Self::Item
fn find_infinite<P>(&mut self, predicate: P) -> Self::Item
Like Iterator::find,
but it is guaranteed to find an item
(or loop forever)
because the iterator is infinite.
§Examples
use infinite_iterator::InfiniteIterator;
assert_eq!((5..).find_infinite(|&num| num > 10), 11);Sourcefn find_map_infinite<B, F>(&mut self, f: F) -> B
fn find_map_infinite<B, F>(&mut self, f: F) -> B
Like Iterator::find_map,
but it is guaranteed to find an item
(or loop forever)
because the iterator is infinite.
§Examples
use infinite_iterator::InfiniteIterator;
assert_eq!((5_u32..).step_by(3).find_map_infinite(|num| num.checked_sub(10)), 1);Sourcefn position_infinite<P>(&mut self, predicate: P) -> usize
fn position_infinite<P>(&mut self, predicate: P) -> usize
Like Iterator::position,
but it is guaranteed to find an item
(or loop forever)
because the iterator is infinite.
§Examples
use infinite_iterator::InfiniteIterator;
assert_eq!((5..).position_infinite(|num| num > 10), 6);