Struct QuickBool

Source
pub struct QuickBool { /* private fields */ }
Expand description

A lock-free boolean implementation using atomic operations.

This type represents a 3-way boolean state:

  • Unset: The value hasn’t been evaluated yet
  • True: The value is true
  • False: The value is false

Once set to true or false, the value cannot be changed, making it effectively immutable after initialization.

Implementations§

Source§

impl QuickBool

Source

pub const fn new() -> Self

Creates a new QuickBool in the unset state.

Source

pub fn get_or_set<F>(&self, f: F) -> bool
where F: FnOnce() -> bool,

Gets the current value, evaluating the closure if the value is unset.

This method is lock-free and will only evaluate the closure once. Subsequent calls will return the cached value.

§Arguments
  • f - A closure that returns a boolean value
§Returns

The boolean value, either from cache or newly computed

§Example
use quick_bool::QuickBool;
 
let quick_bool = QuickBool::new();
let value = quick_bool.get_or_set(|| {
    // This expensive computation only happens once
    std::thread::sleep(std::time::Duration::from_millis(100));
    true
});
 
// Second call returns immediately without computation
let cached_value = quick_bool.get_or_set(|| panic!("This won't execute"));
assert_eq!(value, cached_value);
Source

pub fn get(&self) -> Option<bool>

Gets the current value without computing it.

Returns None if the value is unset, Some(true) if true, or Some(false) if false.

§Example
use quick_bool::QuickBool;
 
let quick_bool = QuickBool::new();
assert_eq!(quick_bool.get(), None);
 
quick_bool.get_or_set(|| true);
assert_eq!(quick_bool.get(), Some(true));
Source

pub fn is_set(&self) -> bool

Checks if the value has been set.

Returns true if the value is either true or false, false if it’s still unset.

§Example
use quick_bool::QuickBool;
 
let quick_bool = QuickBool::new();
assert!(!quick_bool.is_set());
 
quick_bool.get_or_set(|| true);
assert!(quick_bool.is_set());
Source

pub fn reset(&self)

Resets the value back to unset state.

This allows the value to be recomputed on the next call to get_or_set.

§Example
use quick_bool::QuickBool;
 
let quick_bool = QuickBool::new();
quick_bool.get_or_set(|| true);
assert!(quick_bool.is_set());
 
quick_bool.reset();
assert!(!quick_bool.is_set());

Trait Implementations§

Source§

impl Clone for QuickBool

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QuickBool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QuickBool

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.