Struct tokio_stream::wrappers::WatchStream

source ·
pub struct WatchStream<T> { /* private fields */ }
Available on crate feature sync only.
Expand description

A wrapper around tokio::sync::watch::Receiver that implements Stream.

This stream will start by yielding the current value when the WatchStream is polled, regardless of whether it was the initial value or sent afterwards, unless you use WatchStream<T>::from_changes.

§Examples

use tokio_stream::{StreamExt, wrappers::WatchStream};
use tokio::sync::watch;

let (tx, rx) = watch::channel("hello");
let mut rx = WatchStream::new(rx);

assert_eq!(rx.next().await, Some("hello"));

tx.send("goodbye").unwrap();
assert_eq!(rx.next().await, Some("goodbye"));
use tokio_stream::{StreamExt, wrappers::WatchStream};
use tokio::sync::watch;

let (tx, rx) = watch::channel("hello");
let mut rx = WatchStream::new(rx);

// existing rx output with "hello" is ignored here

tx.send("goodbye").unwrap();
assert_eq!(rx.next().await, Some("goodbye"));

Example with WatchStream<T>::from_changes:

use futures::future::FutureExt;
use tokio::sync::watch;
use tokio_stream::{StreamExt, wrappers::WatchStream};

let (tx, rx) = watch::channel("hello");
let mut rx = WatchStream::from_changes(rx);

// no output from rx is available at this point - let's check this:
assert!(rx.next().now_or_never().is_none());

tx.send("goodbye").unwrap();
assert_eq!(rx.next().await, Some("goodbye"));

Implementations§

source§

impl<T: 'static + Clone + Send + Sync> WatchStream<T>

source

pub fn new(rx: Receiver<T>) -> Self

Create a new WatchStream.

source

pub fn from_changes(rx: Receiver<T>) -> Self

Create a new WatchStream that waits for the value to be changed.

Trait Implementations§

source§

impl<T> Debug for WatchStream<T>

source§

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

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

impl<T: 'static + Clone + Send + Sync> From<Receiver<T>> for WatchStream<T>

source§

fn from(recv: Receiver<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Clone + 'static + Send + Sync> Stream for WatchStream<T>

source§

type Item = T

Values yielded by the stream.
source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more
source§

impl<T> Unpin for WatchStream<T>

Auto Trait Implementations§

§

impl<T> Freeze for WatchStream<T>

§

impl<T> !RefUnwindSafe for WatchStream<T>

§

impl<T> Send for WatchStream<T>

§

impl<T> Sync for WatchStream<T>

§

impl<T> !UnwindSafe for WatchStream<T>

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, 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<St> StreamExt for St
where St: Stream + ?Sized,

source§

fn next(&mut self) -> Next<'_, Self>
where Self: Unpin,

Consumes and returns the next value in the stream or None if the stream is finished. Read more
source§

fn try_next<T, E>(&mut self) -> TryNext<'_, Self>
where Self: Stream<Item = Result<T, E>> + Unpin,

Consumes and returns the next item in the stream. If an error is encountered before the next item, the error is returned instead. Read more
source§

fn map<T, F>(self, f: F) -> Map<Self, F>
where F: FnMut(Self::Item) -> T, Self: Sized,

Maps this stream’s items to a different type, returning a new stream of the resulting type. Read more
source§

fn map_while<T, F>(self, f: F) -> MapWhile<Self, F>
where F: FnMut(Self::Item) -> Option<T>, Self: Sized,

Map this stream’s items to a different type for as long as determined by the provided closure. A stream of the target type will be returned, which will yield elements until the closure returns None. Read more
source§

fn then<F, Fut>(self, f: F) -> Then<Self, Fut, F>
where F: FnMut(Self::Item) -> Fut, Fut: Future, Self: Sized,

Maps this stream’s items asynchronously to a different type, returning a new stream of the resulting type. Read more
source§

fn merge<U>(self, other: U) -> Merge<Self, U>
where U: Stream<Item = Self::Item>, Self: Sized,

Combine two streams into one by interleaving the output of both as it is produced. Read more
source§

fn filter<F>(self, f: F) -> Filter<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Filters the values produced by this stream according to the provided predicate. Read more
source§

fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
where F: FnMut(Self::Item) -> Option<T>, Self: Sized,

Filters the values produced by this stream while simultaneously mapping them to a different type according to the provided closure. Read more
source§

fn fuse(self) -> Fuse<Self>
where Self: Sized,

Creates a stream which ends after the first None. Read more
source§

fn take(self, n: usize) -> Take<Self>
where Self: Sized,

Creates a new stream of at most n items of the underlying stream. Read more
source§

fn take_while<F>(self, f: F) -> TakeWhile<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Take elements from this stream while the provided predicate resolves to true. Read more
source§

fn skip(self, n: usize) -> Skip<Self>
where Self: Sized,

Creates a new stream that will skip the n first items of the underlying stream. Read more
source§

fn skip_while<F>(self, f: F) -> SkipWhile<Self, F>
where F: FnMut(&Self::Item) -> bool, Self: Sized,

Skip elements from the underlying stream while the provided predicate resolves to true. Read more
source§

fn all<F>(&mut self, f: F) -> AllFuture<'_, Self, F>
where Self: Unpin, F: FnMut(Self::Item) -> bool,

Tests if every element of the stream matches a predicate. Read more
source§

fn any<F>(&mut self, f: F) -> AnyFuture<'_, Self, F>
where Self: Unpin, F: FnMut(Self::Item) -> bool,

Tests if any element of the stream matches a predicate. Read more
source§

fn chain<U>(self, other: U) -> Chain<Self, U>
where U: Stream<Item = Self::Item>, Self: Sized,

Combine two streams into one by first returning all values from the first stream then all values from the second stream. Read more
source§

fn fold<B, F>(self, init: B, f: F) -> FoldFuture<Self, B, F>
where Self: Sized, F: FnMut(B, Self::Item) -> B,

A combinator that applies a function to every element in a stream producing a single, final value. Read more
source§

fn collect<T>(self) -> Collect<Self, T>
where T: FromStream<Self::Item>, Self: Sized,

Drain stream pushing all emitted values into a collection. Read more
source§

fn timeout(self, duration: Duration) -> Timeout<Self>
where Self: Sized,

Available on crate feature time only.
Applies a per-item timeout to the passed stream. Read more
source§

fn timeout_repeating(self, interval: Interval) -> TimeoutRepeating<Self>
where Self: Sized,

Available on crate feature time only.
Applies a per-item timeout to the passed stream. Read more
source§

fn throttle(self, duration: Duration) -> Throttle<Self>
where Self: Sized,

Available on crate feature time only.
Slows down a stream by enforcing a delay between items. Read more
source§

fn chunks_timeout( self, max_size: usize, duration: Duration, ) -> ChunksTimeout<Self>
where Self: Sized,

Available on crate feature time only.
Batches the items in the given stream using a maximum duration and size for each batch. Read more
source§

fn peekable(self) -> Peekable<Self>
where Self: Sized,

Turns the stream into a peekable stream, whose next element can be peeked at without being consumed. Read more
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<S, T, E> TryStream for S
where S: Stream<Item = Result<T, E>> + ?Sized,

source§

type Ok = T

The type of successful values yielded by this future
source§

type Error = E

The type of failures yielded by this future
source§

fn try_poll_next( self: Pin<&mut S>, cx: &mut Context<'_>, ) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

Poll this TryStream as if it were a Stream. Read more