pub trait OptionExt<T> {
// Required methods
fn context<C>(self, context: C) -> Result<T, C::Destination>
where C: Contextual<NoSource>;
fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
where F: FnOnce() -> C,
C: Contextual<NoSource>;
}Expand description
Extension trait on Option for converting None into a typed error.
Import via use oopsie::prelude::* or use oopsie::OptionExt.
§Example
use oopsie::{Oopsie, OptionExt as _};
#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum LookupError {
#[oopsie("Key not found: {key}")]
Missing { key: String },
}
let opt: Option<i32> = None;
let err = opt.context(Missing { key: "host" }).unwrap_err();
assert_eq!(err.to_string(), "Key not found: host");Required Methods§
Sourcefn context<C>(self, context: C) -> Result<T, C::Destination>where
C: Contextual<NoSource>,
fn context<C>(self, context: C) -> Result<T, C::Destination>where
C: Contextual<NoSource>,
Convert None into an error with an eagerly-evaluated context selector.
Sourcefn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
Convert None into an error with a lazily-evaluated context selector.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".