Repository: tokio-rs/tokio
File: tokio-stream/src/stream_ext/chunks_timeout.rs (Rust)

Code before:
impl<S: Stream> Stream for ChunksTimeout<S> {
    type Item = Vec<S::Item>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut me = self.as_mut().project();

        match me.stream.as_mut().poll_next(cx) {
            Poll::Pending => {}
            Poll::Ready(Some(item)) => {
                me.items.push(item);
                if me.items.len() >= *me.cap {
                    return Poll::Ready(Some(self.take()));
                }
            }

            Poll::Ready(None) => {
                let last = if me.items.is_empty() {
                    None
                } else {
                    let full_buf = std::mem::take(me.items);
                    Some(full_buf)
                };

                return Poll::Ready(last);
            }
        }

        match me.deadline.as_mut().poll(cx) {
            Poll::Pending => {}
            Poll::Ready(()) => {
                return Poll::Ready(Some(self.take()));
            }
        }

        cx.waker().wake_by_ref();
        Poll::Pending
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let chunk_len = if self.items.is_empty() { 0 } else { 1 };
        let (lower, upper) = self.stream.size_hint();
        let lower = lower.saturating_add(chunk_len);
        let upper = upper.and_then(|x| x.checked_add(chunk_len));
        (lower, upper)
    }
}


Review comment (suggestion):
```suggestion
        loop {
            match me.stream.as_mut().poll_next(cx) {
                Poll::Pending => break,
                Poll::Ready(Some(item)) => {
                    me.items.push(item);
                    if me.items.len() >= *me.cap {
                        return Poll::Ready(Some(self.take()));
                    }
                }

                Poll::Ready(None) => {
                    let last = if me.items.is_empty() {
                        None
                    } else {
                        let full_buf = std::mem::take(me.items);
                        Some(full_buf)
                    };

                    return Poll::Ready(last);
                }
            }
        }
```


Code after:
                    if me.items.is_empty() {
                        me.deadline.as_mut().reset(Instant::now() + *me.duration);
                        me.items.reserve_exact(*me.cap);
                    }
                    me.items.push(item);
                    if me.items.len() >= *me.cap {
                        return Poll::Ready(Some(std::mem::take(me.items)));
                    }
                }
                Poll::Ready(None) => {
                    // Returning Some here is only correct because we fuse the inner stream.
                    let last = if me.items.is_empty() {
                        None
                    } else {
                        Some(std::mem::take(me.items))
                    };

                    return Poll::Ready(last);
                }
            }
        }

        if !me.items.is_empty() {
            ready!(me.deadline.poll(cx));
            return Poll::Ready(Some(std::mem::take(me.items)));
        }

        Poll::Pending
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let chunk_len = if self.items.is_empty() { 0 } else { 1 };
        let (lower, upper) = self.stream.size_hint();
        let lower = (lower / self.cap).saturating_add(chunk_len);
        let upper = upper.and_then(|x| x.checked_add(chunk_len));
        (lower, upper)
    }
}