Struct SubscriptionRef

Source
pub struct SubscriptionRef<K, V>
where K: Clone + Debug + Eq + Hash + Ord, V: Clone + Debug,
{ /* private fields */ }
Expand description

A transparent wrapper for the underlying subscription in the map which manages the subscription count and removes the observable if no one holds a subscription to it.

Methods from Deref<Target = Observable<V>>§

Source

pub fn publish(&mut self, value: T)

Store provided value and notify forks.

Source

pub fn modify<M>(&mut self, modify: M)
where M: FnOnce(&mut T),

Modify the underlying value and notify forks.

Source

pub fn modify_conditional<C, M>(&mut self, condition: C, modify: M) -> bool
where C: FnOnce(&T) -> bool, M: FnOnce(&mut T),

If the condition is met, modify the underlying value and notify forks.

Returns true if the modification was executed.

let mut observable = Observable::new(0);
let mut fork = observable.clone();

observable.modify_conditional(|i| *i == 0, |i| *i = 1); // modify
assert_eq!(fork.next().await, 1);

observable.modify_conditional(|i| *i == 0, |i| *i = 2); // doesnt modify
fork.next().await; // runs forever
Source

pub fn clone_and_reset(&self) -> Observable<T>

Same as clone, but the reset causes the fork to instantly have a change available with the current state.

let mut observable = Observable::new(0);
let mut fork = observable.clone_and_reset();

assert_eq!(fork.next().await, 0);
Source

pub fn latest(&self) -> T

Creates a clone of latest version of the observable value, without consuming the change!

let mut observable = Observable::new(0);
let mut fork = observable.clone_and_reset();

observable.publish(1);

assert_eq!(fork.latest(), 1);
assert_eq!(fork.next().await, 1);
Source

pub async fn next(&mut self) -> T

Wait until a new version of the observable was published and return a clone of the new version.

let (mut a, mut b) = Observable::new(0).split();

a.publish(1);
assert_eq!(b.next().await, 1);

a.publish(2);
assert_eq!(b.next().await, 2);

b.next().await; // runs forever!
Source

pub fn synchronize(&mut self) -> T

Skip any potential updates and retrieve the latest version of the observed value.

let (mut a, mut b) = Observable::new(0).split();

a.publish(1);
a.publish(2);
a.publish(3);

assert_eq!(b.synchronize(), 3);

b.next().await; // runs forever!
Source

pub fn publish_if_changed(&mut self, value: T) -> bool

Publish a change if the new value differs from the current one.

Returns true if a change was made.

Trait Implementations§

Source§

impl<K, V> Debug for SubscriptionRef<K, V>
where K: Clone + Debug + Eq + Hash + Ord + Debug, V: Clone + Debug + Debug,

Source§

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

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

impl<K, V> Deref for SubscriptionRef<K, V>
where K: Clone + Debug + Eq + Hash + Ord, V: Clone + Debug,

Source§

type Target = Observable<V>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<K, V> DerefMut for SubscriptionRef<K, V>
where K: Clone + Debug + Eq + Hash + Ord, V: Clone + Debug,

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<K, V> Drop for SubscriptionRef<K, V>
where K: Clone + Debug + Eq + Hash + Ord, V: Clone + Debug,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for SubscriptionRef<K, V>
where K: Freeze,

§

impl<K, V> !RefUnwindSafe for SubscriptionRef<K, V>

§

impl<K, V> Send for SubscriptionRef<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for SubscriptionRef<K, V>
where K: Sync + Send, V: Send,

§

impl<K, V> Unpin for SubscriptionRef<K, V>
where K: Unpin,

§

impl<K, V> !UnwindSafe for SubscriptionRef<K, V>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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