pub trait TryReportTupleExt<C> {
type Output;
// Required method
fn try_collect(self) -> Result<Self::Output, Report<[C]>>;
}
Available on crate feature
unstable
only.Expand description
Extends tuples with error-handling capabilities.
This trait provides a method to collect a tuple of Result
s into a single Result
containing a tuple of the successful values, or an error if any of the results failed.
The trait is implemented for tuples of up to 16 elements.
§Stability
This trait is only available behind the unstable
feature flag and is not covered by
semver guarantees. It may change or be removed in future versions without notice.
Required Associated Types§
Required Methods§
Sourcefn try_collect(self) -> Result<Self::Output, Report<[C]>>
fn try_collect(self) -> Result<Self::Output, Report<[C]>>
Attempts to collect all Result
s in the tuple into a single Result
.
§Errors
If any element is Err
, returns the first encountered Err
, with subsequent errors
appended to it.
§Examples
use error_stack::{Report, TryReportTupleExt};
#[derive(Debug)]
struct CustomError;
impl core::fmt::Display for CustomError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Custom error")
}
}
impl core::error::Error for CustomError {}
let result1: Result<i32, Report<CustomError>> = Ok(1);
let result2: Result<&'static str, Report<CustomError>> = Ok("success");
let result3: Result<bool, Report<CustomError>> = Ok(true);
let combined = (result1, result2, result3).try_collect();
assert_eq!(combined.unwrap(), (1, "success", true));
let result1: Result<i32, Report<CustomError>> = Ok(1);
let result2: Result<&'static str, Report<CustomError>> = Err(Report::new(CustomError));
let result3: Result<bool, Report<CustomError>> = Err(Report::new(CustomError));
let combined_with_error = (result1, result2, result3).try_collect();
assert!(combined_with_error.is_err());