[][src]Trait snafu::ResultExt

pub trait ResultExt<T, E> {
    fn context<C>(self, context: C) -> Result<T, Context<E, C>>;
fn with_context<C>(
        self,
        context: impl FnOnce() -> C
    ) -> Result<T, Context<E, C>>; }

Additions to Result.

Required methods

fn context<C>(self, context: C) -> Result<T, Context<E, C>>

Extend a Result with additional context-sensitive information.

use snafu::{Snafu, ResultExt};

#[derive(Debug, Snafu)]
enum Error {
    Authenticating { user_name: String, user_id: i32, source: ApiError },
}

fn example() -> Result<(), Error> {
    another_function().context(Authenticating { user_name: "admin", user_id: 42 })?;
    Ok(())
}

fn another_function() -> Result<i32, ApiError> {
    /* ... */
}

Note that the From implementation generated by the macro will call Into::into on each field, so the types are not required to exactly match.

fn with_context<C>(
    self,
    context: impl FnOnce() -> C
) -> Result<T, Context<E, C>>

Extend a Result with lazily-generated context-sensitive information.

use snafu::{Snafu, ResultExt};

#[derive(Debug, Snafu)]
enum Error {
    Authenticating { user_name: String, user_id: i32, source: ApiError },
}

fn example() -> Result<(), Error> {
    another_function().with_context(|| Authenticating {
        user_name: "admin".to_string(),
        user_id: 42,
    })?;
    Ok(())
}

fn another_function() -> Result<i32, ApiError> {
    /* ... */
}

Note that this may not be needed in many cases because the From implementation generated by the macro will call Into::into on each field.

Loading content...

Implementations on Foreign Types

impl<T, E> ResultExt<T, E> for Result<T, E>[src]

Loading content...

Implementors

Loading content...