Skip to content

Commit 03f8b38

Browse files
taiki-ecramertj
authored andcommitted
Correct the behavior of Chunks when items are ZST
1 parent 58e3e1f commit 03f8b38

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

futures-util/src/stream/chunks.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use alloc::prelude::v1::*;
1616
pub struct Chunks<St: Stream> {
1717
stream: Fuse<St>,
1818
items: Vec<St::Item>,
19+
cap: usize, // https://github.com/rust-lang-nursery/futures-rs/issues/1475
1920
}
2021

2122
impl<St: Unpin + Stream> Unpin for Chunks<St> {}
@@ -30,11 +31,12 @@ impl<St: Stream> Chunks<St> where St: Stream {
3031
Chunks {
3132
stream: super::Fuse::new(stream),
3233
items: Vec::with_capacity(capacity),
34+
cap: capacity,
3335
}
3436
}
3537

3638
fn take(mut self: Pin<&mut Self>) -> Vec<St::Item> {
37-
let cap = self.items.capacity();
39+
let cap = self.cap;
3840
mem::replace(self.as_mut().items(), Vec::with_capacity(cap))
3941
}
4042

@@ -69,15 +71,14 @@ impl<St: Stream> Stream for Chunks<St> {
6971
mut self: Pin<&mut Self>,
7072
waker: &Waker,
7173
) -> Poll<Option<Self::Item>> {
72-
let cap = self.items.capacity();
7374
loop {
7475
match ready!(self.as_mut().stream().poll_next(waker)) {
7576
// Push the item into the buffer and check whether it is full.
7677
// If so, replace our buffer with a new and empty one and return
7778
// the full one.
7879
Some(item) => {
7980
self.as_mut().items().push(item);
80-
if self.items.len() >= cap {
81+
if self.items.len() >= self.cap {
8182
return Poll::Ready(Some(self.as_mut().take()))
8283
}
8384
}

0 commit comments

Comments
 (0)