Skip to main content

Crate combine_latest

Crate combine_latest 

Source
Expand description

Combine two or more async streams, emitting tuples of the latest values from each input.

use combine_latest::CombineLatest;
use futures::executor::block_on;
use futures::stream::{self, StreamExt};

let s1 = stream::iter([1, 2, 3]);
let s2 = stream::iter(["a", "b"]);
let result: Vec<_> = block_on((s1, s2).combine_latest().collect());
assert_eq!(result, vec![(1, "a"), (2, "a"), (2, "b"), (3, "b")]);

The trait-based API (CombineLatest, CombineLatestOpt, MapLatest, MapLatestOpt, WithLatestFrom, MapWithLatestFrom) supports tuples of 2 to 12 streams. Free functions (combine_latest, map_latest, etc.) are available for 2 to 4 streams.

Traits§

CombineLatest
Combines multiple streams into a single stream that yields tuples with the latest value from each input stream. The output stream won’t yield until every input stream has produced at least one item. Implemented for tuples of 2 to 12 streams whose items implement Clone.
CombineLatestOpt
Like CombineLatest, but wraps each position in Option and starts yielding as soon as any input stream produces a value. Positions that haven’t yielded yet are None. Implemented for tuples of 2 to 12 streams whose items implement Clone.
MapLatest
Combines multiple streams and applies a closure to references of the latest values. The output stream won’t yield until every input stream has produced at least one item. Unlike CombineLatest, the item types don’t need to implement Clone. Implemented for tuples of 2 to 12 streams.
MapLatestOpt
Like MapLatest, but the closure receives Option references and the output stream starts yielding as soon as any input stream produces a value. Implemented for tuples of 2 to 12 streams.
MapWithLatestFrom
Like WithLatestFrom, but applies a closure to references of the latest values instead of returning a tuple. The item types don’t need to implement Clone. Implemented for tuples of 2 to 12 streams.
WithLatestFrom
Emits the latest values from all streams as a tuple, but only when the primary (first) stream yields. Secondary streams silently update their cached values without triggering output. No output is produced until every secondary stream has yielded at least once. Implemented for tuples of 2 to 12 streams whose items implement Clone.

Functions§

combine_latest
Combines two streams into a new stream that always contains the latest items from both streams as a tuple. This stream won’t yield a tuple until both input streams have yielded at least one item each.
combine_latest3
Combines three streams into a new stream that yields the latest items from all streams as a tuple. This stream won’t yield until all three input streams have yielded at least one item each.
combine_latest4
Combines four streams into a new stream that yields the latest items from all streams as a tuple. This stream won’t yield until all four input streams have yielded at least one item each.
combine_latest_opt
Combines two streams into a new stream, yielding tuples of (Option<T1>, Option<T2>). The stream starts yielding tuples as soon as one of the input streams yields an item, and the one that has not yet yielded has a corresponding None in its field of the tuple.
combine_latest_opt3
Combines three streams into a new stream, yielding tuples of (Option<T1>, Option<T2>, Option<T3>). The stream starts yielding as soon as any input stream yields an item.
combine_latest_opt4
Combines four streams into a new stream, yielding tuples of (Option<T1>, Option<T2>, Option<T3>, Option<T4>). The stream starts yielding as soon as any input stream yields an item.
combine_latest_optionalDeprecated
map_latest
Combines two streams into a new stream and applies the given function to each item. The function takes references as arguments, so unlike combine_latest the types don’t have to implement Clone. The returned stream won’t yield until both streams have yielded at least one item each.
map_latest3
Combines three streams and applies the given function to each set of latest items. The function takes references, so the types don’t need to implement Clone. Won’t yield until all three streams have yielded at least one item each.
map_latest4
Combines four streams and applies the given function to each set of latest items. The function takes references, so the types don’t need to implement Clone. Won’t yield until all four streams have yielded at least one item each.
map_latest_opt
Combines two streams into a new stream and applies the given function to each item. The function takes Option references and yields as soon as one input stream yields.
map_latest_opt3
Combines three streams and applies the given function to each set of latest items. The function takes Option references and yields as soon as any input stream yields.
map_latest_opt4
Combines four streams and applies the given function to each set of latest items. The function takes Option references and yields as soon as any input stream yields.
map_with_latest_from
Emits values from two streams by applying a function, but only when the primary (first) stream yields. The function takes references, so the types don’t need to implement Clone.
map_with_latest_from3
Emits values from three streams by applying a function, but only when the primary (first) stream yields. See map_with_latest_from for details.
map_with_latest_from4
Emits values from four streams by applying a function, but only when the primary (first) stream yields. See map_with_latest_from for details.
with_latest_from
Emits tuples of the latest values from both streams, but only when the primary (first) stream yields. The secondary stream silently updates its cached value without triggering output. No output is produced until the secondary stream has yielded at least once.
with_latest_from3
Emits tuples of the latest values from three streams, but only when the primary (first) stream yields. See with_latest_from for details.
with_latest_from4
Emits tuples of the latest values from four streams, but only when the primary (first) stream yields. See with_latest_from for details.