Skip to main content

BoolExt

Trait BoolExt 

Source
pub trait BoolExt {
Show 23 methods // Required methods fn and<T>(self, t: Option<T>) -> Option<T>; fn and_is(self, b: bool) -> bool; fn and_if<F: FnOnce() -> bool>(self, f: F) -> bool; fn and_then<T, F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>; fn clone_or<T: Clone>(self, err: T, t: &T) -> T; fn copy_or<T: Copy>(self, err: T, t: T) -> T; fn expect(self, msg: &str) -> Self; fn expect_false(self, msg: &str) -> Self; fn into_option(self) -> Option<()>; fn into_result(self) -> Result<(), ()>; fn is_false(&self) -> Self; fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T where Self: Sized; fn map_ok_or<T, E, F: FnOnce() -> T>(self, err: E, f: F) -> Result<T, E>; fn map_or<T, F: FnOnce() -> T>(self, err: T, f: F) -> T; fn map_or_else<T, E: FnOnce() -> T, F: FnOnce() -> T>( self, err: E, f: F, ) -> T; fn ok_or<E>(self, err: E) -> Result<(), E>; fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<(), E>; fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T>; fn or_some<T>(self, t: T) -> Option<T>; fn then_async<O: Future, F: FnOnce() -> O>(self, f: F) -> OptionFuture<O> ; fn then_none<T>(self) -> Option<T>; fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>; fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E>;
}
Expand description

Adds composable branching and conversion operations to Boolean values.

The adapters map conditions into options, results, futures, or selected values. Lazy variants invoke their closure only on the documented branch.

Required Methods§

Source

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

Retains t only when the Boolean is true.

A false value produces None regardless of t. An existing None remains absent on either branch.

Source

fn and_is(self, b: bool) -> bool

Computes the conjunction with another Boolean.

Both operands are already evaluated before the method runs. The result is true only when both values are true.

Source

fn and_if<F: FnOnce() -> bool>(self, f: F) -> bool

Computes a conjunction with the closure’s Boolean result.

The closure is invoked even when the receiver is false. Its result is then combined with the receiver using logical conjunction.

Source

fn and_then<T, F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>

Invokes f and returns its option only when the Boolean is true.

A false receiver returns None without calling the closure. A true receiver preserves either option variant returned by f.

Source

fn clone_or<T: Clone>(self, err: T, t: &T) -> T

Clones t when true or returns err when false.

The referenced value is cloned only for the true branch. The owned fallback is returned for false and dropped for true.

Source

fn copy_or<T: Copy>(self, err: T, t: T) -> T

Copies t when true or returns err when false.

Both candidate values are passed by value. The Boolean selects which copy becomes the result.

Source

fn expect(self, msg: &str) -> Self

Asserts that the Boolean is true and returns it.

A successful assertion always returns true. The supplied message is used only for a failed assertion.

§Panics

Panics with msg when the Boolean is false.

Source

fn expect_false(self, msg: &str) -> Self

Asserts that the Boolean is false and returns it.

A successful assertion always returns false. The supplied message is used only for a failed assertion.

§Panics

Panics with msg when the Boolean is true.

Source

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

Converts true to Some(()) and false to None.

The unit value carries no additional state. The option is useful for continuing condition-driven combinator chains.

Source

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

Converts true to Ok(()) and false to Err(()).

Both result payloads are unit values. The conversion preserves only the receiver’s success or failure state.

Source

fn is_false(&self) -> Self

Reports whether the Boolean is false.

The receiver is borrowed rather than consumed. The returned value is the logical negation of the receiver.

Source

fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T
where Self: Sized,

Applies f to the Boolean value.

The closure is invoked exactly once for either Boolean branch. This method allows a Boolean to begin a generic method chain.

Source

fn map_ok_or<T, E, F: FnOnce() -> T>(self, err: E, f: F) -> Result<T, E>

Maps true through f or returns err for false.

The closure is called only for the true branch. The error is supplied eagerly, returned for false, and dropped for true.

Source

fn map_or<T, F: FnOnce() -> T>(self, err: T, f: F) -> T

Maps true through f or returns err for false.

The closure is called only for the true branch. The fallback value is returned directly when the receiver is false.

Source

fn map_or_else<T, E: FnOnce() -> T, F: FnOnce() -> T>(self, err: E, f: F) -> T

Selects between lazy false and true branch closures.

f is invoked when the receiver is true, while err is invoked when it is false. Exactly one closure runs.

Source

fn ok_or<E>(self, err: E) -> Result<(), E>

Converts true to Ok(()) and false to the supplied error.

The error value is supplied eagerly and returned by the false branch. The true branch drops it and carries a unit success value.

Source

fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<(), E>

Converts true to Ok(()) or lazily creates an error for false.

The closure is called only when the receiver is false. A true receiver produces the unit success value directly.

Source

fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T>

Invokes f and returns its value only when the Boolean is false.

A true receiver returns None without calling the closure. It is the false-branch counterpart to bool::then.

Source

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

Returns Some(t) when false and None when true.

The supplied value is returned for false and dropped for true. It is the inverse of bool::then_some.

Source

fn then_async<O: Future, F: FnOnce() -> O>(self, f: F) -> OptionFuture<O>

Creates an optional future only when the Boolean is true.

The closure is not invoked for a false receiver. Awaiting the returned [OptionFuture] yields Some(output) when constructed or None otherwise.

Source

fn then_none<T>(self) -> Option<T>

Produces an absent option for any Boolean value.

The receiver is consumed only to preserve chain shape. No value is constructed for either branch.

Source

fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>

Returns Ok(t) when true or Err(e) when false.

Both values are supplied eagerly and the Boolean selects the result variant. Neither value is converted.

Source

fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E>

Returns Ok(t) when true or lazily creates an error when false.

The error closure is called only for the false branch. The success value is moved into Ok when the receiver is true.

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 BoolExt for bool

Source§

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

Source§

fn and_if<F: FnOnce() -> Self>(self, f: F) -> Self

Source§

fn and_is(self, b: Self) -> Self

Source§

fn and_then<T, F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>

Source§

fn clone_or<T: Clone>(self, err: T, t: &T) -> T

Source§

fn copy_or<T: Copy>(self, err: T, t: T) -> T

Source§

fn expect(self, msg: &str) -> Self

Source§

fn expect_false(self, msg: &str) -> Self

Source§

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

Source§

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

Source§

fn is_false(&self) -> Self

Source§

fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> T
where Self: Sized,

Source§

fn map_ok_or<T, E, F: FnOnce() -> T>(self, err: E, f: F) -> Result<T, E>

Source§

fn map_or<T, F: FnOnce() -> T>(self, err: T, f: F) -> T

Source§

fn map_or_else<T, E: FnOnce() -> T, F: FnOnce() -> T>(self, err: E, f: F) -> T

Source§

fn ok_or<E>(self, err: E) -> Result<(), E>

Source§

fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<(), E>

Source§

fn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T>

Source§

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

Source§

fn then_async<O: Future, F: FnOnce() -> O>(self, f: F) -> OptionFuture<O>

Source§

fn then_none<T>(self) -> Option<T>

Source§

fn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>

Source§

fn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E>

Implementors§