Struct discro::Publisher

source ·
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>

source

pub fn new(initial_value: T) -> Self

Create a new publisher without subscribers.

source

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.

source

pub fn subscribe(&self) -> Subscriber<T>

Create a new subscription that is connected to this publisher.

source

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.

source

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.

source

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.

source

pub fn read(&self) -> Ref<T>

Obtain a reference to the most recent value.

Outstanding borrows hold a read lock.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Publisher<T>where T: RefUnwindSafe,

§

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

§

impl<T> Sync for Publisher<T>where T: Sync,

§

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

§

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

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.