pub struct SubscribableMutex<T: ?Sized> { /* private fields */ }
Expand description

A mutex that can register subscribers to be notified. This works in the same way as Mutex, but has some additional functions:

Self::subscribe will return a [Receiver] which can be used to be notified of changes.

Self::notify_change_subscribers will notify all Receiver that are registered with the subscribe function.

Implementations§

Source§

impl<T> SubscribableMutex<T>

Source

pub fn new(t: T) -> Self

Create a new mutex with the value T

Source

pub async fn lock(&self) -> MutexGuard<'_, T>

👎Deprecated: Consider using a different function instead

Acquires the mutex.

Returns a guard that releases the mutex when dropped.

Direct usage of this function may result in unintentional deadlocks. Consider using one of the following functions instead:

  • modify to edit the inner value.
  • set to set the inner value.
  • compare_and_set compare the inner value with a given value, and if they match, update the value to the second value.
  • copied and cloned gets a copy or clone of the inner value
Source

pub async fn notify_change_subscribers(&self)

Notify the subscribers that a change has occured. Subscribers can be registered by calling Self::subscribe.

Subscribers cannot be removed as they have no unique identifying information. Instead this function will simply remove all senders that fail to deliver their message.

Source

pub async fn subscribe(&self) -> UnboundedReceiver<()>

Create a [Receiver] that will be notified every time a thread calls Self::notify_change_subscribers

Source

pub async fn modify<F>(&self, cb: F)
where F: FnOnce(&mut T),

Modify the internal value, then notify all subscribers that the value is updated.

Source

pub async fn set(&self, val: T)

Set the new inner value, discarding the old ones. This will also notify all subscribers.

Source

pub async fn wait_until<F>(&self, f: F)
where F: FnMut(&T) -> bool,

Wait until condition returns true. Will block until then.

Source

pub fn wait_until_with_trigger<'a, F>( &'a self, f: F, ) -> FuturesOrdered<impl Future<Output = ()> + 'a>
where F: FnMut(&T) -> bool + 'a,

Wait until f returns true. Turns a stream with two ordered events. The first event indicates that the stream is now listening for the state change, and the second event indicates that f has become true

Source

pub fn wait_timeout_until_with_trigger<'a, F>( &'a self, timeout: Duration, f: F, ) -> Timeout<FuturesOrdered<impl Future<Output = ()> + 'a>>
where F: FnMut(&T) -> bool + 'a,

Same functionality as Self::wait_until_with_trigger, except with timeout timeout on both events in stream

Source

pub async fn wait_timeout_until<F>(&self, timeout: Duration, f: F) -> Result<()>
where F: FnMut(&T) -> bool,

Wait timeout until f returns true. Will return Ok(()) if the function returned true before the time elapsed. Notifies caller over ready_chan when has begun to listen for changes to the internal state (locked within the Mutex)

§Errors

Returns an error when this function timed out.

Source§

impl<T: PartialEq> SubscribableMutex<T>

Source

pub async fn compare_and_set(&self, compare: T, set: T)

Compare the value of this mutex. If the value is equal to compare, it will be set to set and all subscribers will be notified

Source§

impl<T: Clone> SubscribableMutex<T>

Source

pub async fn cloned(&self) -> T

Return a clone of the current value of T

Source§

impl<T: Copy> SubscribableMutex<T>

Source

pub async fn copied(&self) -> T

Return a copy of the current value of T

Trait Implementations§

Source§

impl<T: Debug> Debug for SubscribableMutex<T>

Source§

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

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

impl<T: Default + ?Sized> Default for SubscribableMutex<T>

Source§

fn default() -> SubscribableMutex<T>

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

Auto Trait Implementations§

§

impl<T> !Freeze for SubscribableMutex<T>

§

impl<T> !RefUnwindSafe for SubscribableMutex<T>

§

impl<T> Send for SubscribableMutex<T>
where T: Send + ?Sized,

§

impl<T> Sync for SubscribableMutex<T>
where T: Send + ?Sized,

§

impl<T> Unpin for SubscribableMutex<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for SubscribableMutex<T>
where T: UnwindSafe + ?Sized,

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more