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>;
}
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§
Required Methods§
Sourcefn try_collect_reports<A>(self) -> Result<A, Report<[C]>>where
A: FromIterator<Self::Ok>,
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);
Sourcefn try_collect_reports_bounded<A>(self, bound: usize) -> 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>,
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.