futures-concurrency-dynamic
A dynamic merge combinator for Rust streams, based on futures-concurrency.
Overview
This library provides DynamicMerge, a stream combinator that allows you to:
- Merge streams with different concrete types (but same
Itemtype) - Dynamically add new streams at runtime
- Automatically drop completed streams
Relation to futures-concurrency
While futures-concurrency provides excellent combinators for fixed sets of futures and streams known at compile time, this library extends that concept to support dynamic stream collections where:
- Streams can be added during runtime
- Each stream can have a different concrete type
- The number of streams is not known at compile time
- Streams are automatically cleaned up when they complete
Example
use DynamicMerge;
use StreamExt;
async
API
Direct API
DynamicMerge::new()- Create empty mergeDynamicMerge::with_capacity(n)- Pre-allocate capacitypush(stream)- Add a stream (can be called anytime)len()/is_empty()- Query active stream countclear()- Remove all streamsclose()- Signal no more streams will be added
Handle-based API
For cases where you need separate mutable access to the stream and the ability to push new streams:
use dynamic_merge_with_handle;
use StreamExt;
async
Handle API:
dynamic_merge_with_handle()- Create stream and handle pairDynamicMergeHandle::push(stream)- Add a stream via handleDynamicMergeHandle::close()- Signal completion via handleDynamicMergeHandle::len()/is_empty()- Query via handleDynamicMergeHandle::clear()- Clear all streams via handleDynamicMergeHandle::clone()- Clone the handle for shared access
License
MIT OR Apache-2.0