Trait CanMultiplexSubscription

Source
pub trait CanMultiplexSubscription {
    // Required method
    fn multiplex_subscription<T, U>(
        &self,
        subscription: impl Subscription<Item = T>,
        map_item: impl Fn(T) -> U + Async,
    ) -> Arc<dyn Subscription<Item = U>>
       where T: Async + Clone,
             U: Async + Clone;
}
Expand description

Multiplex the incoming Stream provided by an underlying Subscription into multiple outgoing Streams through a background task. This is an auto trait implemented by all runtime contexts that implement HasSpawner, HasMutex, CanCreateChannels, CanUseChannels, and CanStreamReceiver.

This can be used to improve the efficiency of naive subscriptions created from CanCreateClosureSubscription. For example, one can first create a subscription closure that establishes new network connection each time subscribe is called. The subscription closure is then passed to multiplex_subscription, which would return a wrapped subscription which would only create one network connection at a time.

The multiplexed subscription also attempts to recover by calling the subscribe method of the underlying subsciption again, if a given Stream terminates. This would allow for auto recovery from underlying errors such as network disconnection. The multiplexed subscription would only terminate if the underlying subscribe returns None.

The streams returned from the subscribe of the multiplexed subscription will automatically resume streaming from a new underlying stream, if the original underlying stream is terminated. However, since a consumer cannot know if a Subscription implementation is multiplexed or not, it should always retry calling subscribe in case a Stream ends.

Required Methods§

Source

fn multiplex_subscription<T, U>( &self, subscription: impl Subscription<Item = T>, map_item: impl Fn(T) -> U + Async, ) -> Arc<dyn Subscription<Item = U>>
where T: Async + Clone, U: Async + Clone,

Multiplex a given subscription, with a mapper function that maps the item coming from the underlying subscription from T to U. Returns a new multiplexed subscription that shares the same underlying Stream.

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§