Skip to main content

TokioMonitor

Struct TokioMonitor 

Source
pub struct TokioMonitor<T> { /* private fields */ }
Expand description

Asynchronous monitor built on Tokio synchronization primitives.

TokioMonitor protects one state value with a Tokio mutex and coordinates waiters with a Tokio notification primitive. Notification semantics follow Tokio’s Notify behavior.

Implementations§

Source§

impl<T> TokioMonitor<T>

Source

pub fn new(state: T) -> Self

Creates an asynchronous monitor protecting the supplied state.

§Arguments
  • state - Initial protected state.
§Returns

A Tokio-based monitor.

Source

pub async fn async_read<R, F>(&self, f: F) -> R
where F: FnOnce(&T) -> R,

Acquires the monitor and reads the protected state.

§Arguments
  • f - Closure that receives an immutable reference to the state.
§Returns

The value returned by the closure.

Source

pub async fn async_write<R, F>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Acquires the monitor and mutates the protected state.

This does not notify waiters automatically.

§Arguments
  • f - Closure that receives a mutable reference to the state.
§Returns

The value returned by the closure.

Source

pub async fn async_write_notify_one<R, F>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Mutates the protected state and wakes one waiter.

§Arguments
  • f - Closure that receives a mutable reference to the state.
§Returns

The value returned by the closure.

Source

pub async fn async_write_notify_all<R, F>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R,

Mutates the protected state and wakes all waiters.

§Arguments
  • f - Closure that receives a mutable reference to the state.
§Returns

The value returned by the closure.

Source

pub fn notify_one(&self)

Wakes one async waiter.

Source

pub fn notify_all(&self)

Wakes all async waiters.

Trait Implementations§

Source§

impl<T> AsRef<TokioMonitor<T>> for ArcTokioMonitor<T>

Source§

fn as_ref(&self) -> &TokioMonitor<T>

Returns a reference to the wrapped Tokio monitor.

Source§

impl<T: Send> AsyncConditionWaiter for TokioMonitor<T>

Source§

fn async_wait_until<'a, R, P, F>( &'a self, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, R>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits until the predicate becomes true.

Source§

fn async_wait_while<'a, R, P, F>( &'a self, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, R>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits while the predicate remains true.

Source§

type State = T

State protected by the monitor.
Source§

impl<T: Send> AsyncNotificationWaiter for TokioMonitor<T>

Source§

fn async_wait<'a>(&'a self) -> AsyncMonitorFuture<'a, ()>

Returns a future that resolves after a Tokio notification.

Source§

impl<T: Send> AsyncTimeoutConditionWaiter for TokioMonitor<T>

Source§

fn async_wait_until_for<'a, R, P, F>( &'a self, timeout: Duration, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits until the predicate becomes true or times out.

Source§

fn async_wait_while_for<'a, R, P, F>( &'a self, timeout: Duration, predicate: P, action: F, ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
where R: Send + 'a, P: FnMut(&Self::State) -> bool + Send + 'a, F: FnOnce(&mut Self::State) -> R + Send + 'a,

Returns a future that waits while the predicate remains true or times out.

Source§

impl<T: Send> AsyncTimeoutNotificationWaiter for TokioMonitor<T>

Source§

fn async_wait_for<'a>( &'a self, timeout: Duration, ) -> AsyncMonitorFuture<'a, WaitTimeoutStatus>

Returns a future that resolves after notification or timeout.

Source§

impl<T: Default> Default for TokioMonitor<T>

Source§

fn default() -> Self

Creates a Tokio monitor containing T::default().

Source§

impl<T> From<T> for TokioMonitor<T>

Source§

fn from(value: T) -> Self

Creates a Tokio monitor from an initial state value.

Source§

impl<T> Notifier for TokioMonitor<T>

Source§

fn notify_one(&self)

Wakes one async waiter.

Source§

fn notify_all(&self)

Wakes all async waiters.

Auto Trait Implementations§

§

impl<T> !Freeze for TokioMonitor<T>

§

impl<T> !RefUnwindSafe for TokioMonitor<T>

§

impl<T> Send for TokioMonitor<T>
where T: Send,

§

impl<T> Sync for TokioMonitor<T>
where T: Send,

§

impl<T> Unpin for TokioMonitor<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for TokioMonitor<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for TokioMonitor<T>
where T: UnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> AsyncMonitor for T