1 // This should really be in the stream module,
2 // but `pub(crate)` isn't available until Rust 1.18,
3 // and pre-1.18 there isn't a really good way to have a sub-module
4 // available to the crate, but not without it.
5 use core::marker::PhantomData;
6 
7 use {Poll, Async};
8 use stream::Stream;
9 
10 
11 /// A stream combinator used to convert a `Stream<Item=T,Error=E>`
12 /// to a `Stream<Item=Result<T,E>>`.
13 ///
14 /// A poll on this stream will never return an `Err`. As such the
15 /// actual error type is parameterized, so it can match whatever error
16 /// type is needed.
17 ///
18 /// This structure is produced by the `Stream::results` method.
19 #[derive(Debug)]
20 #[must_use = "streams do nothing unless polled"]
21 pub struct Results<S: Stream, E> {
22     inner: S,
23     phantom: PhantomData<E>
24 }
25 
new<S, E>(s: S) -> Results<S, E> where S: Stream26 pub fn new<S, E>(s: S) -> Results<S, E> where S: Stream {
27     Results {
28         inner: s,
29         phantom: PhantomData
30     }
31 }
32 
33 impl<S: Stream, E> Stream for Results<S, E> {
34     type Item = Result<S::Item, S::Error>;
35     type Error = E;
36 
poll(&mut self) -> Poll<Option<Result<S::Item, S::Error>>, E>37     fn poll(&mut self) -> Poll<Option<Result<S::Item, S::Error>>, E> {
38         match self.inner.poll() {
39             Ok(Async::Ready(Some(item))) => Ok(Async::Ready(Some(Ok(item)))),
40             Err(e) => Ok(Async::Ready(Some(Err(e)))),
41             Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
42             Ok(Async::NotReady) => Ok(Async::NotReady)
43         }
44     }
45 }
46 
47