[−][src]Trait snafu::ResultExt
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,
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.
Provided methods
fn eager_context<C, E2>(self, context: C) -> Result<T, E2> where
Context<E, C>: Into<E2>,
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>,
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.
Implementations on Foreign Types
impl<T, E> ResultExt<T, E> for Result<T, E>
[src]
fn context<C>(self, context: C) -> Result<T, Context<E, C>>
[src]
fn with_context<F, C>(self, context: F) -> Result<T, Context<E, C>> where
F: FnOnce() -> C,
[src]
F: FnOnce() -> C,
fn eager_context<C, E2>(self, context: C) -> Result<T, E2> where
Context<E, C>: Into<E2>,
[src]
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>,
[src]
F: FnOnce() -> C,
Context<E, C>: Into<E2>,