Struct awaitable_bool::AwaitableBool
source · pub struct AwaitableBool { /* private fields */ }Expand description
A bool whose value changes can be waited on.
Internally, it uses AtomicBool
and tokio::sync::Notify.
Because of that, most methods only need a shared reference (& as opposed to &mut) to self,
so sharing between tasks or threads should be cheap and easy.
This struct doesn’t implement Clone,
so place it in an Arc
(no lock protecting it needed) if cloning is needed.
Implementations§
source§impl AwaitableBool
impl AwaitableBool
sourcepub fn new<IntoAtomicBool: Into<AtomicBool>>(value: IntoAtomicBool) -> Self
pub fn new<IntoAtomicBool: Into<AtomicBool>>(value: IntoAtomicBool) -> Self
Creates a new AwaitableBool.
Examples
use awaitable_bool::AwaitableBool;
let awaitable_true = AwaitableBool::new(true);
let awaitable_false = AwaitableBool::new(false);sourcepub fn toggle(&self)
pub fn toggle(&self)
Set the AwaitableBool to the inverse of its current value (i.e. false if true or true if false)
(with AcqRel ordering).
This wakes all tasks waiting for [wait].
It also wakes those waiting for [wait_true] if the value was just changed from false to true,
or those waiting for [wait_false] if the value was just changed from true to false.
sourcepub fn is_true(&self) -> bool
pub fn is_true(&self) -> bool
Check if the AwaitableBool’s value is currently true
(with Acquire ordering).
sourcepub fn is_false(&self) -> bool
pub fn is_false(&self) -> bool
Check if the AwaitableBool’s value is currently false
(with Acquire ordering).
sourcepub async fn wait(&self)
pub async fn wait(&self)
Wait for this AwaitableBool’s value to change.
Use [load] after to know what it changed to.
sourcepub async fn wait_true(&self)
pub async fn wait_true(&self)
Wait for this AwaitableBool’s value to become true.
sourcepub async fn wait_false(&self)
pub async fn wait_false(&self)
Wait for this AwaitableBool’s value to become false.
sourcepub fn into_inner(self) -> AtomicBool
pub fn into_inner(self) -> AtomicBool
Consume this AwaitableBool to get the contained AtomicBool.
AtomicBool also has
an into_inner method to get its contained bool.