Skip to main content

Latch

Struct Latch 

Source
pub struct Latch { /* private fields */ }
Available on crate feature 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

Source

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);
Source

pub fn count(&self) -> u32

Returns the current count.

§Examples
use asyncband::latch::Latch;

let latch = Latch::new(5);
assert_eq!(latch.count(), 5);
Source

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);
Source

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);
Source

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(()));
Source

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;
Source

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();

Trait Implementations§

Source§

impl Debug for Latch

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Latch

§

impl RefUnwindSafe for Latch

§

impl Send for Latch

§

impl Sync for Latch

§

impl Unpin for Latch

§

impl UnsafeUnpin for Latch

§

impl UnwindSafe for Latch

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.