Trait OptionExt
pub trait OptionExt<T> {
// Required methods
fn ok_or_error<Kind, Context>(
self,
kind: Kind,
context: Context,
) -> Result<T, Error<Kind>>
where Context: Into<Box<dyn Error + 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 + Send + Sync>>,
F: FnOnce() -> Context;
}Expand description
Extension methods for Option.
Required Methods§
fn ok_or_error<Kind, Context>(
self,
kind: Kind,
context: Context,
) -> Result<T, Error<Kind>>
fn ok_or_error<Kind, Context>( self, kind: Kind, context: Context, ) -> Result<T, Error<Kind>>
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>>
fn ok_or_error_with<Kind, Context, F>( self, kind: Kind, context: F, ) -> Result<T, Error<Kind>>
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);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.