ReportSink

Struct ReportSink 

Source
pub struct ReportSink<C> { /* private fields */ }
Available on crate feature unstable only.
Expand description

A sink for collecting multiple Reports into a single Result.

ReportSink allows you to accumulate multiple errors or reports and then finalize them into a single Result. This is particularly useful when you need to collect errors from multiple operations before deciding whether to proceed or fail.

The sink is equipped with a “bomb” mechanism to ensure proper usage, if the sink hasn’t been finished when dropped, it will emit a warning or panic, depending on the constructor used.

§Examples

use error_stack::{Report, ReportSink};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct InternalError;

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

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

fn operation1() -> Result<u32, Report<InternalError>> {
    // ...
}

fn operation2() -> Result<(), Report<InternalError>> {
    // ...
}

fn process_data() -> Result<(), Report<[InternalError]>> {
    let mut sink = ReportSink::new();

    if let Some(value) = sink.attempt(operation1()) {
        // process value
    }

    if let Err(e) = operation2() {
        sink.append(e);
    }

    sink.finish()
}

Implementations§

Source§

impl<C> ReportSink<C>

Source

pub const fn new() -> Self

Creates a new ReportSink.

If the sink hasn’t been finished when dropped, it will emit a warning.

Source

pub const fn new_armed() -> Self

Creates a new ReportSink.

If the sink hasn’t been finished when dropped, it will panic.

Source

pub fn append(&mut self, report: impl Into<Report<[C]>>)

Adds a Report to the sink.

§Examples
let mut sink = ReportSink::new();
sink.append(Report::new(io::Error::new(
    io::ErrorKind::Other,
    "I/O error",
)));
Source

pub fn capture(&mut self, error: impl Into<Report<C>>)

Captures a single error or report in the sink.

This method is similar to append, but allows for bare errors without prior Report creation.

§Examples
let mut sink = ReportSink::new();
sink.capture(io::Error::new(io::ErrorKind::Other, "I/O error"));
Source

pub fn attempt<T, R>(&mut self, result: Result<T, R>) -> Option<T>
where R: Into<Report<C>>,

Attempts to execute a fallible operation and collect any errors.

This method takes a Result and returns an Option:

  • If the Result is Ok, it returns Some(T) with the successful value.
  • If the Result is Err, it captures the error in the sink and returns None.

This is useful for concisely handling operations that may fail, allowing you to collect errors while continuing execution.

§Examples
fn fallible_operation() -> Result<u32, io::Error> {
    // ...
}

let mut sink = ReportSink::new();
let value = sink.attempt(fallible_operation());
if let Some(v) = value {
    // Use the successful value
}
// Any errors are now collected in the sink
Source

pub fn finish(self) -> Result<(), Report<[C]>>

Finishes the sink and returns a Result.

This method consumes the sink, and returns Ok(()) if no errors were collected, or Err(Report<[C]>) containing all collected errors otherwise.

§Examples
let mut sink = ReportSink::new();
// ... add errors ...
let result = sink.finish();
Source

pub fn finish_with<T>(self, ok: impl FnOnce() -> T) -> Result<T, Report<[C]>>

Finishes the sink and returns a Result with a custom success value.

Similar to finish, but allows specifying a function to generate the success value.

§Examples
let mut sink = ReportSink::new();
// ... add errors ...
let result = sink.finish_with(|| "Operation completed");
Source

pub fn finish_default<T: Default>(self) -> Result<T, Report<[C]>>

Finishes the sink and returns a Result with a default success value.

Similar to finish, but uses T::default() as the success value.

§Examples
let mut sink = ReportSink::new();
// ... add errors ...
let result: Result<Vec<String>, _> = sink.finish_default();
Source

pub fn finish_ok<T>(self, ok: T) -> Result<T, Report<[C]>>

Finishes the sink and returns a Result with a provided success value.

Similar to finish, but allows specifying a concrete value for the success case.

§Examples
let mut sink = ReportSink::new();
// ... add errors ...
let result = sink.finish_ok(42);

Trait Implementations§

Source§

impl<C> Default for ReportSink<C>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<C> FromResidual for ReportSink<C>

Available on nightly only.
Source§

fn from_residual(residual: <Self as Try>::Residual) -> Self

🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from a compatible Residual type. Read more
Source§

impl<C> Try for ReportSink<C>

Available on nightly only.
Source§

type Output = ()

🔬This is a nightly-only experimental API. (try_trait_v2)
The type of the value produced by ? when not short-circuiting.
Source§

type Residual = Result<Infallible, Report<[C]>>

🔬This is a nightly-only experimental API. (try_trait_v2)
The type of the value passed to FromResidual::from_residual as part of ? when short-circuiting. Read more
Source§

fn from_output((): ()) -> Self

🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from its Output type. Read more
Source§

fn branch(self) -> ControlFlow<Self::Residual, Self::Output>

🔬This is a nightly-only experimental API. (try_trait_v2)
Used in ? to decide whether the operator should produce a value (because this returned ControlFlow::Continue) or propagate a value back to the caller (because this returned ControlFlow::Break). Read more

Auto Trait Implementations§

§

impl<C> Freeze for ReportSink<C>

§

impl<C> !RefUnwindSafe for ReportSink<C>

§

impl<C> Send for ReportSink<C>

§

impl<C> Sync for ReportSink<C>

§

impl<C> Unpin for ReportSink<C>

§

impl<C> !UnwindSafe for ReportSink<C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> OpaqueAttachment for T
where T: Send + Sync + 'static,