Struct Stream

Source
pub struct Stream<'a, Sig> { /* private fields */ }
Expand description

A stream of signals.

A stream represents a composable signal producer. When you decide to send a signal down a stream, any other streams composed with that first stream will also receive the signal. This enables to construct more interesting and complex streams by composing them via, for instance, Stream::map, Stream::filter_map, Stream::filter, Stream::fold, Stream::merge, Stream::zip, etc.

Implementations§

Source§

impl<'a, Sig> Stream<'a, Sig>
where Sig: 'a + Send + Sync,

Source

pub fn new() -> Self

Create a new stream.

Source

pub fn observe<F>(&self, subscriber: F) -> u128
where F: 'a + FnMut(&Sig) + Send,

Observe a stream signal output flowing out of the stream.

Do not abuse this function, as its primary use is to build other combinators.

Source

pub fn observe_with_handle<F>(&self, subscriber: F, handle_id: u128)
where F: 'a + FnMut(&Sig) + Send,

Observe a stream signal output flowing out of the stream.

Do not abuse this function, as its primary use is to build other combinators.

Source

pub fn remove(&self, handle_id: u128)

Removes the subscriber with the given handle_id.

Source

pub fn clear(&self)

Removes all subscribers without dropping the stream.

Source

pub fn send(&self, signal: &Sig)

Send a signal down the stream.

Source

pub fn map<F, OutSig>(&self, f: F) -> Stream<'a, OutSig>
where F: 'a + Fn(&Sig) -> OutSig + Send, OutSig: 'a + Send + Sync,

Map any signals flowing out of a stream.

Please note that this function is total: you cannot ignore signals. Even if you map uninteresting signals to None, you’ll still compose signals for those. If you’re interested in filtering signals while mapping, have a look at the Stream::filter_map function.

Source

pub fn filter_map<F, OutSig>(&self, f: F) -> Stream<'a, OutSig>
where F: 'a + Fn(&Sig) -> Option<OutSig> + Send, OutSig: 'a + Send + Sync,

Filter and map signals flowing out of a stream.

If you’re not interested in a specific signal, you can emit None: no signal will be sent.

Source

pub fn filter<F>(&self, pred: F) -> Self
where F: 'a + Fn(&Sig) -> bool + Send,

Filter the signals flowing out of a stream with a predicate.

Source

pub fn fold<F, A>(&self, value: A, f: F) -> Stream<'a, A>
where F: 'a + Fn(A, &Sig) -> A + Send, A: 'a + Send + Sync,

Fold all signals flowing out of a stream into a stream of values.

Source

pub fn merge(&self, rhs: &Self) -> Self

Merge two streams into one.

Merging streams enables you to perform later useful compositions, such as folding the merged results.

Source

pub fn merge_with<F, SigRHS>( &self, rhs: &Stream<'a, SigRHS>, adapter: F, ) -> Self
where F: 'a + Fn(&SigRHS) -> Sig + Send, SigRHS: 'a + Send + Sync,

Merge two streams into one with incompatible types.

This method performs the same logical operation as:

use reactive_graph_reactive_model_impl::Stream;

let stream_a = Stream::new();
let stream_b = Stream::new();
let merged = stream_a.merge(&stream_b.map(String::len));

However, because it does it in a more optimal way, you are advised to use this combinator instead.

Source

pub fn zip<SigRHS>( &self, rhs: &Stream<'a, SigRHS>, ) -> Stream<'a, Either<Sig, SigRHS>>
where Sig: Clone + Send, SigRHS: 'a + Clone + Send + Sync,

Zip two streams with each other.

The resulting stream will output Either from one or the other stream.

Source

pub fn entangled<F, G, GSig>(f: F, g: G) -> (Self, Stream<'a, GSig>)
where F: 'a + Fn(&Sig) -> Option<GSig> + Send, G: 'a + Fn(&GSig) -> Option<Sig> + Send, GSig: 'a + Send + Sync,

Create a pair of entangled streams.

If any of the streams sends a signal, the other one receives it. However, be careful: since the signals are defined in terms of each other, it’s quite easy to cause infinite loops if you don’t have a well-defined bottom to your recursion. This is why you’re expected to return Option<_> signals.

Source

pub fn recv(&self) -> Receiver<Sig>
where Sig: Clone,

Sink a stream.

This method allows to receive the signal and extract it out of the stream via a channel.

Source§

impl<'a, SigA, SigB> Stream<'a, Either<SigA, SigB>>
where SigA: 'static + Send + Sync, SigB: 'static + Send + Sync,

Source

pub fn unzip(&self) -> (Stream<'a, SigA>, Stream<'a, SigB>)

Split a stream of zipped values into two streams.

If the Either::Left part of the stream emits a signal, it is sent to the first stream. If the Either::Right part of the tream emits, it is sent to the second stream.

Trait Implementations§

Source§

impl<'a, Sig> Default for Stream<'a, Sig>
where Sig: 'a + Send + Sync,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<Sig> Send for Stream<'_, Sig>

TODO: This solves many problems, but is it really OK?

Source§

impl<Sig> Sync for Stream<'_, Sig>

Auto Trait Implementations§

§

impl<'a, Sig> !Freeze for Stream<'a, Sig>

§

impl<'a, Sig> RefUnwindSafe for Stream<'a, Sig>

§

impl<'a, Sig> Unpin for Stream<'a, Sig>

§

impl<'a, Sig> UnwindSafe for Stream<'a, Sig>

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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.