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§
- Combine
Latest - 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. - Combine
Latest Opt - Like
CombineLatest, but wraps each position inOptionand starts yielding as soon as any input stream produces a value. Positions that haven’t yielded yet areNone. Implemented for tuples of 2 to 12 streams whose items implementClone. - 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 implementClone. Implemented for tuples of 2 to 12 streams. - MapLatest
Opt - Like
MapLatest, but the closure receivesOptionreferences and the output stream starts yielding as soon as any input stream produces a value. Implemented for tuples of 2 to 12 streams. - MapWith
Latest From - Like
WithLatestFrom, but applies a closure to references of the latest values instead of returning a tuple. The item types don’t need to implementClone. Implemented for tuples of 2 to 12 streams. - With
Latest From - 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 correspondingNonein 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_ optional Deprecated - 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_latestthe types don’t have to implementClone. 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
Optionreferences 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
Optionreferences 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
Optionreferences 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_fromfor 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_fromfor 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_fromfor details. - with_
latest_ from4 - Emits tuples of the latest values from four streams, but only when the primary (first)
stream yields. See
with_latest_fromfor details.