Trait rs_std_ext::option::OptionExt

source ·
pub trait OptionExt<T> {
    // Required methods
    fn err_or<U>(self, ok: U) -> Result<U, T>;
    fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>;
    fn err_or_default<U: Default>(self) -> Result<U, T>;
    fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool;
    fn expect_none(self, msg: &str);
    fn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>;
}
Expand description

Extension methods for std::option::Option.

Required Methods§

source

fn err_or<U>(self, ok: U) -> Result<U, T>

Converts an Option to a Result.

Unlike the standard library, the value of Option is treated as Result::Err instead of Result::Ok.

Arguments passed to err_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use OptionExt::err_or_else, which is lazily evaluated.

§Example
use rs_std_ext::option::OptionExt;
 
assert_eq!(Some("err").err_or(0), Err("err"));
assert_eq!(None::<&str>.err_or(0), Ok(0));
source

fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>

Converts an Option to a Result.

Unlike the standard library, the value of Option is treated as Result::Err instead of Result::Ok.

§Example
use rs_std_ext::option::OptionExt;
 
assert_eq!(Some("err").err_or_else(|| 0), Err("err"));
assert_eq!(None::<&str>.err_or_else(|| 0), Ok(0));
source

fn err_or_default<U: Default>(self) -> Result<U, T>

Converts an Option to a Result.

Unlike the standard library, the value of Option is treated as Result::Err instead of Result::Ok.

§Example
use rs_std_ext::option::OptionExt;
 
assert_eq!(Some("err").err_or_default::<usize>(), Err("err"));
assert_eq!(None::<&str>.err_or_default::<usize>(), Ok(0));
source

fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool

Returns true if the Option is a None or the value inside of it matches a predicate.

This is equivalent to option.is_some_and(!f).

§Example
use rs_std_ext::option::OptionExt;
 
assert_eq!(None::<usize>.is_none_or(|&i| i == 2), true);
assert_eq!(Some(5).is_none_or(|&i| i == 2), false);
source

fn expect_none(self, msg: &str)

Returns nothing, consuming the value contained.

§Panics

Panics if the value is a Some with a custom panic message provided by msg.

§Example
use rs_std_ext::option::OptionExt;
 
let x: Option<&str> = Some("10");
x.expect_none("fruits are healthy");// panics with `fruits are healthy`

See the documentation for Option::expect for more information.

source

fn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>

Tap into an Option and modify its value.

This method captures the Option in order not to break the caller-chain.

§Example
use rs_std_ext::option::OptionExt;
 
let mut x: Option<String> = Some("4".into());
assert_eq!(x.tap_mut(|s| s.push('2')), Some("42".into()));

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

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

source§

fn err_or<U>(self, ok: U) -> Result<U, T>

source§

fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>

source§

fn err_or_default<U: Default>(self) -> Result<U, T>

source§

fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool

source§

fn expect_none(self, msg: &str)

source§

fn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>

Implementors§