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 necessary per the previous sentence) 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
§Specify an initial value
use awaitable_bool::AwaitableBool;
let initially_true = AwaitableBool::new(true);
let initially_false = AwaitableBool::new(false);§Use an existing AtomicBool to make an AwaitableBool
use std::sync::atomic::AtomicBool;
use awaitable_bool::AwaitableBool;
let atomic_bool = AtomicBool::new(false);
let awaitable_bool = AwaitableBool::new(atomic_bool);Sourcepub fn set_false(&self)
pub fn set_false(&self)
Set the AwaitableBool to false
(with Release ordering if not already false
and Relaxed ordering if it is).
This wakes all tasks waiting for wait_false.
It also wakes those waiting for wait if the value wasn’t already 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 Release 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.
This returns immediately if it’s already true.
Sourcepub async fn wait_false(&self)
pub async fn wait_false(&self)
Wait for this AwaitableBool’s value to become false.
This returns immediately if it’s already false.
Sourcepub const fn into_inner(self) -> AtomicBool
pub const 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.