pub struct Publisher<T> { /* private fields */ }Expand description
Read/write a shared value and emit change notifications on write.
Publishers are not aware of how many Subscribers are connected
who are observing changes.
All methods borrow self immutably to allow sharing a Publisher
safely between threads. Only a single instance is supported, i.e.
Clone is probably not implemented.
If more than one instance is needed in different contexts with independent
lifetimes then the single instance could be shared by wrapping it into
Rc or Arc.
Implementations§
source§impl<T> Publisher<T>
impl<T> Publisher<T>
sourcepub fn has_subscribers(&self) -> bool
pub fn has_subscribers(&self) -> bool
Check if the publisher has subscribers.
Returns true if at least one subscriber is connected
or false if the publish is orphaned.
sourcepub fn subscribe(&self) -> Subscriber<T>
pub fn subscribe(&self) -> Subscriber<T>
Create a new subscription that is connected to this publisher.
sourcepub fn write(&self, new_value: impl Into<T>)
pub fn write(&self, new_value: impl Into<T>)
Overwrite the current value with a new value and emit a change notification.
The change notification is emitted unconditionally, i.e. independent of both the current and the new value.
sourcepub fn replace(&self, new_value: impl Into<T>) -> T
pub fn replace(&self, new_value: impl Into<T>) -> T
Replace and return the current value with a new value and emit a change notification.
The change notification is emitted unconditionally, i.e. independent of both the current and the new value.
sourcepub fn modify<M>(&self, modify: M) -> boolwhere
M: FnOnce(&mut T) -> bool,
pub fn modify<M>(&self, modify: M) -> boolwhere M: FnOnce(&mut T) -> bool,
Modify the current value in-place and conditionally emit a change notification.
The mutable borrow of the current value is protected by
a write lock during the execution scope of the modify
closure.
The result of the invoked modify closure controls if
a change notification is sent or not. This result is
finally returned.