-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ChunksTimeout] Consumes the stream reminder and return the items immediately #7715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Use caseuse 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 reminder = chunked.into_reminder();
// use `receiver` here
} |
aaa757e to
0fb2203
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a similar interface in the standard library: std::slice::ChunksExactMut:into_remainder. This might be a better interface for your requirements. What do you think?
|
Thank you @ADD-SP for your review. The original plan was to completely consume the stream indeed and just return the reminder. This however won't work with the Pinned ChunksTimeout stream which is required when polling next() in a select statement. This means an I will still rename the method to |
f2c8ed1 to
4187d7b
Compare
| } | ||
|
|
||
| /// Drains the buffered items, returning them without waiting for the timeout or capacity limit. | ||
| pub fn reminder(mut self: Pin<&mut Self>) -> Vec<S::Item> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub fn reminder(mut self: Pin<&mut Self>) -> Vec<S::Item> { | |
| pub fn into_remainder(mut self: Pin<&mut Self>) -> Vec<S::Item> { |
Does this make more sense?
| } | ||
| } | ||
|
|
||
| /// Drains the buffered items, returning them without waiting for the timeout or capacity limit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// Drains the buffered items, returning them without waiting for the timeout or capacity limit. | |
| /// Consumes the [`ChunksTimeout`] and then returns all buffered items. |
…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.
|
Thank you so much @ADD-SP for your review. I applied all your comments. Let me know if you have more comments. |
[ChunksTimeout] Consumes the stream reminder and return the items immediately
Summary:
When the underlying stream is an exclusive reference (&mut stream), and we need to drop the
ChunksTimoutstream without losing the buffered items.