TryReportStreamExt

Trait TryReportStreamExt 

Source
pub trait TryReportStreamExt<C>: TryStream<Error: Into<Report<[C]>>> {
    // Provided methods
    fn try_collect_reports<A>(self) -> TryCollectReports<Self, A, C> 
       where A: Default + Extend<Self::Ok>,
             Self: Sized { ... }
    fn try_collect_reports_bounded<A>(
        self,
        bound: usize,
    ) -> TryCollectReports<Self, A, C> 
       where A: Default + Extend<Self::Ok>,
             Self: Sized { ... }
}
Available on crate features futures and unstable only.
Expand description

Trait extending TryStream with methods for collecting error-stack results in a fail-slow manner.

This trait provides additional functionality to TryStreams, allowing for the collection of successful items while accumulating errors. It’s particularly useful when you want to continue processing a stream even after encountering errors, gathering all successful results and errors until the stream is exhausted or a specified error limit is reached.

The fail-slow approach means that the stream processing continues after encountering errors, unlike traditional error handling that might stop at the first error.

§Performance Considerations

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

§Note

This trait is only available behind the unstable flag and is not covered by semver guarantees. It may be subject to breaking changes in future releases.

Provided Methods§

Source

fn try_collect_reports<A>(self) -> TryCollectReports<Self, A, C>
where A: Default + Extend<Self::Ok>, Self: Sized,

Collects all successful items from the stream into a collection, accumulating all errors.

This method will continue processing the stream even after encountering errors, collecting all successful items and all errors until the stream is exhausted.

§Examples

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownError;

impl core::fmt::Display for UnknownError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("UnknownError")
    }
}

impl core::error::Error for UnknownError {}

let stream = stream::iter([
    Ok(1),
    Err(Report::new(UnknownError)),
    Ok(2),
    Err(Report::new(UnknownError)),
]);

let result: Result<Vec<i32>, _> = stream.try_collect_reports().await;
let report = result.expect_err("should have failed twice");

assert_eq!(report.current_contexts().count(), 2);
Source

fn try_collect_reports_bounded<A>( self, bound: usize, ) -> TryCollectReports<Self, A, C>
where A: Default + Extend<Self::Ok>, Self: Sized,

Collects successful items from the stream into a collection, accumulating errors up to a specified bound.

This method will continue processing the stream after encountering errors, but will stop once the number of accumulated errors reaches the specified bound.


#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownError;

impl core::fmt::Display for UnknownError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("UnknownError")
    }
}

impl core::error::Error for UnknownError {}

let stream = stream::iter([
    Ok(1),
    Err(Report::new(UnknownError)),
    Ok(2),
    Err(Report::new(UnknownError)),
]);

let result: Result<Vec<i32>, _> = stream.try_collect_reports_bounded(1).await;
let report = result.expect_err("should have failed twice");

assert_eq!(report.current_contexts().count(), 1);

Implementors§

Source§

impl<S, C> TryReportStreamExt<C> for S
where S: TryStream<Error: Into<Report<[C]>>>,