Skip to main content

Context

Trait Context 

Source
pub trait Context<T> {
    // Required methods
    fn context(self, context: &str) -> Result<T>;
    fn with_context(self, f: impl FnOnce() -> String) -> Result<T>;
}
Expand description

Trait for adding context to the context chain of a Result. This should be used frequently to create a better stack trace.

This trait is also re-exported in libgm::prelude.

Required Methods§

Source

fn context(self, context: &str) -> Result<T>

Adds context to this Result. This pushes a string to the end of the error context chain in case of Err and is a no-op in case of Ok.

Avoid allocating Strings before the error actually occurred. In that case, use Context::with_context for lazy evaluation instead.

Source

fn with_context(self, f: impl FnOnce() -> String) -> Result<T>

Adds context to this Result using the given closure that returns a String.

The context to be appended is lazily evaluated, meaning the closure only executes if an error actually occurred.

This makes it more suited for format! calls since it avoids a heap allocation in the common Ok case.

For more information, see Context::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.

Implementations on Foreign Types§

Source§

impl<T> Context<T> for Result<T, &str>

Source§

fn context(self, context: &str) -> Result<T>

Source§

fn with_context(self, f: impl FnOnce() -> String) -> Result<T>

Source§

impl<T> Context<T> for Result<T, String>

Source§

fn context(self, context: &str) -> Result<T>

Source§

fn with_context(self, f: impl FnOnce() -> String) -> Result<T>

Implementors§

Source§

impl<T> Context<T> for libgm::error::Result<T>