pub struct WatchChannel { /* private fields */ }Expand description
Observes changes of the latest state value: holds and updates one value, and all observers
(WatchChannel::subscribe) are notified when it changes.
The semantics are “latest value”, not a message queue:
- observers are only guaranteed to eventually see the latest value, not every intermediate change — values
overwritten between two
notifycalls are never re-sent; - unbounded: a new value simply replaces the old one; there is no queue capacity, and no backpressure;
- observers are independent of each other; how fast one consumes does not affect the others.
Driven by the MessageChannel interface:
MessageChannel::notify— update the value to the latest message; all observers get notified;MessageChannel::ask— not supported (watch has no “reply” concept), returnsChannelError::NotSupported;- receive side:
WatchReceiver::recvwaits for a change and takes the latest value.
Implements Clone — every clone is a sender of the same channel, and any clone’s notify
notifies all observers.
§Example
use molo::{MessageChannel, WatchChannel};
let status = WatchChannel::new();
let sub_a = status.subscribe();
let sub_b = status.subscribe();
status.notify("ready").await?;
assert_eq!(sub_a.recv().await?.text(), "ready");
assert_eq!(sub_b.recv().await?.text(), "ready");§Notes
- the initial value is an empty string: before the first
notify,recvkeeps waiting; - when there are no observers (never
subscribed, or all observers have been dropped),notifyreturnsChannelError::NoReceiver— unlikeBroadcastChannel, which succeeds silently even with nobody listening, sosubscribefirst, thennotify; the channel itself is not closed, andnotifyworks again after re-subscribing.
Implementations§
Source§impl WatchChannel
impl WatchChannel
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a watch channel whose initial value is an empty string.
Before the first notify, a subscriber’s recv keeps waiting.
Sourcepub fn subscribe(&self) -> WatchReceiver
pub fn subscribe(&self) -> WatchReceiver
Subscribes to value changes; each observer gets an independent receive end, unaffected by the others.
Trait Implementations§
Source§impl Clone for WatchChannel
impl Clone for WatchChannel
Source§fn clone(&self) -> WatchChannel
fn clone(&self) -> WatchChannel
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for WatchChannel
impl Debug for WatchChannel
Source§impl Default for WatchChannel
impl Default for WatchChannel
Source§impl MessageChannel for WatchChannel
impl MessageChannel for WatchChannel
Source§fn ask<'life0, 'life1, 'async_trait>(
&'life0 self,
_message: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<String, ChannelError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn ask<'life0, 'life1, 'async_trait>(
&'life0 self,
_message: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<String, ChannelError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sends a question and waits for a reply (request-response). Read more
Source§fn notify<'life0, 'life1, 'async_trait>(
&'life0 self,
message: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<(), ChannelError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn notify<'life0, 'life1, 'async_trait>(
&'life0 self,
message: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<(), ChannelError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
A one-way notification that does not wait for a reply (broadcast to all receivers).
Auto Trait Implementations§
impl Freeze for WatchChannel
impl RefUnwindSafe for WatchChannel
impl Send for WatchChannel
impl Sync for WatchChannel
impl Unpin for WatchChannel
impl UnsafeUnpin for WatchChannel
impl UnwindSafe for WatchChannel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more