BoolExt

Trait BoolExt 

Source
pub trait BoolExt: Copy {
    // Required methods
    fn some<T>(self, t: T) -> Option<T>;
    fn some_with<T, F>(self, run: F) -> Option<T>
       where F: FnOnce() -> T;
    fn as_option(self) -> Option<()>;
    fn ok<T>(self, t: T) -> Result<T, ()>;
    fn ok_with<T, F>(self, run: F) -> Result<T, ()>
       where F: FnOnce() -> T;
    fn as_result(self) -> Result<(), ()>;
}
Expand description

Various extensions to the bool primitive.

Required Methods§

Source

fn some<T>(self, t: T) -> Option<T>

A stable version of then_some.

Returns Some(t) if the bool is true, or None otherwise.

§Examples
use fenn::BoolExt;

assert_eq!(false.some(0), None);
assert_eq!(true.some(0), Some(0));
Source

fn some_with<T, F>(self, run: F) -> Option<T>
where F: FnOnce() -> T,

A stable version of then.

Returns Some(f()) if the bool is true, or None otherwise.

§Examples
use fenn::BoolExt;

assert_eq!(false.some_with(|| 0), None);
assert_eq!(true.some_with(|| 0), Some(0));
Source

fn as_option(self) -> Option<()>

Returns Some(()) if the bool is true, or None otherwise.

§Examples
use fenn::BoolExt;

assert!(false.as_option().is_none());
assert!(true.as_option().is_some());
Source

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

Returns Ok(t) if the bool is true, or Er(()) otherwise.

§Examples
use fenn::BoolExt;

assert_eq!(false.ok(0), Err(()));
assert_eq!(true.ok(0), Ok(0));
Source

fn ok_with<T, F>(self, run: F) -> Result<T, ()>
where F: FnOnce() -> T,

Returns Ok(run()) if the bool is true, or Er(()) otherwise.

§Examples
use fenn::BoolExt;

assert_eq!(false.ok_with(|| 0), Err(()));
assert_eq!(true.ok_with(|| 0), Ok(0));
Source

fn as_result(self) -> Result<(), ()>

Returns Ok(()) if the bool is true, or Err(()) otherwise.

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.

Implementations on Foreign Types§

Source§

impl BoolExt for bool

Source§

fn some<T>(self, t: T) -> Option<T>

Source§

fn some_with<T, F>(self, run: F) -> Option<T>
where F: FnOnce() -> T,

Source§

fn as_option(self) -> Option<()>

Source§

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

Source§

fn ok_with<T, F>(self, run: F) -> Result<T, ()>
where F: FnOnce() -> T,

Source§

fn as_result(self) -> Result<(), ()>

Implementors§