#![no_std]
#[cfg(feature = "use_alloc")]
extern crate alloc;
#[cfg(feature = "use_alloc")]
use alloc::vec::Vec;
use core::fmt;
pub mod structs {
pub use crate::{
and_then::{AndThen, AndThenBorrow},
flatten::FlattenOk,
map_err::MapErr,
map_ok::{MapOk, MapOkBorrow},
};
}
mod and_then;
mod flatten;
mod map_err;
mod map_ok;
pub trait Iterflow: Iterator {
fn map_err<F, T, E, R>(self, op: F) -> map_err::MapErr<Self, F>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(E) -> R,
{
map_err::map_err(self, op)
}
fn map_ok<F, T, U, E>(self, op: F) -> map_ok::MapOk<Self, F>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(T) -> U,
{
map_ok::map_ok(self, op)
}
fn map_ok_borrow<F, T, U, E>(self, op: F) -> map_ok::MapOkBorrow<Self, F>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(&T) -> U,
{
map_ok::map_ok_borrow(self, op)
}
fn and_then<F, T, U, E, R>(self, op: F) -> and_then::AndThen<Self, F>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(T) -> Result<U, R>,
E: From<R>,
{
and_then::and_then(self, op)
}
fn and_then_borrow<F, T, U, E, R>(self, op: F) -> and_then::AndThenBorrow<Self, F>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(&T) -> Result<U, R>,
E: From<R>,
{
and_then::and_then_borrow(self, op)
}
fn flat_map_ok<F, T, U, E>(self, op: F) -> flatten::FlattenOk<map_ok::MapOk<Self, F>, U, E>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(T) -> U,
U: IntoIterator,
{
flatten::flat_map_ok(self, op)
}
fn flat_map_ok_borrow<F, T, U, E>(self, op: F) -> flatten::FlattenOk<map_ok::MapOkBorrow<Self, F>, U, E>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(&T) -> U,
U: IntoIterator,
{
flatten::flat_map_ok_borrow(self, op)
}
fn and_then_flat<F, T, U, E, R>(self, op: F) -> flatten::FlattenOk<and_then::AndThen<Self, F>, U, E>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(T) -> Result<U, R>,
E: From<R>,
U: IntoIterator,
{
flatten::and_then_flat(self, op)
}
fn and_then_flat_borrow<F, T, U, E, R>(self, op: F) -> flatten::FlattenOk<and_then::AndThenBorrow<Self, F>, U, E>
where
Self: Iterator<Item = Result<T, E>> + Sized,
F: FnMut(&T) -> Result<U, R>,
E: From<R>,
U: IntoIterator,
{
flatten::and_then_flat_borrow(self, op)
}
fn finish<T, U, E>(self) -> Result<U, E>
where
Self: Iterator<Item = Result<T, E>> + Sized,
Result<U, E>: FromIterator<Result<T, E>>,
{
self.collect()
}
#[cfg(feature = "use_alloc")]
fn finish_vec<T, E>(self) -> Result<Vec<T>, E>
where
Self: Iterator<Item = Result<T, E>> + Sized,
{
self.collect()
}
}
impl<T: ?Sized> Iterflow for T where T: Iterator {}
pub fn assert_equal<I, J>(a: I, b: J)
where
I: IntoIterator,
J: IntoIterator,
I::Item: fmt::Debug + PartialEq<J::Item>,
J::Item: fmt::Debug,
{
let mut ia = a.into_iter();
let mut ib = b.into_iter();
let mut i = 0;
loop {
match (ia.next(), ib.next()) {
(None, None) => return,
(a, b) => {
let equal = match (&a, &b) {
(&Some(ref a), &Some(ref b)) => a == b,
_ => false,
};
assert!(
equal,
"Failed assertion {a:?} == {b:?} for iteration {i}",
i = i,
a = a,
b = b
);
i += 1;
}
}
}
}