ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T> {
    // Required method
    fn context(self, context: impl Into<String>) -> Result<T>;
}
Expand description

Extension trait for adding context to Results.

This trait provides a convenient way to add context to any Result type, similar to anyhow::Context.

§Example

use reasonkit::{Result, error::ResultExt};

fn load_data(path: &str) -> Result<String> {
    std::fs::read_to_string(path)
        .context(format!("Failed to load data from {}", path))
}

Required Methods§

Source

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

Add context to an error.

If the result is Ok, it is returned unchanged. If the result is Err, the error is wrapped with the given context.

§Arguments
  • context - Description of the operation that was being attempted

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, E> ResultExt<T> for Result<T, E>
where E: Error + Send + Sync + 'static,

Source§

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

Implementors§