pub struct ReportSink<C> { /* private fields */ }
unstable
only.Expand description
A sink for collecting multiple Report
s 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>
impl<C> ReportSink<C>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new ReportSink
.
If the sink hasn’t been finished when dropped, it will emit a warning.
Sourcepub const fn new_armed() -> Self
pub const fn new_armed() -> Self
Creates a new ReportSink
.
If the sink hasn’t been finished when dropped, it will panic.
Sourcepub fn attempt<T, R>(&mut self, result: Result<T, R>) -> Option<T>
pub fn attempt<T, R>(&mut self, result: Result<T, R>) -> Option<T>
Attempts to execute a fallible operation and collect any errors.
This method takes a Result
and returns an Option
:
- If the
Result
isOk
, it returnsSome(T)
with the successful value. - If the
Result
isErr
, it captures the error in the sink and returnsNone
.
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
Trait Implementations§
Source§impl<C> Default for ReportSink<C>
impl<C> Default for ReportSink<C>
Source§impl<C> FromResidual for ReportSink<C>
Available on nightly
only.
impl<C> FromResidual for ReportSink<C>
nightly
only.Source§impl<C> Try for ReportSink<C>
Available on nightly
only.
impl<C> Try for ReportSink<C>
nightly
only.Source§type Output = ()
type Output = ()
try_trait_v2
)?
when not short-circuiting.Source§type Residual = Result<Infallible, Report<[C]>>
type Residual = Result<Infallible, Report<[C]>>
try_trait_v2
)FromResidual::from_residual
as part of ?
when short-circuiting. Read moreSource§fn from_output((): ()) -> Self
fn from_output((): ()) -> Self
try_trait_v2
)Output
type. Read moreSource§fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
try_trait_v2
)?
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