A stream adapter that buffers elements to improve performance when downstream tends to be busy (eg. db compactions).
In comparison to buffered it works not only with Futures but any element type as there is no async.
use buffer::StreamBufferExt;use futures::stream;use futures::StreamExt;asyncfnslow_cpu_heavy_operation(x:i32) -> i32{// Simulate a CPU-heavy operation with a delaysleep(Duration::from_millis(500)).await;
x
}asyncfnmock_db_insert(x:i32){// Simulate a database insert operation that can become slow during IO bounded compactionssleep(Duration::from_millis(100)).await;}let collected:Vec<i32> = stream::iter(0..100_000).map(|x| slow_cpu_heavy_operation(x)).buffer(1000).map(|x| mock_db_insert(x)).collect().await;