Skip to main content

ContextExt

Trait ContextExt 

Source
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§

Source

fn with_context<C>(self, context: impl FnOnce() -> C) -> Result<T, BevyError>
where C: Into<Cow<'static, str>>,

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§

Source

fn context<C>(self, context: C) -> Result<T, BevyError>
where C: Into<Cow<'static, str>>,

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".

Implementations on Foreign Types§

Source§

impl<T, E> ContextExt<T> for Result<T, E>
where E: Into<BevyError>,

Source§

fn with_context<C>(self, context: impl FnOnce() -> C) -> Result<T, BevyError>
where C: Into<Cow<'static, str>>,

Source§

impl<T> ContextExt<T> for Option<T>

Source§

fn with_context<C>(self, context: impl FnOnce() -> C) -> Result<T, BevyError>
where C: Into<Cow<'static, str>>,

Implementors§