all-the-errors 0.1.0-beta3

Collect all the errors from iterators over results
Documentation
  • Coverage
  • 83.33%
    5 out of 6 items documented0 out of 4 items with examples
  • Size
  • Source code size: 64.29 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 657.25 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 31s Average build duration of successful builds.
  • all releases: 27s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • avirshup

all-the-errors

Build Status Latest Version

("clean all the things!" meme except it says "collect all the errors!") [^memesrc]

[^memesrc]: meme source

A silly little trait with a few methods for getting all the errors out of a Iterator<Item=Result<T,E>>.

When you bring the trait all_the_errors::CollectAllTheErrors into scope, it automatically implements 3 methods on Iterators: partition_results, collect_all_results, and collect_oks_or_iter_errs.

No-std compatible: does not use std interally (although it's mostly useful with allocation, as in the example below).

Example: collecting into Vecs

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

The methods available on an iter: Iterable<Item=Result<T, E>> are:

  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), Ok(2), Err("a"), Err("b")]
        .into_iter()
        .partition_results(),
    (vec![1, 2], vec!["a", "b"])
);

assert_eq!(
    [Ok(1), Ok(2)]
        .into_iter()
        .collected_results(),
    Ok(vec![1, 2])
);

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

Why?

This is useful because, given the stdlib's trait implementations, Iterator::collect will stop collecting results after the first error. This allows you to transform, for instance, Iterator<Item=Result<T, E>> into an Result<Vec<T>, E> - i.e., either many Ts or just one E.

But sometimes, though, you want all the errors - e.g., Result<Vec<T>, Vec<E>>, or even a tuple of (Vec<T>, Vec<E>).

Iterator::partition can give you (Vec<Result<T, E>>, Vec<Result<T,E>>), which is annoying because you have to unwrap everything yourself and it adds 2 unecessary allocations.

What about splitting it into 2 different iterators?

License and Copyright Boilerplate

  • License: This project is provided under the MIT License.
  • Copyrightability: All material here was written by (and for) the human authors. No LLM-derived content is present in any form.