#![doc(html_root_url = "https://docs.rs/streaming-iterator/0.1")]
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
extern crate core;
use core::cmp;
mod sources;
pub use sources::{convert, Convert};
pub use sources::{convert_mut, ConvertMut};
pub use sources::{convert_ref, ConvertRef};
pub use sources::{empty, Empty};
pub use sources::{from_fn, FromFn};
pub use sources::{once, Once};
pub use sources::{once_with, OnceWith};
pub use sources::{repeat, Repeat};
pub use sources::{repeat_with, RepeatWith};
pub use sources::{successors, Successors};
pub trait StreamingIterator {
type Item: ?Sized;
fn advance(&mut self);
fn get(&self) -> Option<&Self::Item>;
#[inline]
fn next(&mut self) -> Option<&Self::Item> {
self.advance();
(*self).get()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
#[inline]
fn all<F>(&mut self, mut f: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
while let Some(i) = self.next() {
if !f(i) {
return false;
}
}
true
}
#[inline]
fn any<F>(&mut self, mut f: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
!self.all(|i| !f(i))
}
#[inline]
fn by_ref(&mut self) -> &mut Self
where
Self: Sized,
{
self
}
#[inline]
fn chain<I>(self, other: I) -> Chain<Self, I>
where
Self: Sized,
I: StreamingIterator<Item = Self::Item> + Sized,
{
Chain {
a: self,
b: other,
state: ChainState::BothForward,
}
}
#[inline]
fn cloned(self) -> Cloned<Self>
where
Self: Sized,
Self::Item: Clone,
{
Cloned(self)
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.fold(0, |count, _| count + 1)
}
#[inline]
fn filter<F>(self, f: F) -> Filter<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
Filter { it: self, f: f }
}
#[inline]
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, B, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> Option<B>,
{
FilterMap {
it: self,
f: f,
item: None,
}
}
#[inline]
fn flat_map<J, F>(self, f: F) -> FlatMap<Self, J, F>
where
Self: Sized,
J: StreamingIterator,
F: FnMut(&Self::Item) -> J,
{
FlatMap {
it: self,
f,
sub_iter: None,
}
}
#[inline]
fn filter_map_deref<B, F>(self, f: F) -> FilterMapDeref<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> Option<B>,
{
FilterMapDeref { it: self, f }
}
#[inline]
fn find<F>(&mut self, mut f: F) -> Option<&Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
loop {
self.advance();
match self.get() {
Some(i) => if f(i) {
break;
},
None => break,
}
}
(*self).get()
}
#[inline]
fn fuse(self) -> Fuse<Self>
where
Self: Sized,
{
Fuse {
it: self,
state: FuseState::Start,
}
}
#[inline]
fn inspect<F>(self, f: F) -> Inspect<Self, F>
where
F: FnMut(&Self::Item),
Self: Sized,
{
Inspect { it: self, f }
}
#[inline]
fn map<B, F>(self, f: F) -> Map<Self, B, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> B,
{
Map {
it: self,
f: f,
item: None,
}
}
#[inline]
fn map_deref<B, F>(self, f: F) -> MapDeref<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> B,
{
MapDeref { it: self, f }
}
#[inline]
fn map_ref<B: ?Sized, F>(self, f: F) -> MapRef<Self, F>
where
Self: Sized,
F: Fn(&Self::Item) -> &B,
{
MapRef { it: self, f: f }
}
#[inline]
fn nth(&mut self, n: usize) -> Option<&Self::Item> {
for _ in 0..n {
self.advance();
if self.get().is_none() {
return None;
}
}
self.next()
}
#[cfg(feature = "std")]
#[inline]
fn owned(self) -> Owned<Self>
where
Self: Sized,
Self::Item: ToOwned,
{
Owned(self)
}
#[inline]
fn position<F>(&mut self, mut f: F) -> Option<usize>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
let mut n = 0;
while let Some(i) = self.next() {
if f(i) {
return Some(n);
}
n += 1;
}
None
}
#[inline]
fn skip(self, n: usize) -> Skip<Self>
where
Self: Sized,
{
Skip { it: self, n: n }
}
#[inline]
fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
SkipWhile {
it: self,
f: f,
done: false,
}
}
#[inline]
fn take(self, n: usize) -> Take<Self>
where
Self: Sized,
{
Take {
it: self,
n: n,
done: false,
}
}
#[inline]
fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
TakeWhile {
it: self,
f: f,
done: false,
}
}
#[inline]
fn rev(self) -> Rev<Self>
where
Self: Sized + DoubleEndedStreamingIterator,
{
Rev(self)
}
#[inline]
fn fold<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, &Self::Item) -> B,
{
let mut acc = init;
while let Some(item) = self.next() {
acc = f(acc, item);
}
acc
}
#[inline]
fn for_each<F>(self, mut f: F)
where
Self: Sized,
F: FnMut(&Self::Item),
{
self.fold((), move |(), item| f(item));
}
}
impl<'a, I: ?Sized> StreamingIterator for &'a mut I
where
I: StreamingIterator,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
(**self).advance()
}
#[inline]
fn get(&self) -> Option<&Self::Item> {
(**self).get()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
#[inline]
fn next(&mut self) -> Option<&Self::Item> {
(**self).next()
}
}
#[cfg(feature = "std")]
impl<I: ?Sized> StreamingIterator for Box<I>
where
I: StreamingIterator,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
(**self).advance()
}
#[inline]
fn get(&self) -> Option<&Self::Item> {
(**self).get()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
}
#[inline]
fn next(&mut self) -> Option<&Self::Item> {
(**self).next()
}
}
pub trait DoubleEndedStreamingIterator: StreamingIterator {
fn advance_back(&mut self);
#[inline]
fn next_back(&mut self) -> Option<&Self::Item> {
self.advance_back();
(*self).get()
}
#[inline]
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, &Self::Item) -> B,
{
let mut acc = init;
while let Some(item) = self.next_back() {
acc = f(acc, item);
}
acc
}
}
pub trait StreamingIteratorMut: StreamingIterator {
fn get_mut(&mut self) -> Option<&mut Self::Item>;
#[inline]
fn next_mut(&mut self) -> Option<&mut Self::Item> {
self.advance();
(*self).get_mut()
}
#[inline]
fn fold_mut<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, &mut Self::Item) -> B,
{
let mut acc = init;
while let Some(item) = self.next_mut() {
acc = f(acc, item);
}
acc
}
#[inline]
fn for_each_mut<F>(self, mut f: F)
where
Self: Sized,
F: FnMut(&mut Self::Item),
{
self.fold_mut((), move |(), item| f(item));
}
#[inline]
fn map_deref_mut<B, F>(self, f: F) -> MapDerefMut<Self, F>
where
Self: Sized,
F: FnMut(&mut Self::Item) -> B,
{
MapDerefMut { it: self, f }
}
#[inline]
fn find_mut<F>(&mut self, mut f: F) -> Option<&mut Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
{
loop {
self.advance();
match self.get() {
Some(i) => if f(i) {
break;
},
None => break,
}
}
(*self).get_mut()
}
}
impl<'a, I: ?Sized> StreamingIteratorMut for &'a mut I
where
I: StreamingIteratorMut,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut Self::Item> {
(**self).get_mut()
}
#[inline]
fn next_mut(&mut self) -> Option<&mut Self::Item> {
(**self).next_mut()
}
}
#[cfg(feature = "std")]
impl<I: ?Sized> StreamingIteratorMut for Box<I>
where
I: StreamingIteratorMut,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut Self::Item> {
(**self).get_mut()
}
#[inline]
fn next_mut(&mut self) -> Option<&mut Self::Item> {
(**self).next_mut()
}
}
pub trait DoubleEndedStreamingIteratorMut:
DoubleEndedStreamingIterator + StreamingIteratorMut
{
#[inline]
fn next_back_mut(&mut self) -> Option<&mut Self::Item> {
self.advance_back();
(*self).get_mut()
}
#[inline]
fn rfold_mut<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, &mut Self::Item) -> B,
{
let mut acc = init;
while let Some(item) = self.next_back_mut() {
acc = f(acc, item);
}
acc
}
}
#[derive(Debug)]
pub struct Chain<A, B> {
a: A,
b: B,
state: ChainState,
}
#[derive(Debug)]
enum ChainState {
BothForward,
BothBackward,
Front,
Back,
}
impl<A, B> StreamingIterator for Chain<A, B>
where
A: StreamingIterator,
B: StreamingIterator<Item = A::Item>,
{
type Item = A::Item;
#[inline]
fn advance(&mut self) {
use ChainState::*;
match self.state {
BothForward | BothBackward => {
self.state = if self.a.next().is_none() {
self.b.advance();
Back
} else {
BothForward
};
}
Front => self.a.advance(),
Back => self.b.advance(),
}
}
#[inline]
fn get(&self) -> Option<&Self::Item> {
use ChainState::*;
match self.state {
BothForward | Front => self.a.get(),
BothBackward | Back => self.b.get(),
}
}
#[inline]
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
where
Self: Sized,
F: FnMut(Acc, &Self::Item) -> Acc,
{
let mut accum = init;
match self.state {
ChainState::Back => {}
_ => accum = self.a.fold(accum, &mut f),
}
match self.state {
ChainState::Front => {}
_ => accum = self.b.fold(accum, &mut f),
}
accum
}
}
impl<A, B> DoubleEndedStreamingIterator for Chain<A, B>
where
A: DoubleEndedStreamingIterator,
B: DoubleEndedStreamingIterator<Item = A::Item>,
{
#[inline]
fn advance_back(&mut self) {
use ChainState::*;
match self.state {
BothForward | BothBackward => {
self.state = if self.b.next_back().is_none() {
self.a.advance_back();
Front
} else {
BothBackward
};
}
Front => self.a.advance_back(),
Back => self.b.advance_back(),
}
}
#[inline]
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
where
Self: Sized,
F: FnMut(Acc, &Self::Item) -> Acc,
{
let mut accum = init;
match self.state {
ChainState::Front => {}
_ => accum = self.b.rfold(accum, &mut f),
}
match self.state {
ChainState::Back => {}
_ => accum = self.a.rfold(accum, &mut f),
}
accum
}
}
impl<A, B> StreamingIteratorMut for Chain<A, B>
where
A: StreamingIteratorMut,
B: StreamingIteratorMut<Item = A::Item>,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut Self::Item> {
use ChainState::*;
match self.state {
BothForward | Front => self.a.get_mut(),
BothBackward | Back => self.b.get_mut(),
}
}
#[inline]
fn fold_mut<Acc, F>(self, init: Acc, mut f: F) -> Acc
where
Self: Sized,
F: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut accum = init;
match self.state {
ChainState::Back => {}
_ => accum = self.a.fold_mut(accum, &mut f),
}
match self.state {
ChainState::Front => {}
_ => accum = self.b.fold_mut(accum, &mut f),
}
accum
}
}
impl<A, B> DoubleEndedStreamingIteratorMut for Chain<A, B>
where
A: DoubleEndedStreamingIteratorMut,
B: DoubleEndedStreamingIteratorMut<Item = A::Item>,
{
fn rfold_mut<Acc, F>(self, init: Acc, mut f: F) -> Acc
where
Self: Sized,
F: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut accum = init;
match self.state {
ChainState::Front => {}
_ => accum = self.b.rfold_mut(accum, &mut f),
}
match self.state {
ChainState::Back => {}
_ => accum = self.a.rfold_mut(accum, &mut f),
}
accum
}
}
#[derive(Clone, Debug)]
pub struct Cloned<I>(I);
impl<I> Iterator for Cloned<I>
where
I: StreamingIterator,
I::Item: Clone,
{
type Item = I::Item;
#[inline]
fn next(&mut self) -> Option<I::Item> {
self.0.next().map(Clone::clone)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.0.fold(init, move |acc, item| f(acc, item.clone()))
}
}
impl<I> DoubleEndedIterator for Cloned<I>
where
I: DoubleEndedStreamingIterator,
I::Item: Clone,
{
#[inline]
fn next_back(&mut self) -> Option<I::Item> {
self.0.next_back().map(Clone::clone)
}
}
#[derive(Debug)]
pub struct Filter<I, F> {
it: I,
f: F,
}
impl<I, F> StreamingIterator for Filter<I, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> bool,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
while let Some(i) = self.it.next() {
if (self.f)(i) {
break;
}
}
}
#[inline]
fn get(&self) -> Option<&I::Item> {
self.it.get()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.it.size_hint().1)
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold(
init,
move |acc, item| {
if f(item) {
fold(acc, item)
} else {
acc
}
},
)
}
}
impl<I, F> DoubleEndedStreamingIterator for Filter<I, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> bool,
{
#[inline]
fn advance_back(&mut self) {
while let Some(i) = self.it.next_back() {
if (self.f)(i) {
break;
}
}
}
#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold(
init,
move |acc, item| {
if f(item) {
fold(acc, item)
} else {
acc
}
},
)
}
}
impl<I, F> StreamingIteratorMut for Filter<I, F>
where
I: StreamingIteratorMut,
F: FnMut(&I::Item) -> bool,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut I::Item> {
self.it.get_mut()
}
#[inline]
fn fold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold_mut(
init,
move |acc, item| {
if f(&*item) {
fold(acc, item)
} else {
acc
}
},
)
}
}
impl<I, F> DoubleEndedStreamingIteratorMut for Filter<I, F>
where
I: DoubleEndedStreamingIteratorMut,
F: FnMut(&I::Item) -> bool,
{
#[inline]
fn rfold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold_mut(
init,
move |acc, item| {
if f(&*item) {
fold(acc, item)
} else {
acc
}
},
)
}
}
#[derive(Debug)]
pub struct FilterMap<I, B, F> {
it: I,
f: F,
item: Option<B>,
}
impl<I, B, F> StreamingIterator for FilterMap<I, B, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> Option<B>,
{
type Item = B;
#[inline]
fn advance(&mut self) {
loop {
match self.it.next() {
Some(i) => if let Some(i) = (self.f)(i) {
self.item = Some(i);
break;
},
None => {
self.item = None;
break;
}
}
}
}
#[inline]
fn get(&self) -> Option<&B> {
self.item.as_ref()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.it.size_hint().1)
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold(init, move |acc, item| match f(item) {
Some(item) => fold(acc, &item),
None => acc,
})
}
}
impl<I, B, F> DoubleEndedStreamingIterator for FilterMap<I, B, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> Option<B>,
{
#[inline]
fn advance_back(&mut self) {
loop {
match self.it.next_back() {
Some(i) => if let Some(i) = (self.f)(i) {
self.item = Some(i);
break;
},
None => {
self.item = None;
break;
}
}
}
}
#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold(init, move |acc, item| match f(item) {
Some(item) => fold(acc, &item),
None => acc,
})
}
}
impl<I, B, F> StreamingIteratorMut for FilterMap<I, B, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> Option<B>,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut B> {
self.item.as_mut()
}
#[inline]
fn fold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold(init, move |acc, item| match f(item) {
Some(mut item) => fold(acc, &mut item),
None => acc,
})
}
}
impl<I, B, F> DoubleEndedStreamingIteratorMut for FilterMap<I, B, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> Option<B>,
{
#[inline]
fn rfold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold(init, move |acc, item| match f(item) {
Some(mut item) => fold(acc, &mut item),
None => acc,
})
}
}
#[derive(Debug)]
pub struct FlatMap<I, J, F> {
it: I,
f: F,
sub_iter: Option<J>,
}
impl<I, J, F> StreamingIterator for FlatMap<I, J, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> J,
J: StreamingIterator,
{
type Item = J::Item;
#[inline]
fn advance(&mut self) {
while self.sub_iter.as_mut().and_then(J::next).is_none() {
if let Some(item) = self.it.next() {
self.sub_iter = Some((self.f)(item));
} else {
break;
}
}
}
#[inline]
fn get(&self) -> Option<&Self::Item> {
self.sub_iter.as_ref().and_then(J::get)
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut acc = init;
if let Some(iter) = self.sub_iter {
acc = iter.fold(acc, &mut fold);
}
let mut f = self.f;
self.it.fold(acc, |acc, item| f(item).fold(acc, &mut fold))
}
}
impl<I, J, F> StreamingIteratorMut for FlatMap<I, J, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> J,
J: StreamingIteratorMut,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut Self::Item> {
self.sub_iter.as_mut().and_then(J::get_mut)
}
#[inline]
fn fold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut acc = init;
if let Some(iter) = self.sub_iter {
acc = iter.fold_mut(acc, &mut fold);
}
let mut f = self.f;
self.it
.fold(acc, |acc, item| f(item).fold_mut(acc, &mut fold))
}
}
#[derive(Debug)]
pub struct FilterMapDeref<I, F> {
it: I,
f: F,
}
impl<I, B, F> Iterator for FilterMapDeref<I, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> Option<B>,
{
type Item = B;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(item) = self.it.next() {
if let Some(mapped) = (self.f)(item) {
return Some(mapped);
}
}
None
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
let mut map = self.f;
self.it.fold(init, move |acc, item| match map(item) {
Some(mapped) => f(acc, mapped),
None => acc,
})
}
}
impl<I, B, F> DoubleEndedIterator for FilterMapDeref<I, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> Option<B>,
{
#[inline]
fn next_back(&mut self) -> Option<B> {
while let Some(item) = self.it.next_back() {
if let Some(mapped) = (self.f)(item) {
return Some(mapped);
}
}
None
}
}
#[derive(Copy, Clone, Debug)]
enum FuseState {
Start,
Middle,
End,
}
#[derive(Clone, Debug)]
pub struct Fuse<I> {
it: I,
state: FuseState,
}
impl<I> StreamingIterator for Fuse<I>
where
I: StreamingIterator,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
match self.state {
FuseState::Start => {
self.it.advance();
self.state = match self.it.get() {
Some(_) => FuseState::Middle,
None => FuseState::End,
};
}
FuseState::Middle => {
self.it.advance();
if let None = self.it.get() {
self.state = FuseState::End;
}
}
FuseState::End => {}
}
}
#[inline]
fn get(&self) -> Option<&I::Item> {
match self.state {
FuseState::Start | FuseState::End => None,
FuseState::Middle => self.it.get(),
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn next(&mut self) -> Option<&I::Item> {
match self.state {
FuseState::Start => match self.it.next() {
Some(i) => {
self.state = FuseState::Middle;
Some(i)
}
None => {
self.state = FuseState::End;
None
}
},
FuseState::Middle => match self.it.next() {
Some(i) => Some(i),
None => {
self.state = FuseState::End;
None
}
},
FuseState::End => None,
}
}
#[inline]
fn count(self) -> usize {
match self.state {
FuseState::Start | FuseState::Middle => self.it.count(),
FuseState::End => 0,
}
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
match self.state {
FuseState::Start | FuseState::Middle => self.it.fold(init, fold),
FuseState::End => init,
}
}
}
impl<I> StreamingIteratorMut for Fuse<I>
where
I: StreamingIteratorMut,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut I::Item> {
match self.state {
FuseState::Start | FuseState::End => None,
FuseState::Middle => self.it.get_mut(),
}
}
#[inline]
fn fold_mut<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
match self.state {
FuseState::Start | FuseState::Middle => self.it.fold_mut(init, fold),
FuseState::End => init,
}
}
}
#[derive(Debug)]
pub struct Inspect<I, F> {
it: I,
f: F,
}
impl<I, F> StreamingIterator for Inspect<I, F>
where
I: StreamingIterator,
F: FnMut(&I::Item),
{
type Item = I::Item;
fn advance(&mut self) {
if let Some(item) = self.it.next() {
(self.f)(item);
}
}
fn get(&self) -> Option<&Self::Item> {
self.it.get()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold(init, |acc, item| {
f(item);
fold(acc, item)
})
}
}
impl<I, F> DoubleEndedStreamingIterator for Inspect<I, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item),
{
fn advance_back(&mut self) {
if let Some(item) = self.it.next_back() {
(self.f)(item);
}
}
#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold(init, |acc, item| {
f(item);
fold(acc, item)
})
}
}
impl<I, F> StreamingIteratorMut for Inspect<I, F>
where
I: StreamingIteratorMut,
F: FnMut(&I::Item),
{
fn get_mut(&mut self) -> Option<&mut Self::Item> {
self.it.get_mut()
}
#[inline]
fn fold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold_mut(init, |acc, item| {
f(&*item);
fold(acc, item)
})
}
}
impl<I, F> DoubleEndedStreamingIteratorMut for Inspect<I, F>
where
I: DoubleEndedStreamingIteratorMut,
F: FnMut(&I::Item),
{
#[inline]
fn rfold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold_mut(init, |acc, item| {
f(&*item);
fold(acc, item)
})
}
}
#[derive(Debug)]
pub struct Map<I, B, F> {
it: I,
f: F,
item: Option<B>,
}
impl<I, B, F> StreamingIterator for Map<I, B, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> B,
{
type Item = B;
#[inline]
fn advance(&mut self) {
self.item = self.it.next().map(&mut self.f);
}
#[inline]
fn get(&self) -> Option<&B> {
self.item.as_ref()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold(init, move |acc, item| fold(acc, &f(item)))
}
}
impl<I, B, F> DoubleEndedStreamingIterator for Map<I, B, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> B,
{
#[inline]
fn advance_back(&mut self) {
self.item = self.it.next_back().map(&mut self.f);
}
#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold(init, move |acc, item| fold(acc, &f(item)))
}
}
impl<I, B, F> StreamingIteratorMut for Map<I, B, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> B,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut B> {
self.item.as_mut()
}
#[inline]
fn fold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.fold(init, move |acc, item| fold(acc, &mut f(item)))
}
}
impl<I, B, F> DoubleEndedStreamingIteratorMut for Map<I, B, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> B,
{
#[inline]
fn rfold_mut<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
let mut f = self.f;
self.it.rfold(init, move |acc, item| fold(acc, &mut f(item)))
}
}
#[derive(Debug)]
pub struct MapDeref<I, F> {
it: I,
f: F,
}
impl<I, B, F> Iterator for MapDeref<I, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> B,
{
type Item = B;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(&mut self.f)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
let mut map = self.f;
self.it.fold(init, move |acc, item| f(acc, map(item)))
}
}
impl<I, B, F> DoubleEndedIterator for MapDeref<I, F>
where
I: DoubleEndedStreamingIterator,
F: FnMut(&I::Item) -> B,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.it.next_back().map(&mut self.f)
}
}
#[derive(Debug)]
pub struct MapDerefMut<I, F> {
it: I,
f: F,
}
impl<I, B, F> Iterator for MapDerefMut<I, F>
where
I: StreamingIteratorMut,
F: FnMut(&mut I::Item) -> B,
{
type Item = B;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.it.next_mut().map(&mut self.f)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
let mut map = self.f;
self.it.fold_mut(init, move |acc, item| f(acc, map(item)))
}
}
impl<I, B, F> DoubleEndedIterator for MapDerefMut<I, F>
where
I: DoubleEndedStreamingIteratorMut,
F: FnMut(&mut I::Item) -> B,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.it.next_back_mut().map(&mut self.f)
}
}
#[derive(Debug)]
pub struct MapRef<I, F> {
it: I,
f: F,
}
impl<I, B: ?Sized, F> StreamingIterator for MapRef<I, F>
where
I: StreamingIterator,
F: Fn(&I::Item) -> &B,
{
type Item = B;
#[inline]
fn advance(&mut self) {
self.it.advance();
}
#[inline]
fn get(&self) -> Option<&B> {
self.it.get().map(&self.f)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
#[inline]
fn next(&mut self) -> Option<&B> {
self.it.next().map(&self.f)
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
let f = self.f;
self.it.fold(init, move |acc, item| fold(acc, f(item)))
}
}
#[cfg(feature = "std")]
#[derive(Clone, Debug)]
pub struct Owned<I>(I);
#[cfg(feature = "std")]
impl<I> Iterator for Owned<I>
where
I: StreamingIterator,
I::Item: ToOwned,
{
type Item = <I::Item as ToOwned>::Owned;
#[inline]
fn next(&mut self) -> Option<<I::Item as ToOwned>::Owned> {
self.0.next().map(ToOwned::to_owned)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.0.fold(init, move |acc, item| f(acc, item.to_owned()))
}
}
#[cfg(feature = "std")]
impl<I> DoubleEndedIterator for Owned<I>
where
I: DoubleEndedStreamingIterator,
I::Item: Sized + ToOwned,
{
#[inline]
fn next_back(&mut self) -> Option<<I::Item as ToOwned>::Owned> {
self.0.next_back().map(ToOwned::to_owned)
}
}
#[derive(Clone, Debug)]
pub struct Skip<I> {
it: I,
n: usize,
}
impl<I> StreamingIterator for Skip<I>
where
I: StreamingIterator,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
self.it.nth(self.n);
self.n = 0;
}
#[inline]
fn get(&self) -> Option<&I::Item> {
self.it.get()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let hint = self.it.size_hint();
(
hint.0.saturating_sub(self.n),
hint.1.map(|n| n.saturating_sub(self.n)),
)
}
#[inline]
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
if self.n > 0 {
if let None = self.it.nth(self.n - 1) {
return init;
}
}
self.it.fold(init, fold)
}
}
impl<I> StreamingIteratorMut for Skip<I>
where
I: StreamingIteratorMut,
{
fn get_mut(&mut self) -> Option<&mut Self::Item> {
self.it.get_mut()
}
#[inline]
fn fold_mut<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
if self.n > 0 {
if let None = self.it.nth(self.n - 1) {
return init;
}
}
self.it.fold_mut(init, fold)
}
}
#[derive(Clone, Debug)]
pub struct SkipWhile<I, F> {
it: I,
f: F,
done: bool,
}
impl<I, F> StreamingIterator for SkipWhile<I, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> bool,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
if !self.done {
let f = &mut self.f;
self.it.find(|i| !f(i));
self.done = true;
} else {
self.it.advance();
}
}
#[inline]
fn get(&self) -> Option<&I::Item> {
self.it.get()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let hint = self.it.size_hint();
(0, hint.1)
}
#[inline]
fn fold<Acc, Fold>(mut self, mut init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
if !self.done {
match self.next() {
Some(item) => init = fold(init, item),
None => return init,
}
}
self.it.fold(init, fold)
}
}
impl<I, F> StreamingIteratorMut for SkipWhile<I, F>
where
I: StreamingIteratorMut,
F: FnMut(&I::Item) -> bool,
{
fn get_mut(&mut self) -> Option<&mut Self::Item> {
self.it.get_mut()
}
#[inline]
fn fold_mut<Acc, Fold>(mut self, mut init: Acc, mut fold: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &mut Self::Item) -> Acc,
{
if !self.done {
match self.next_mut() {
Some(item) => init = fold(init, item),
None => return init,
}
}
self.it.fold_mut(init, fold)
}
}
#[derive(Clone, Debug)]
pub struct Take<I> {
it: I,
n: usize,
done: bool,
}
impl<I> StreamingIterator for Take<I>
where
I: StreamingIterator,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
if self.n != 0 {
self.it.advance();
self.n -= 1;
} else {
self.done = true;
}
}
#[inline]
fn get(&self) -> Option<&I::Item> {
if self.done {
None
} else {
self.it.get()
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let hint = self.it.size_hint();
(cmp::min(hint.0, self.n), Some(self.n))
}
}
impl<I> StreamingIteratorMut for Take<I>
where
I: StreamingIteratorMut,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut I::Item> {
if self.done {
None
} else {
self.it.get_mut()
}
}
}
#[derive(Debug)]
pub struct TakeWhile<I, F> {
it: I,
f: F,
done: bool,
}
impl<I, F> StreamingIterator for TakeWhile<I, F>
where
I: StreamingIterator,
F: FnMut(&I::Item) -> bool,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
if !self.done {
self.it.advance();
if let Some(i) = self.it.get() {
if !(self.f)(i) {
self.done = true;
}
}
}
}
#[inline]
fn get(&self) -> Option<&I::Item> {
if self.done {
None
} else {
self.it.get()
}
}
#[inline]
fn next(&mut self) -> Option<&I::Item> {
if self.done {
None
} else {
match self.it.next() {
Some(i) => if (self.f)(i) {
Some(i)
} else {
self.done = true;
None
},
None => None,
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let upper = if self.done {
Some(0)
} else {
self.it.size_hint().1
};
(0, upper)
}
}
impl<I, F> StreamingIteratorMut for TakeWhile<I, F>
where
I: StreamingIteratorMut,
F: FnMut(&I::Item) -> bool,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut I::Item> {
if self.done {
None
} else {
self.it.get_mut()
}
}
}
pub struct Rev<I>(I);
impl<I> StreamingIterator for Rev<I>
where
I: DoubleEndedStreamingIterator,
{
type Item = I::Item;
#[inline]
fn advance(&mut self) {
self.0.advance_back();
}
#[inline]
fn get(&self) -> Option<&I::Item> {
self.0.get()
}
#[inline]
fn next(&mut self) -> Option<&I::Item> {
self.0.next_back()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn fold<Acc, Fold>(self, init: Acc, f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
self.0.rfold(init, f)
}
}
impl<I> DoubleEndedStreamingIterator for Rev<I>
where
I: DoubleEndedStreamingIterator,
{
#[inline]
fn advance_back(&mut self) {
self.0.advance();
}
#[inline]
fn next_back(&mut self) -> Option<&I::Item> {
self.0.next()
}
#[inline]
fn rfold<Acc, Fold>(self, init: Acc, f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
self.0.fold(init, f)
}
}
impl<I> StreamingIteratorMut for Rev<I>
where
I: DoubleEndedStreamingIteratorMut,
{
#[inline]
fn get_mut(&mut self) -> Option<&mut I::Item> {
self.0.get_mut()
}
#[inline]
fn fold_mut<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, &mut Self::Item) -> B,
{
self.0.rfold_mut(init, f)
}
}
impl<I> DoubleEndedStreamingIteratorMut for Rev<I>
where
I: DoubleEndedStreamingIteratorMut,
{
#[inline]
fn rfold_mut<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, &mut Self::Item) -> B,
{
self.0.fold_mut(init, f)
}
}
#[cfg(test)]
mod test {
use core::fmt::Debug;
use super::*;
fn test<I>(mut it: I, expected: &[I::Item])
where
I: StreamingIterator,
I::Item: Sized + PartialEq + Debug,
{
for item in expected {
it.advance();
assert_eq!(it.get(), Some(item));
assert_eq!(it.get(), Some(item));
}
it.advance();
assert_eq!(it.get(), None);
assert_eq!(it.get(), None);
}
fn test_back<I>(mut it: I, expected: &[I::Item])
where
I: DoubleEndedStreamingIterator,
I::Item: Sized + PartialEq + Debug,
{
for item in expected {
it.advance_back();
assert_eq!(it.get(), Some(item));
assert_eq!(it.get(), Some(item));
}
it.advance_back();
assert_eq!(it.get(), None);
assert_eq!(it.get(), None);
}
fn test_deref<I>(mut it: I, expected: &[I::Item])
where
I: Iterator,
I::Item: Sized + PartialEq + Debug,
{
for item in expected {
assert_eq!(it.next().as_ref(), Some(item));
}
assert_eq!(it.next(), None)
}
#[test]
fn all() {
let items = [0, 1, 2];
let it = convert(items.iter().cloned());
assert!(it.clone().all(|&i| i < 3));
assert!(!it.clone().all(|&i| i % 2 == 0));
}
#[test]
fn any() {
let items = [0, 1, 2];
let it = convert(items.iter().cloned());
assert!(it.clone().any(|&i| i > 1));
assert!(!it.clone().any(|&i| i > 2));
}
#[test]
fn test_chain() {
let items_a = [0, 1, 2, 3];
let items_b = [10, 20, 30];
let expected = [0, 1, 2, 3, 10, 20, 30];
let it = convert(items_a.iter().cloned()).chain(convert(items_b.iter().cloned()));
test(it, &expected);
}
#[test]
fn test_chain_back() {
let items_a = [0, 1, 2, 3];
let items_b = [10, 20, 30];
let expected = [30, 20, 10, 3, 2, 1, 0];
let it = convert(items_a.iter().cloned()).chain(convert(items_b.iter().cloned()));
test_back(it, &expected);
}
#[test]
fn test_chain_mixed() {
let items_a = [0, 1, 2, 3];
let items_b = [10, 20, 30];
let mut it = convert(items_a.iter().cloned()).chain(convert(items_b.iter().cloned()));
assert_eq!(it.get(), None);
it.advance();
assert_eq!(it.get().cloned(), Some(0));
it.advance_back();
assert_eq!(it.get().cloned(), Some(30));
it.advance();
assert_eq!(it.get().cloned(), Some(1));
it.advance_back();
assert_eq!(it.get().cloned(), Some(20));
it.advance();
assert_eq!(it.get().cloned(), Some(2));
it.advance_back();
assert_eq!(it.get().cloned(), Some(10));
it.advance_back();
assert_eq!(it.get().cloned(), Some(3));
}
#[test]
fn cloned() {
let items = [0, 1];
let mut it = convert(items.iter().cloned()).cloned();
assert_eq!(it.next(), Some(0));
assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), None);
}
#[test]
fn test_convert() {
let items = [0, 1];
let it = convert(items.iter().cloned());
test(it, &items);
}
#[test]
fn test_convert_ref() {
let items = [&0, &1];
let it = convert_ref(items.iter());
test(it, &items);
}
#[test]
fn count() {
let items = [0, 1, 2, 3];
let it = convert(items.iter());
assert_eq!(it.count(), 4);
}
#[test]
fn filter() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned()).filter(|x| x % 2 == 0);
test(it, &[0, 2]);
}
#[test]
fn fuse() {
struct Flicker(i32);
impl StreamingIterator for Flicker {
type Item = i32;
fn advance(&mut self) {
self.0 += 1;
}
fn get(&self) -> Option<&i32> {
if self.0 % 4 == 3 {
None
} else {
Some(&self.0)
}
}
}
let mut it = Flicker(0).fuse();
assert_eq!(it.get(), None);
it.advance();
assert_eq!(it.get(), Some(&1));
assert_eq!(it.get(), Some(&1));
it.advance();
assert_eq!(it.get(), Some(&2));
assert_eq!(it.get(), Some(&2));
it.advance();
assert_eq!(it.get(), None);
assert_eq!(it.get(), None);
it.advance();
assert_eq!(it.get(), None);
assert_eq!(it.get(), None);
}
#[test]
fn inspect() {
let items = [0, 1, 2, 3];
let mut idx = 0;
let mut items_inspected = [-1, -1, -1, -1];
{
let it = convert(items.iter().cloned()).inspect(|&i| {
items_inspected[idx] = i;
idx += 1;
});
test(it, &items);
}
assert_eq!(&items_inspected, &items);
}
#[test]
fn map() {
let items = [0, 1];
let it = convert(items.iter().map(|&i| i as usize)).map(|&i| i as i32);
test(it, &items);
}
#[test]
fn map_deref() {
let items = [0, 1];
let it = convert(items.iter().map(|&i| i as usize)).map_deref(|&i| i as i32);
test_deref(it, &items);
}
#[test]
fn map_deref_mut() {
let mut items = [1, 2, 3];
{
let it = convert_mut(&mut items).map_deref_mut(|i| -core::mem::replace(i, 0));
test_deref(it, &[-1, -2, -3]);
}
assert_eq!(items, [0, 0, 0]);
}
#[test]
fn map_ref() {
#[derive(Clone)]
struct Foo(i32);
let items = [Foo(0), Foo(1)];
let it = convert(items.iter().cloned()).map_ref(|f| &f.0);
test(it, &[0, 1]);
}
#[test]
fn flat_map() {
let items = [[0, 1, 2], [3, 4, 5]];
let it = convert(items.iter()).flat_map(|i| convert(i.iter().cloned()));
test(it, &[0, 1, 2, 3, 4, 5]);
}
#[test]
fn nth() {
let items = [0, 1];
let it = convert(items.iter().cloned());
assert_eq!(it.clone().nth(0), Some(&0));
assert_eq!(it.clone().nth(1), Some(&1));
assert_eq!(it.clone().nth(2), None);
}
#[test]
fn filter_map() {
let items = [0u8, 1, 1, 2, 4];
let it = convert(items.iter()).filter_map(|&&i| if i % 2 == 0 { Some(i) } else { None });
test(it, &[0, 2, 4])
}
#[test]
fn filter_map_deref() {
let items = [0u8, 1, 1, 2, 4];
let it =
convert(items.iter()).filter_map_deref(|&&i| if i % 2 == 0 { Some(i) } else { None });
test_deref(it, &[0, 2, 4])
}
#[test]
fn find() {
let items = [0, 1];
let it = convert(items.iter().cloned());
assert_eq!(it.clone().find(|&x| x % 2 == 1), Some(&1));
assert_eq!(it.clone().find(|&x| x % 3 == 2), None);
}
#[test]
#[cfg(feature = "std")]
fn owned() {
let items = [0, 1];
let it = convert(items.iter().cloned()).owned();
assert_eq!(it.collect::<Vec<_>>(), items);
}
#[test]
#[cfg(feature = "std")]
fn owned_str() {
let s = "The quick brown fox jumps over the lazy dog";
let words = s.split_whitespace().map(str::to_owned).collect::<Vec<_>>();
let it = convert_ref(s.split_whitespace()).owned();
assert_eq!(it.collect::<Vec<_>>(), words);
}
#[test]
fn position() {
let items = [0, 1];
let it = convert(items.iter().cloned());
assert_eq!(it.clone().position(|&x| x % 2 == 1), Some(1));
assert_eq!(it.clone().position(|&x| x % 3 == 2), None);
}
#[test]
fn skip() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
test(it.clone().skip(0), &[0, 1, 2, 3]);
test(it.clone().skip(2), &[2, 3]);
test(it.clone().skip(5), &[]);
}
#[test]
fn skip_while() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
test(it.clone().skip_while(|&i| i < 0), &[0, 1, 2, 3]);
test(it.clone().skip_while(|&i| i < 2), &[2, 3]);
test(it.clone().skip_while(|&i| i < 5), &[]);
}
#[test]
fn take() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
test(it.clone().take(0), &[]);
test(it.clone().take(2), &[0, 1]);
test(it.clone().take(5), &[0, 1, 2, 3]);
}
#[test]
fn take_while() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
test(it.clone().take_while(|&i| i < 0), &[]);
test(it.clone().take_while(|&i| i < 2), &[0, 1]);
test(it.clone().take_while(|&i| i < 5), &[0, 1, 2, 3]);
}
#[allow(bare_trait_objects, unknown_lints)]
fn _is_object_safe(_: &StreamingIterator<Item = ()>) {}
#[allow(bare_trait_objects, unknown_lints)]
fn _is_object_safe_mut(_: &StreamingIteratorMut<Item = ()>) {}
#[test]
fn empty_iterator() {
let mut it: Empty<u8> = empty();
assert_eq!(it.next(), None);
}
#[test]
fn rev() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
test(it.clone().rev(), &[3, 2, 1, 0]);
}
#[test]
fn fold() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
assert_eq!(it.fold(0, |acc, i| acc * 10 + i), 123);
}
#[test]
fn for_each() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
let mut acc = 0;
it.for_each(|i| acc = acc * 10 + i);
assert_eq!(acc, 123);
}
#[test]
fn rfold() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
assert_eq!(it.rfold(0, |acc, i| acc * 10 + i), 3210);
}
#[test]
fn for_each_rev() {
let items = [0, 1, 2, 3];
let it = convert(items.iter().cloned());
let mut acc = 0;
it.rev().for_each(|i| acc = acc * 10 + i);
assert_eq!(acc, 3210);
}
#[test]
fn for_each_mut() {
let mut items = [0, 1, 2, 3];
convert_mut(&mut items)
.for_each_mut(|i: &mut i32| *i += 10);
assert_eq!(items, [10, 11, 12, 13]);
convert_mut(&mut items)
.filter(|&i: &i32| i & 1 == 0)
.for_each_mut(|i: &mut i32| *i /= 2);
assert_eq!(items, [5, 11, 6, 13]);
}
}