[−][src]Trait evitable::ResultExt
Extension trait for result types (and other types carrying errors with them).
Required methods
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();