Skip to main content

CollectAllTheErrors

Trait CollectAllTheErrors 

Source
pub trait CollectAllTheErrors<T, E> {
    // Required methods
    fn partition_results<Oks, Errs>(self) -> (Oks, Errs)
       where Oks: Default + Extend<T>,
             Errs: Default + Extend<E>;
    fn collected_results<Oks, Errs>(self) -> Result<Oks, Errs>
       where Oks: Default + Extend<T>,
             Errs: FromIterator<E>;
    fn collect_oks_or_iter_errs<Oks>(
        self,
    ) -> Result<Oks, impl Iterator<Item = E>>
       where Oks: Default + Extend<T>;
}
Expand description

Trait for getting all the errors from an iterator over results.

Mostly useful for its implementation on Iterator<Item=Result<T, E>>.

Required Methods§

Source

fn partition_results<Oks, Errs>(self) -> (Oks, Errs)
where Oks: Default + Extend<T>, Errs: Default + Extend<E>,

Partition results into success and err values.

Unlike Iterator::partition, which returns, for instance (Vec<Result<T, E>>, Vec<Result<T, E>>), this can return (Vec<T>, Vec<E>).

Source

fn collected_results<Oks, Errs>(self) -> Result<Oks, Errs>
where Oks: Default + Extend<T>, Errs: FromIterator<E>,

Returns Ok(all_oks) if no error occurs, otherwise Err(all_errs).

Unlike Iterator::collect, which might returns, for instance Result<Vec<T>, E>, this can return this can return Result<Vec<T>, Vec<E>>.

Source

fn collect_oks_or_iter_errs<Oks>(self) -> Result<Oks, impl Iterator<Item = E>>
where Oks: Default + Extend<T>,

Returns Ok(all_oks) if no error occurs, otherwise Err(iterator over all errors).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<Iter, T, E> CollectAllTheErrors<T, E> for Iter
where Iter: Iterator<Item = Result<T, E>>,

Primary implementation of the CollectAllTheErrors trait.

§Example: collecting into Vecs

For clarity, let’s assume you want to collect results into Vecs (see the docs for the generic signature).

Bringing this trait into scope will give you the following extra methods on iter: Iterable<Item=Result<T, E>>:

  1. iter.partition_results() -> (Vec<T>, Vec<E>)
  2. iter.collected_results() -> Result<Vec<T>, Vec<E>>
  3. iter.collect_oks_or_iter_errs() -> Result<Vec<T>, impl Iterator<Item=E>>

For example:

use all_the_errors::CollectAllTheErrors;

assert_eq!(
    [Ok(1), Err("a"), Ok(2), Err("b")]
        .into_iter()
        .partition_results(),
    (vec![1, 2], vec!["a", "b"])
);

assert_eq!(
    [Ok(1), Ok(2)]
        .into_iter()
        .collected_results::<Vec<_>, Vec<()>>(),
    Ok(vec![1, 2])
);

assert_eq!(
    [Ok(1), Err("a"), Ok(2), Err("b")]
        .into_iter()
        .collected_results::<Vec<_>, Vec<_>>(),
    Err(vec!["a", "b"])
);