Trait astral_engine::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 + Sync + Send + 'static, Global>>;
fn ok_or_error_with<Kind, Context, F>(
self,
kind: Kind,
context: F
) -> Result<T, Error<Kind>>
where
Context: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
F: FnOnce() -> Context;
}Expand description
Extension methods for Option.
Required Methods
sourcefn ok_or_error<Kind, Context>(
self,
kind: Kind,
context: Context
) -> Result<T, Error<Kind>>where
Context: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
fn ok_or_error<Kind, Context>(
self,
kind: Kind,
context: Context
) -> Result<T, Error<Kind>>where
Context: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
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);sourcefn ok_or_error_with<Kind, Context, F>(
self,
kind: Kind,
context: F
) -> Result<T, Error<Kind>>where
Context: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
F: FnOnce() -> Context,
fn ok_or_error_with<Kind, Context, F>(
self,
kind: Kind,
context: F
) -> Result<T, Error<Kind>>where
Context: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
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);