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 collect_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§
Sourcefn partition_results<Oks, Errs>(self) -> (Oks, Errs)
fn partition_results<Oks, Errs>(self) -> (Oks, Errs)
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>).
Sourcefn collect_results<Oks, Errs>(self) -> Result<Oks, Errs>
fn collect_results<Oks, Errs>(self) -> Result<Oks, Errs>
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>>.
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§
impl<Iter, T, E> CollectAllTheErrors<T, E> for Iter
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>>:
iter.partition_results() -> (Vec<T>, Vec<E>)iter.collect_results() -> Result<Vec<T>, Vec<E>>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()
.collect_results::<Vec<_>, Vec<()>>(),
Ok(vec![1, 2])
);
assert_eq!(
[Ok(1), Err("a"), Ok(2), Err("b")]
.into_iter()
.collect_results::<Vec<_>, Vec<_>>(),
Err(vec!["a", "b"])
);