[][src]Trait snafu::ResultExt

pub trait ResultExt<T, E>: Sized {
    fn context<C>(self, context: C) -> Result<T, Context<E, C>>;
fn with_context<F, C>(self, context: F) -> Result<T, Context<E, C>>
    where
        F: FnOnce() -> C
; fn eager_context<C, E2>(self, context: C) -> Result<T, E2>
    where
        Context<E, C>: Into<E2>
, { ... }
fn with_eager_context<F, C, E2>(self, context: F) -> Result<T, E2>
    where
        F: FnOnce() -> C,
        Context<E, C>: Into<E2>
, { ... } }

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<F, C>(self, context: F) -> Result<T, Context<E, C>> where
    F: FnOnce() -> 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...

Provided methods

fn eager_context<C, E2>(self, context: C) -> Result<T, E2> where
    Context<E, C>: Into<E2>, 

Extend a Result with additional context-sensitive information and immediately convert it to another Result.

This is most useful when using Result's combinators and when the final Result type is already constrained.

use snafu::{Snafu, ResultExt};

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

fn example() -> Result<i32, Error> {
    another_function()
        .map(|v| v + 10)
        .eager_context(Authenticating { user_name: "admin", user_id: 42 })
}

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

fn with_eager_context<F, C, E2>(self, context: F) -> Result<T, E2> where
    F: FnOnce() -> C,
    Context<E, C>: Into<E2>, 

Extend a Result with lazily-generated context-sensitive information and immediately convert it to another Result.

This is most useful when using Result's combinators and when the final Result type is already constrained.

use snafu::{Snafu, ResultExt};

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

fn example() -> Result<i32, Error> {
    another_function()
        .map(|v| v + 10)
        .with_eager_context(|| Authenticating {
            user_name: "admin".to_string(),
            user_id: 42,
        })
}

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]

fn eager_context<C, E2>(self, context: C) -> Result<T, E2> where
    Context<E, C>: Into<E2>, 
[src]

fn with_eager_context<F, C, E2>(self, context: F) -> Result<T, E2> where
    F: FnOnce() -> C,
    Context<E, C>: Into<E2>, 
[src]

Loading content...

Implementors

Loading content...