TryReportIteratorExt

Trait TryReportIteratorExt 

Source
pub trait TryReportIteratorExt<C> {
    type Ok;

    // Required methods
    fn try_collect_reports<A>(self) -> Result<A, Report<[C]>>
       where A: FromIterator<Self::Ok>;
    fn try_collect_reports_bounded<A>(
        self,
        bound: usize,
    ) -> Result<A, Report<[C]>>
       where A: FromIterator<Self::Ok>;
}
Available on crate feature unstable only.
Expand description

An extension trait for iterators that enables error-aware collection of items.

This trait enhances iterators yielding Result items by providing methods to collect successful items into a container while aggregating encountered errors.

§Performance Considerations

These methods may have performance implications as they potentially iterate through the entire collection, even after encountering errors.

§Unstable Feature

This trait is currently available only under the unstable feature flag and does not adhere to semver guarantees. Its API may change in future releases.

Required Associated Types§

Source

type Ok

The type of the successful items in the iterator.

Required Methods§

Source

fn try_collect_reports<A>(self) -> Result<A, Report<[C]>>
where A: FromIterator<Self::Ok>,

Collects the successful items from the iterator into a container, or returns all errors that occured.

This method attempts to collect all successful items from the iterator into the specified container type. If an error is encountered during iteration, the method will exhaust the iterator and return a Report containing all errors encountered.

§Errors

If any error is encountered during iteration, the method will return a Report containing all errors encountered up to that point.

§Examples
use error_stack::{Report, TryReportIteratorExt};
use std::io;

fn fetch_fail() -> Result<u8, Report<io::Error>> {
   ...
}

let results = [Ok(1_u8), fetch_fail(), Ok(2), fetch_fail(), fetch_fail()];
let collected: Result<Vec<_>, _> = results.into_iter().try_collect_reports();
let error = collected.expect_err("multiple calls should have failed");

assert_eq!(error.current_contexts().count(), 3);
Source

fn try_collect_reports_bounded<A>(self, bound: usize) -> Result<A, Report<[C]>>
where A: FromIterator<Self::Ok>,

Collects the successful items from the iterator into a container or returns all errors up to the specified bound.

This method is similar to try_collect_reports, but it limits the number of errors collected to the specified bound. If the number of errors encountered exceeds the bound, the method stops collecting errors and returns the collected errors up to that point.

§Errors

If any error is encountered during iteration, the method will return a Report containing all errors encountered up to the specified bound.

§Examples
use error_stack::{Report, TryReportIteratorExt};
use std::io;

fn fetch_fail() -> Result<u8, Report<io::Error>> {
   ...
}

let results = [Ok(1_u8), fetch_fail(), Ok(2), fetch_fail(), fetch_fail()];
let collected: Result<Vec<_>, _> = results.into_iter().try_collect_reports_bounded(2);
let error = collected.expect_err("should have failed");

assert_eq!(error.current_contexts().count(), 2);

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<T, C, R, I> TryReportIteratorExt<C> for I
where I: Iterator<Item = Result<T, R>>, R: Into<Report<[C]>>, C: Context,

Source§

type Ok = T