pub trait ResultExt<T, E, C: ErrorContext>: OptionExt<T, C> {
// Required method
fn context_with(self, f: impl FnOnce(&E) -> C) -> Result<T, C::Error>;
}Expand description
Extension trait for result types (and other types carrying errors with them).
Required Methods§
Sourcefn context_with(self, f: impl FnOnce(&E) -> C) -> Result<T, C::Error>
fn context_with(self, f: impl FnOnce(&E) -> C) -> Result<T, C::Error>
Convert to a Result where ther error case is constructed from the given context factory which also accepts a reference to the original error.
§Arguments
f- Error context factory
§Example
#[evitable(description = "Error")]
pub struct Context(String);
// Later
let io_error: std::io::Result<u8> = Err(io::Error::from(io::ErrorKind::NotFound));
let io_ok: std::io::Result<u8> = Ok(42);
// The functions here are called
let error_with_source = io_error.context_with(|e| Context(format!("{:?}", e)));
// The functions here will never be called
let ok_result = io_ok.context_with(|e| Context(unimplemented!()));
assert!(error_with_source.unwrap_err().source().is_some());
ok_result.unwrap();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.