Watcher

Trait Watcher 

pub trait Watcher: Clone {
    type Value: Clone + Eq;

    // Required methods
    fn update(&mut self) -> bool;
    fn peek(&self) -> &Self::Value;
    fn is_connected(&self) -> bool;
    fn poll_updated(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), Disconnected>>;

    // Provided methods
    fn get(&mut self) -> Self::Value { ... }
    fn updated(&mut self) -> NextFut<'_, Self> { ... }
    fn initialized<T, W>(&mut self) -> InitializedFut<'_, T, W, Self>
       where W: Nullable<T> + Clone,
             Self: Watcher<Value = W> { ... }
    fn stream(self) -> Stream<Self>
       where Self: Unpin { ... }
    fn stream_updates_only(self) -> Stream<Self>
       where Self: Unpin { ... }
    fn map<T>(
        self,
        map: impl Fn(Self::Value) -> T + Send + Sync + 'static,
    ) -> Map<Self, T>
       where T: Clone + Eq { ... }
    fn or<W>(self, other: W) -> Tuple<Self, W>
       where W: Watcher { ... }
}
Expand description

A handle to a value that’s represented by one or more underlying [Watchable]s.

A Watcher can get the current value, and will be notified when the value changes. Only the most recent value is accessible, and if the threads with the underlying [Watchable]s change the value faster than the threads with the Watcher can keep up with, then it’ll miss in-between values. When the thread changing the [Watchable] pauses updating, the Watcher will always end up reporting the most recent state eventually.

Watchers can be modified via Watcher::map to observe a value derived from the original value via a function.

Watchers can be combined via Watcher::or to allow observing multiple values at once and getting an update in case any of the values updates.

One of the underlying [Watchable]s might already be dropped. In that case, the watcher will be “disconnected” and return Err(Disconnected) on some function calls or, when turned into a stream, that stream will end. This property can also be checked with Watcher::is_connected.

Required Associated Types§

type Value: Clone + Eq

The type of value that can change.

We require Clone, because we need to be able to make the values have a lifetime that’s detached from the original [Watchable]’s lifetime.

We require Eq, to be able to check whether the value actually changed or not, so we can notify or not notify accordingly.

Required Methods§

fn update(&mut self) -> bool

Updates the watcher to the latest value and returns whether it changed.

Watchers keep track of the “latest known” value they fetched. This function updates that internal value by looking up the latest value at the [Watchable](s) that this watcher is linked to.

fn peek(&self) -> &Self::Value

Returns a reference to the value currently stored in the watcher.

Watchers keep track of the “latest known” value they fetched. Calling this won’t update the latest value, unlike Watcher::get or Watcher::update.

This can be useful if you want to avoid copying out the internal value frequently like what Watcher::get will end up doing.

fn is_connected(&self) -> bool

Whether this watcher is still connected to all of its underlying [Watchable]s.

Returns false when any of the underlying watchables has been dropped.

fn poll_updated( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), Disconnected>>

Polls for the next value, or returns [Disconnected] if one of the underlying [Watchable]s has been dropped.

Provided Methods§

fn get(&mut self) -> Self::Value

Updates the watcher to the latest value and returns that value.

If any of the underlying [Watchable] values have been dropped, then this might return an outdated value for that watchable, specifically, the latest value that was fetched for that watchable, as opposed to the latest value that was set on the watchable before it was dropped.

The default implementation for this is simply

ⓘ
fn get(&mut self) -> Self::Value {
    self.update();
    self.peek().clone()
}

fn updated(&mut self) -> NextFut<'_, Self>

Returns a future completing with Ok(value) once a new value is set, or with Err(Disconnected) if the connected [Watchable] was dropped.

§Cancel Safety

The returned future is cancel-safe.

fn initialized<T, W>(&mut self) -> InitializedFut<'_, T, W, Self>
where W: Nullable<T> + Clone, Self: Watcher<Value = W>,

Returns a future completing once the value is set to Some value.

If the current value is Some value, this future will resolve immediately.

This is a utility for the common case of storing an Option inside a [Watchable].

§Cancel Safety

The returned future is cancel-safe.

fn stream(self) -> Stream<Self>
where Self: Unpin,

Returns a stream which will yield the most recent values as items.

The first item of the stream is the current value, so that this stream can be easily used to operate on the most recent value.

Note however, that only the last item is stored. If the stream is not polled when an item is available it can be replaced with another item by the time it is polled.

This stream ends once the original [Watchable] has been dropped.

§Cancel Safety

The returned stream is cancel-safe.

fn stream_updates_only(self) -> Stream<Self>
where Self: Unpin,

Returns a stream which will yield the most recent values as items, starting from the next unobserved future value.

This means this stream will only yield values when the watched value changes, the value stored at the time the stream is created is not yielded.

Note however, that only the last item is stored. If the stream is not polled when an item is available it can be replaced with another item by the time it is polled.

This stream ends once the original [Watchable] has been dropped.

§Cancel Safety

The returned stream is cancel-safe.

fn map<T>( self, map: impl Fn(Self::Value) -> T + Send + Sync + 'static, ) -> Map<Self, T>
where T: Clone + Eq,

Maps this watcher with a function that transforms the observed values.

The returned watcher will only register updates, when the mapped value observably changes.

fn or<W>(self, other: W) -> Tuple<Self, W>
where W: Watcher,

Returns a watcher that updates every time this or the other watcher updates, and yields both watcher’s items together when that happens.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

§

impl<S, T> Watcher for Tuple<S, T>
where S: Watcher, T: Watcher,

§

type Value = (<S as Watcher>::Value, <T as Watcher>::Value)

§

impl<S, T, U> Watcher for Triple<S, T, U>
where S: Watcher, T: Watcher, U: Watcher,

§

type Value = (<S as Watcher>::Value, <T as Watcher>::Value, <U as Watcher>::Value)

§

impl<T> Watcher for Direct<T>
where T: Clone + Eq,

§

type Value = T

§

impl<T, W> Watcher for Join<T, W>
where T: Clone + Eq, W: Watcher<Value = T>,

§

type Value = Vec<T>

§

impl<W, T> Watcher for Map<W, T>
where W: Watcher, T: Clone + Eq,

§

type Value = T