[]Trait astral::error::OptionExt

pub trait OptionExt<T> {
    fn ok_or_error<Kind, Context>(
        self,
        kind: Kind,
        context: Context
    ) -> Result<T, Error<Kind>>
    where
        Context: Into<Box<dyn Error + 'static + Send + Sync>>
;
fn ok_or_error_with<Kind, Context, F>(
        self,
        kind: Kind,
        context: F
    ) -> Result<T, Error<Kind>>
    where
        Context: Into<Box<dyn Error + 'static + Send + Sync>>,
        F: FnOnce() -> Context
; }

Extension methods for Option.

Required methods

fn ok_or_error<Kind, Context>(
    self,
    kind: Kind,
    context: Context
) -> Result<T, Error<Kind>> where
    Context: Into<Box<dyn Error + 'static + Send + Sync>>, 

Transforms the Option<T> into a Result<T, Error<Kind>>, mapping Some(v) to Ok(v) and None to Err(Error::new(kind, context)).

Example

use astral::error::OptionExt;

#[derive(Debug, PartialEq)]
enum MyErrorKind {
    Variant,
}

let option: Option<u32> = None;
let x = option.ok_or_error(MyErrorKind::Variant, "oh no!").unwrap_err();

assert_eq!(x.kind(), &MyErrorKind::Variant);

fn ok_or_error_with<Kind, Context, F>(
    self,
    kind: Kind,
    context: F
) -> Result<T, Error<Kind>> where
    Context: Into<Box<dyn Error + 'static + Send + Sync>>,
    F: FnOnce() -> Context, 

Transforms the Option<T> into a Result<T, Error<Kind>>, mapping Some(v) to Ok(v) and None to Err(Error::new(kind, context)) by applying the provided closure FnOnce() -> impl Into<Box<dyn error::Error + Send + Sync>>.

Example

use astral::error::OptionExt;

#[derive(Debug, PartialEq)]
enum MyErrorKind {
    Variant,
}

let option: Option<u32> = None;
let x = option.ok_or_error_with(MyErrorKind::Variant, || "oh no!").unwrap_err();

assert_eq!(x.kind(), &MyErrorKind::Variant);
Loading content...

Implementations on Foreign Types

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

Loading content...

Implementors

Loading content...