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§
Sourcefn and<T>(self, t: Option<T>) -> Option<T>
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.
Sourcefn and_is(self, b: bool) -> bool
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.
Sourcefn and_if<F: FnOnce() -> bool>(self, f: F) -> bool
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.
Sourcefn and_then<T, F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>
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.
Sourcefn clone_or<T: Clone>(self, err: T, t: &T) -> T
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.
Sourcefn copy_or<T: Copy>(self, err: T, t: T) -> T
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.
Sourcefn expect(self, msg: &str) -> Self
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.
Sourcefn expect_false(self, msg: &str) -> Self
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.
Sourcefn into_option(self) -> Option<()>
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.
Sourcefn into_result(self) -> Result<(), ()>
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.
Sourcefn is_false(&self) -> Self
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.
Sourcefn map<T, F: FnOnce(Self) -> T>(self, f: F) -> Twhere
Self: Sized,
fn map<T, F: FnOnce(Self) -> T>(self, f: F) -> Twhere
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.
Sourcefn map_ok_or<T, E, F: FnOnce() -> T>(self, err: E, f: F) -> Result<T, E>
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.
Sourcefn map_or<T, F: FnOnce() -> T>(self, err: T, f: F) -> T
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.
Sourcefn map_or_else<T, E: FnOnce() -> T, F: FnOnce() -> T>(self, err: E, f: F) -> T
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.
Sourcefn ok_or<E>(self, err: E) -> Result<(), E>
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.
Sourcefn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<(), E>
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.
Sourcefn or<T, F: FnOnce() -> T>(self, f: F) -> Option<T>
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.
Sourcefn or_some<T>(self, t: T) -> Option<T>
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.
Sourcefn then_async<O: Future, F: FnOnce() -> O>(self, f: F) -> OptionFuture<O> ⓘ
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.
Sourcefn then_none<T>(self) -> Option<T>
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.
Sourcefn then_ok_or<T, E>(self, t: T, e: E) -> Result<T, E>
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.
Sourcefn then_ok_or_else<T, E, F: FnOnce() -> E>(self, t: T, e: F) -> Result<T, E>
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".