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>
impl<T> SubscribableMutex<T>
Sourcepub async fn lock(&self) -> MutexGuard<'_, T>
👎Deprecated: Consider using a different function instead
pub async fn lock(&self) -> MutexGuard<'_, T>
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
andcloned
gets a copy or clone of the inner value
Sourcepub async fn notify_change_subscribers(&self)
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.
Sourcepub async fn subscribe(&self) -> UnboundedReceiver<()>
pub async fn subscribe(&self) -> UnboundedReceiver<()>
Create a [Receiver
] that will be notified every time a thread calls Self::notify_change_subscribers
Sourcepub async fn modify<F>(&self, cb: F)
pub async fn modify<F>(&self, cb: F)
Modify the internal value, then notify all subscribers that the value is updated.
Sourcepub async fn set(&self, val: T)
pub async fn set(&self, val: T)
Set the new inner value, discarding the old ones. This will also notify all subscribers.
Sourcepub async fn wait_until<F>(&self, f: F)
pub async fn wait_until<F>(&self, f: F)
Wait until condition
returns true
. Will block until then.
Sourcepub fn wait_until_with_trigger<'a, F>(
&'a self,
f: F,
) -> FuturesOrdered<impl Future<Output = ()> + 'a>
pub fn wait_until_with_trigger<'a, F>( &'a self, f: F, ) -> FuturesOrdered<impl Future<Output = ()> + '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
Sourcepub fn wait_timeout_until_with_trigger<'a, F>(
&'a self,
timeout: Duration,
f: F,
) -> Timeout<FuturesOrdered<impl Future<Output = ()> + 'a>>
pub fn wait_timeout_until_with_trigger<'a, F>( &'a self, timeout: Duration, f: F, ) -> Timeout<FuturesOrdered<impl Future<Output = ()> + 'a>>
Same functionality as Self::wait_until_with_trigger
, except
with timeout timeout
on both events in stream
Sourcepub async fn wait_timeout_until<F>(&self, timeout: Duration, f: F) -> Result<()>
pub async fn wait_timeout_until<F>(&self, timeout: Duration, f: F) -> Result<()>
Source§impl<T: PartialEq> SubscribableMutex<T>
impl<T: PartialEq> SubscribableMutex<T>
Sourcepub async fn compare_and_set(&self, compare: T, set: T)
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