use std; use futures::stream::Stream; use futures::{Poll, Async}; pub struct LineStream where S: Stream { stream: S, progress: Vec } impl LineStream where S: Stream + Sized { pub fn new(stream: S) -> LineStream { LineStream { stream: stream, progress: vec![] } } } impl Stream for LineStream where S: Stream { type Item = Vec; type Error = E; fn poll(&mut self) -> Poll, Self::Error> { loop { match self.stream.poll() { Ok(Async::Ready(Some(byte))) => { if byte == 0x0a { let mut new_vec = vec![]; std::mem::swap(&mut self.progress, &mut new_vec); return Ok(Async::Ready(Some(new_vec))) } else { self.progress.push(byte) } }, Ok(Async::Ready(None)) => return Ok(Async::Ready(None)), Ok(Async::NotReady) => return Ok(Async::NotReady), Err(e) => return Err(e) } } } }