pub struct Latch { /* private fields */ }latch only.Expand description
A one-shot countdown that can wake any number of waiting tasks.
See the module level documentation for more.
Implementations§
Source§impl Latch
impl Latch
Sourcepub fn new(count: u32) -> Self
pub fn new(count: u32) -> Self
Creates a new latch initialized with the given count.
§Examples
use asyncband::latch::Latch;
let latch = Latch::new(3);Sourcepub fn count(&self) -> u32
pub fn count(&self) -> u32
Returns the current count.
§Examples
use asyncband::latch::Latch;
let latch = Latch::new(5);
assert_eq!(latch.count(), 5);Sourcepub fn count_down(&self)
pub fn count_down(&self)
Decrements the latch count by one, waking up all pending tasks if the counter reaches zero.
If the current count is zero, this method has no effect.
§Examples
use asyncband::latch::Latch;
let latch = Latch::new(2);
latch.count_down();
assert_eq!(latch.count(), 1);Sourcepub fn arrive(&self, n: u32)
pub fn arrive(&self, n: u32)
Decrements the latch count by n, waking up all waiting tasks if the counter reaches zero.
The count saturates at zero. Passing zero or calling this after the latch has completed has no effect.
§Examples
use asyncband::latch::Latch;
let latch = Latch::new(5);
latch.arrive(3);
assert_eq!(latch.count(), 2);
latch.arrive(10);
assert_eq!(latch.count(), 0);Sourcepub fn try_wait(&self) -> Result<(), u32>
pub fn try_wait(&self) -> Result<(), u32>
Attempts to wait for the latch count to reach zero without blocking.
Returns Ok(()) if the latch is complete, or Err(count) with the current nonzero count.
§Examples
use asyncband::latch::Latch;
let latch = Latch::new(2);
assert_eq!(latch.try_wait(), Err(2));
latch.count_down();
assert_eq!(latch.try_wait(), Err(1));
latch.count_down();
assert_eq!(latch.try_wait(), Ok(()));Sourcepub async fn wait(&self)
pub async fn wait(&self)
Returns a future that will complete when the latch count reaches zero.
This method is cancel safe. Dropping a pending wait does not change the countdown or affect other waiters.
§Examples
use asyncband::latch::Latch;
let latch = Latch::new(1);
latch.count_down();
latch.wait().await;Sourcepub async fn wait_owned(self: Arc<Self>)
pub async fn wait_owned(self: Arc<Self>)
Returns a future that will complete when the latch count reaches zero.
The latch must be wrapped in an Arc to call this method. The future owns that Arc, so
it can be moved into a spawned task. Like wait, this method is cancel safe.
§Examples
use std::sync::Arc;
use asyncband::latch::Latch;
let latch = Arc::new(Latch::new(1));
let waiter = tokio::spawn(latch.clone().wait_owned());
latch.count_down();
waiter.await.unwrap();