[−][src]Trait snafu::OptionExt
Additions to Option
.
Required methods
fn context<C>(self, context: C) -> Result<T, Context<NoneError, C>>
Convert an Option
into a Result
with additional
context-sensitive information.
use snafu::{Snafu, OptionExt}; #[derive(Debug, Snafu)] enum Error { UserLookup { user_id: i32 }, } fn example(user_id: i32) -> Result<(), Error> { let name = username(user_id).context(UserLookup { user_id })?; println!("Username was {}", name); Ok(()) } fn username(user_id: i32) -> Option<String> { /* ... */ }
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<NoneError, C>> where
F: FnOnce() -> C,
F: FnOnce() -> C,
Convert an Option
into a Result
with lazily-generated
context-sensitive information.
use snafu::{Snafu, OptionExt}; #[derive(Debug, Snafu)] enum Error { UserLookup { user_id: i32, previous_ids: Vec<i32> }, } fn example(user_id: i32) -> Result<(), Error> { let name = username(user_id).with_context(|| UserLookup { user_id, previous_ids: Vec::new(), })?; println!("Username was {}", name); Ok(()) } fn username(user_id: i32) -> Option<String> { /* ... */ }
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<NoneError, C>: Into<E2>,
Context<NoneError, C>: Into<E2>,
Convert an Option
into a Result
with additional
context-sensitive information.
This is most useful when the final Result
type is already
constrained.
use snafu::{Snafu, OptionExt}; #[derive(Debug, Snafu)] enum Error { UserLookup { user_id: i32 }, } fn example(user_id: i32) -> Result<String, Error> { username(user_id).eager_context(UserLookup { user_id }) } fn username(user_id: i32) -> Option<String> { /* ... */ }
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_eager_context<F, C, E2>(self, context: F) -> Result<T, E2> where
F: FnOnce() -> C,
Context<NoneError, C>: Into<E2>,
F: FnOnce() -> C,
Context<NoneError, C>: Into<E2>,
Convert an Option
into a Result
with lazily-generated
context-sensitive information.
This is most useful when the final Result
type is already
constrained.
use snafu::{Snafu, OptionExt}; #[derive(Debug, Snafu)] enum Error { UserLookup { user_id: i32, previous_ids: Vec<i32> }, } fn example(user_id: i32) -> Result<String, Error> { username(user_id).with_eager_context(|| UserLookup { user_id, previous_ids: Vec::new(), }) } fn username(user_id: i32) -> Option<String> { /* ... */ }
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> OptionExt<T> for Option<T>
[src]
fn context<C>(self, context: C) -> Result<T, Context<NoneError, C>>
[src]
fn with_context<F, C>(self, context: F) -> Result<T, Context<NoneError, C>> where
F: FnOnce() -> C,
[src]
F: FnOnce() -> C,
fn eager_context<C, E2>(self, context: C) -> Result<T, E2> where
Context<NoneError, C>: Into<E2>,
[src]
Context<NoneError, C>: Into<E2>,
fn with_eager_context<F, C, E2>(self, context: F) -> Result<T, E2> where
F: FnOnce() -> C,
Context<NoneError, C>: Into<E2>,
[src]
F: FnOnce() -> C,
Context<NoneError, C>: Into<E2>,