Skip to main content

ToTrue

Trait ToTrue 

Source
pub trait ToTrue: Sized {
    // Required methods
    fn to_true<F, R>(&mut self, f: F) -> Option<R>
       where F: FnOnce() -> R;
    fn to_false<F, R>(&mut self, f: F) -> Option<R>
       where F: FnOnce() -> R;
}
Expand description

Convenient conversion of bool states

§Examples

let mut state = false;
let mut n = 0;

assert_eq!(state.to_true(|| n += 1), Some(()));
assert_eq!((n, state), (1, true));

assert_eq!(state.to_true(|| n += 1), None);
assert_eq!((n, state), (1, true));

assert_eq!(state.to_false(|| n += 1), Some(()));
assert_eq!((n, state), (2, false));

assert_eq!(state.to_false(|| n += 1), None);
assert_eq!((n, state), (2, false));

Required Methods§

Source

fn to_true<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce() -> R,

Run f when *self == false, then assign *self to true

Source

fn to_false<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce() -> R,

Run f when *self == true, then assign *self to false

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ToTrue for bool

Source§

fn to_true<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce() -> R,

Source§

fn to_false<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce() -> R,

Implementors§