Skip to main content

OptionExt

Trait OptionExt 

Source
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§

Source

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.

Source

fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
where F: FnOnce() -> C, C: Contextual<NoSource>,

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".

Implementations on Foreign Types§

Source§

impl<T> OptionExt<T> for Option<T>

Source§

fn context<C>(self, context: C) -> Result<T, C::Destination>
where C: Contextual<NoSource>,

Source§

fn with_context<F, C>(self, context: F) -> Result<T, C::Destination>
where F: FnOnce() -> C, C: Contextual<NoSource>,

Implementors§