pub trait ResultExt<T>where
Self: Sized,{
// Required method
fn with_context<C, D>(self, chain: C) -> Result<T, Error>
where C: FnOnce(&Error) -> D,
D: Into<Error>;
}
Expand description
Extra convenience functions for results based on core errors.
Required Methods§
Sourcefn with_context<C, D>(self, chain: C) -> Result<T, Error>
fn with_context<C, D>(self, chain: C) -> Result<T, Error>
Provide a context for the result in case it is an error.
The context callback is expected to return a new error, which will replace the given error
and set the replaced error as its source
.
§Examples
use amethyst_error::{Error, ResultExt};
fn failing_function() -> Result<(), Error> {
Err(Error::from_string("failing"))
}
fn other_function() -> Result<(), Error> {
Ok(failing_function().with_context(|_| Error::from_string("other"))?)
}
let e = other_function().expect_err("no error");
assert_eq!("other", e.to_string());
assert_eq!("failing", e.source().expect("no source").to_string());
assert!(e.source().expect("no source").source().is_none());
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.