forked from rust-lang/rfcs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#211 from NeoLegends/buffer-combinator
Implement buffer combinator
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::mem; | ||
use std::prelude::v1::*; | ||
|
||
use {Async, Poll}; | ||
use stream::{Stream, Fuse}; | ||
|
||
/// An adaptor that chunks up elements in a vector. | ||
/// | ||
/// This adaptor will buffer up a list of items in the stream and pass on the | ||
/// vector used for buffering when a specified capacity has been reached. This is | ||
/// created by the `Stream::chunks` method. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct Chunks<S> | ||
where S: Stream | ||
{ | ||
capacity: usize, // TODO: Do we need this? Doesn't Vec::capacity() suffice? | ||
items: Vec<<S as Stream>::Item>, | ||
stream: Fuse<S> | ||
} | ||
|
||
pub fn new<S>(s: S, capacity: usize) -> Chunks<S> | ||
where S: Stream | ||
{ | ||
assert!(capacity > 0); | ||
|
||
Chunks { | ||
capacity: capacity, | ||
items: Vec::with_capacity(capacity), | ||
stream: super::fuse::new(s), | ||
} | ||
} | ||
|
||
impl<S> Stream for Chunks<S> | ||
where S: Stream | ||
{ | ||
type Item = Vec<<S as Stream>::Item>; | ||
type Error = <S as Stream>::Error; | ||
|
||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | ||
loop { | ||
if let Some(item) = try_ready!(self.stream.poll()) { | ||
// Push the item into the buffer and check whether it is | ||
// full. If so, replace our buffer with a new and empty one | ||
// and return the full one. | ||
self.items.push(item); | ||
if self.items.len() >= self.capacity { | ||
let full_buf = mem::replace(&mut self.items, Vec::with_capacity(self.capacity)); | ||
return Ok(Async::Ready(Some(full_buf))) | ||
} | ||
} else { | ||
// Since the underlying stream ran out of values, return | ||
// what we have buffered, if we have anything. | ||
return if self.items.len() > 0 { | ||
let full_buf = mem::replace(&mut self.items, Vec::new()); | ||
Ok(Async::Ready(Some(full_buf))) | ||
} else { | ||
Ok(Async::Ready(None)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters