Skip to main content

ManualResetEvent

Struct ManualResetEvent 

Source
pub struct ManualResetEvent { /* private fields */ }
Available on crate feature event only.
Expand description

A reusable event that remains set until explicitly reset.

See the module-level documentation for its waiting semantics.

§Synchronization

An unset-to-set transition synchronizes with the waits it releases and with waits first polled while the event remains set. Memory operations sequenced before set are therefore visible after those waits complete.

A set call that finds the event already set does not establish this guarantee. is_set is only a snapshot and cannot replace a wait or support check-then-act.

Implementations§

Source§

impl ManualResetEvent

Source

pub const fn new() -> Self

Creates an unset event.

§Examples
use asyncband::event::ManualResetEvent;

let event = ManualResetEvent::new();
assert!(!event.is_set());
Source

pub const fn with_state(is_set: bool) -> Self

Creates an event with the specified initial state.

§Examples
use asyncband::event::ManualResetEvent;

let ready = ManualResetEvent::with_state(true);
assert!(ready.is_set());
Source

pub fn is_set(&self) -> bool

Returns whether the event is currently set.

This is a snapshot only; it does not reserve or consume the set state.

§Examples
use asyncband::event::ManualResetEvent;

let event = ManualResetEvent::new();
assert!(!event.is_set());

event.set();
assert!(event.is_set());

event.reset();
assert!(!event.is_set());
Source

pub fn set(&self)

Sets the event and releases every currently registered wait.

The event remains set until reset is called. Calling set while it is already set has no effect. No ordering is guaranteed among the released waits.

§Panics

Panics if a registered waker panics. The state transition remains committed, and every remaining registered waker is still notified before the first panic resumes.

§Examples
use asyncband::event::ManualResetEvent;

let event = ManualResetEvent::new();
event.set();

// The event stays ready, so every later wait completes without blocking.
event.wait().await;
event.wait().await;
Source

pub fn reset(&self)

Resets the event so new waits block until another set.

Waiters already committed by a preceding set remain ready. Calling reset while the event is already unset has no effect.

§Examples
use asyncband::event::ManualResetEvent;

let event = ManualResetEvent::with_state(true);
event.wait().await;

// Subsequent waits block again until the next `set`.
event.reset();
assert!(!event.is_set());

event.set();
event.wait().await;
Source

pub async fn wait(&self)

Waits until the event is set.

If the event is already set, the wait completes immediately. Once a set commits a registered wait, a later reset cannot make that wait pending again.

§Cancel safety

Dropping a pending wait unregisters only that call; it does not change the event or affect other waiters. If cancellation races with set, either cancellation unregisters first or set commits the wait first. A waker already detached by set may still run after the wait is dropped.

§Examples
use asyncband::event::ManualResetEvent;

let event = ManualResetEvent::new();
let waiter = async {
    event.wait().await;
    "released"
};
let setter = async { event.set() };

let (released, ()) = tokio::join!(waiter, setter);
assert_eq!(released, "released");
Source

pub async fn wait_owned(self: Arc<Self>)

Waits until the event is set without borrowing it.

The event must be held in an Arc. The returned future owns that Arc, which makes it suitable for spawned tasks. Its waiting and cancellation semantics match wait.

§Examples
use std::sync::Arc;

use asyncband::event::ManualResetEvent;

let event = Arc::new(ManualResetEvent::new());
let waiter = tokio::spawn(event.clone().wait_owned());

event.set();
waiter.await.unwrap();

Trait Implementations§

Source§

impl Debug for ManualResetEvent

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ManualResetEvent

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.