| Crates.io | stream_shared |
| lib.rs | stream_shared |
| version | 0.8.6 |
| created_at | 2025-10-06 01:49:07.03994+00 |
| updated_at | 2025-12-13 02:50:01.012724+00 |
| description | A library for creating shareable streams that can be cloned and consumed by multiple consumers |
| homepage | |
| repository | https://github.com/ankurmittal/stream-shared-rs |
| max_upload_size | |
| id | 1869639 |
| size | 79,211 |
A Rust library for creating shareable streams that can be cloned and consumed by multiple tasks.
stream_shared provides SharedStream, which allows you to create a stream that can be cloned and shared across multiple consumers. All clones share the same underlying stream state, so clones created at the same time will see the same items, while clones created after partial consumption will only see the remaining items.
use stream_shared::SharedStream;
use futures_util::stream;
use futures_util::StreamExt;
#[tokio::main]
async fn main() {
let data = vec![1, 2, 3, 4, 5];
let stream = stream::iter(data.clone());
let shared_stream = SharedStream::new(stream);
// Clone the stream for multiple consumers
let consumer1 = shared_stream.clone();
let consumer2 = shared_stream.clone();
// Both consumers will receive all items
let (result1, result2) = tokio::join!(
consumer1.collect::<Vec<i32>>(),
consumer2.collect::<Vec<i32>>()
);
assert_eq!(result1, data);
assert_eq!(result2, data);
println!("Both consumers got: {:?}", result1);
}
Send + Sync - clones can be moved across threadsUnpin stream: Compatible with most async streams!Unpin support: Use Box::pin() for streams that aren't Unpinstats — Enable runtime diagnostics.SharedStream is optimized for scenarios with multiple consumers. Benchmark results show:
Benchmarks run on 1KB payloads with 5 consumers. See benches/ for full benchmark code.
UnpinCloneSend + SyncLicensed under the Apache License, Version 2.0.