imag/libimagutil/src/iter.rs
John Sirois 7f57e5e234 Move from a helper function to a typeclass.
This introduces the `FoldResut` trait to move from `func(receiver, ...)`
style to `receiver.func(...)` style. Also add a means to pass the
default result explicitly.
2016-07-14 07:28:54 -06:00

30 lines
1,004 B
Rust

/// Folds its contents to a result.
pub trait FoldResult: Sized {
type Item;
/// Processes all contained items returning the last successful result or the first error.
/// If there are no items, returns `Ok(R::default())`.
fn fold_defresult<R, E, F>(self, func: F) -> Result<R, E>
where R: Default,
F: FnMut(Self::Item)
-> Result<R, E>
{
self.fold_result(R::default(), func)
}
/// Processes all contained items returning the last successful result or the first error.
/// If there are no items, returns `Ok(default)`.
fn fold_result<R, E, F>(self, default: R, mut func: F) -> Result<R, E>
where F: FnMut(Self::Item) -> Result<R, E>;
}
impl<X, I: Iterator<Item = X>> FoldResult for I {
type Item = X;
fn fold_result<R, E, F>(self, default: R, mut func: F) -> Result<R, E>
where F: FnMut(Self::Item) -> Result<R, E>
{
self.fold(Ok(default), |acc, item| acc.and_then(|_| func(item)))
}
}