use std::prelude::v1::*; use std::mem; use {Future, Poll, Async}; use stream::Stream; /// A future which collects all of the values of a stream into a vector. /// /// This future is created by the `Stream::collect` method. #[derive(Debug)] #[must_use = "streams do nothing unless polled"] pub struct Collect where S: Stream { stream: S, items: Vec, } pub fn new(s: S) -> Collect where S: Stream, { Collect { stream: s, items: Vec::new(), } } impl Collect { fn finish(&mut self) -> Vec { mem::replace(&mut self.items, Vec::new()) } } impl Future for Collect where S: Stream, { type Item = Vec; type Error = S::Error; fn poll(&mut self) -> Poll, S::Error> { loop { match self.stream.poll() { Ok(Async::Ready(Some(e))) => self.items.push(e), Ok(Async::Ready(None)) => return Ok(Async::Ready(self.finish())), Ok(Async::NotReady) => return Ok(Async::NotReady), Err(e) => { self.finish(); return Err(e) } } } } }