IteratorExt

Trait IteratorExt 

Source
pub trait IteratorExt: Iterator {
    // Provided methods
    fn accumulate<E>(self, acc: &mut Accumulator<E>) -> Accumulate<'_, Self, E> 
       where Self: Sized,
             Self::Item: IntoResultParts,
             <Self::Item as IntoResultParts>::Err: Into<E> { ... }
    fn collect_errors<E>(self, acc: &mut Accumulator<E>)
       where Self: Sized,
             Self::Item: IntoResultParts,
             <Self::Item as IntoResultParts>::Err: Into<E> { ... }
    fn partition_results<C>(
        self,
    ) -> (C, Accumulator<<Self::Item as IntoResultParts>::Err>)
       where Self: Sized,
             Self::Item: IntoResultParts,
             C: FromIterator<<Self::Item as IntoResultParts>::Ok> { ... }
}
Expand description

Extends Iterator with methods for complex error handling:

Provided Methods§

Source

fn accumulate<E>(self, acc: &mut Accumulator<E>) -> Accumulate<'_, Self, E>
where Self: Sized, Self::Item: IntoResultParts, <Self::Item as IntoResultParts>::Err: Into<E>,

Creates an iterator that filters results, collecting errors into an error accumulator and yielding an iterator over all of the “ok” values.

accumulate can be used to make chains of filter, map, and handle more concise. The example below shows how a common filter_map call can be shortened.

§Examples

Basic usage:

use fused_error::{Accumulator, IteratorExt};

let results = [
    Ok(1),
    Err("foo"),
    Ok(3),
    Err("bar"),
    Ok(5),
];

let mut acc = Accumulator::<&str>::new();

let sum: i32 = results
    .into_iter()
    .accumulate(&mut acc)
    .sum();

assert_eq!(sum, 9);
assert_eq!(acc.into_vec(), ["foo", "bar"]);

Here’s the same example, but with filter_map:

use fused_error::Accumulator;

let results = [
    Ok(1),
    Err("foo"),
    Ok(3),
    Err("bar"),
    Ok(5),
];

let mut acc = Accumulator::<&str>::new();

let sum: i32 = results
    .into_iter()
    .filter_map(|res| acc.handle(res))
    .sum();

assert_eq!(sum, 9);
assert_eq!(acc.into_vec(), ["foo", "bar"]);
Source

fn collect_errors<E>(self, acc: &mut Accumulator<E>)
where Self: Sized, Self::Item: IntoResultParts, <Self::Item as IntoResultParts>::Err: Into<E>,

Drains the errors from an iterator of results into an error accumulator, discarding any “ok” values.

§Examples
use fused_error::{Accumulator, IteratorExt};

let results = [Err("foo"), Ok(()), Err("bar")];
let mut accumulator = Accumulator::<&str>::new();

results.into_iter().collect_errors(&mut accumulator);

assert_eq!(accumulator.into_vec(), ["foo", "bar"]);
Source

fn partition_results<C>( self, ) -> (C, Accumulator<<Self::Item as IntoResultParts>::Err>)
where Self: Sized, Self::Item: IntoResultParts, C: FromIterator<<Self::Item as IntoResultParts>::Ok>,

Unwraps an iterator of results, collecting all errors into a new error accumulator and “ok” values into some collection that implements FromIterator.

§Examples
use fused_error::{Accumulator, IteratorExt};

let results = [
    Ok("foo"),
    Err("bar"),
    Ok("baz"),
    Err("qux"),
];

let (vec, acc): (Vec<_>, _) = results.into_iter().partition_results();
assert_eq!(vec, ["foo", "baz"]);
assert_eq!(acc.into_vec(), ["bar", "qux"]);

Implementors§

Source§

impl<I> IteratorExt for I
where I: Iterator,