pub trait ContextExt<T>: Sized {
// Required method
fn with_context<C>(
self,
context: impl FnOnce() -> C,
) -> Result<T, BevyError>
where C: Into<Cow<'static, str>>;
// Provided method
fn context<C>(self, context: C) -> Result<T, BevyError>
where C: Into<Cow<'static, str>> { ... }
}Expand description
Extension methods for adding additional context messages to a BevyError
Required Methods§
Sourcefn with_context<C>(self, context: impl FnOnce() -> C) -> Result<T, BevyError>
fn with_context<C>(self, context: impl FnOnce() -> C) -> Result<T, BevyError>
Annotate the error with a context message from a closure
§Example
fn fallible() -> Result<(), BevyError> {
let path = "some_file.txt";
let _message = fs::read_to_string(path)
.with_context(|| format!("failed to read {path}"))?;
Ok(())
}Provided Methods§
Sourcefn context<C>(self, context: C) -> Result<T, BevyError>
fn context<C>(self, context: C) -> Result<T, BevyError>
Annotate the error with a context message.
§Example
fn fallible() -> Result<(), BevyError> {
// Produces a `BevyError` with the message
// "failed to parse number: invalid digit found in string"
let _parsed: usize = "I am not a number"
.parse()
.context("failed to parse number")?;
Ok(())
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".