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 yetTrue
: The value is trueFalse
: 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
impl QuickBool
Sourcepub fn get_or_set<F>(&self, f: F) -> bool
pub fn get_or_set<F>(&self, f: F) -> 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);
Sourcepub fn get(&self) -> Option<bool>
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));
Sourcepub fn is_set(&self) -> bool
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());
Sourcepub fn reset(&self)
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());