[][src]Crate demux_stream

(De)multiplexing for Rust's streams.

Demultiplexing

Demultiplexing a stream S is semantically equivalent to matching each item with a type T and pushing it into Stream<T>, e.g.: Stream<Coprod!(A, B, ...)>Hlist![Stream<A>, Stream<B>, ...].

Example

// Imports are omitted...

type MyType = Coprod!(bool, i32, String);

let coproducts = stream::once(async { MyType::inject(123) });

let (mut bool_s, mut i32_s, mut string_s) = <_>::demux(coproducts).into();

assert_eq!(i32_s.next().await, Some(123));
assert_eq!(bool_s.next().await, None);
assert_eq!(i32_s.next().await, None);
assert_eq!(string_s.next().await, None);

Multiplexing

Multiplexing is a dual of demultiplexing. Given a heterogenous list of streams H, redirect each stream's variant with a type T into the resulting stream, e.g.: Hlist![Stream<A>, Stream<B>, ...]Stream<Coprod!(A, B, ...)>.

Example

// Imports are omitted...

type MyType = Coprod!(bool, i32, String, ());

let streams = hlist![
    stream::once(async { true }),
    stream::once(async { 123i32 }),
    stream::once(async { "Hello".to_owned() })
];

let mut multiplexed = streams.mux();

assert_eq!(multiplexed.next().await, Some(MyType::inject(true)));
assert_eq!(multiplexed.next().await, Some(MyType::inject(123i32)));
assert_eq!(
    multiplexed.next().await,
    Some(MyType::inject("Hello".to_owned()))
);
assert_eq!(multiplexed.next().await, None);

Traits

Demux

A type T, for which Stream<T> can be demultiplexed.

Mux

A type T, which can be multiplexed.