SourceContext

Trait SourceContext 

Source
pub trait SourceContext<T, E> {
    // Required methods
    fn with_source_context(self, context: impl FnOnce() -> String) -> Result<T>;
    fn source_context<C>(self, context: C) -> Result<T>
       where C: Display + Send + Sync + 'static;
}

Required Methods§

Source

fn with_source_context(self, context: impl FnOnce() -> String) -> Result<T>

Similar to with_context in anyhow::Context, but adds the source location of the caller. This is especially useful when generating a message to attach to an error object as the context.

Example:

let mount_point = system_task
    .lookup_path_from_root(locked, mount_point)
    .with_source_context(|| {
        format!("lookup path from root: {}", String::from_utf8_lossy(mount_point))
    })?;

Results in a context message like:

Caused by:
    1: lookup path from root: ..., some_file.rs:12:34
Source

fn source_context<C>(self, context: C) -> Result<T>
where C: Display + Send + Sync + 'static,

Similar to context in anyhow::Context, but adds the source location of the caller. This is especially useful when generating a message to attach to an error object as the context.

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.

Implementors§

Source§

impl<T, E> SourceContext<T, E> for Result<T, E>
where Result<T, E>: Context<T, E>,

All result objects that support with_context will also support with_source_context.