Skip to content

Conversation

@muhamadazmy
Copy link

[ChunksTimeout] Consumes the stream and return the buffered items immediately

Summary:
When the underlying stream is an exclusive reference (&mut stream), and we need to drop the ChunksTimout stream without losing the buffered items.

@muhamadazmy
Copy link
Author

Use case

use std::time::Duration;

use tokio::sync::mpsc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};

#[tokio::main]
async fn main() {
    let (tx, rx) = mpsc::channel::<u64>(20);

    let mut receiver = ReceiverStream::new(rx);

    let chunked = (&mut receiver).chunks_timeout(3, Duration::from_secs(3));
    tokio::pin!(chunked);

    tx.send(10).await.unwrap();
    tx.send(20).await.unwrap();

    tokio::select! {
        Some(batch) = chunked.next() => {
            println!("Got: {:?}", batch);
        }
        _ = tokio::time::sleep(Duration::from_secs(1)) => {
            // another condition
        }
    }

    let remaining = chunked.remaining();

    // use `receiver` here
}

@ADD-SP ADD-SP added A-tokio Area: The main tokio crate A-tokio-stream Area: The tokio-stream crate and removed A-tokio Area: The main tokio crate labels Oct 28, 2025
@muhamadazmy muhamadazmy force-pushed the pr7715 branch 2 times, most recently from d725ea7 to aaa757e Compare October 30, 2025 10:25
…ediately

Summary:
When the underlying stream is an exclusive reference (&mut stream), and we need to drop the `ChunksTimout` stream without losing the buffered items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio-stream Area: The tokio-stream crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants