1 use {Poll, Async};
2 use stream::Stream;
3 
4 /// A stream which emits single element and then EOF.
5 ///
6 /// This stream will never block and is always ready.
7 #[derive(Debug)]
8 #[must_use = "streams do nothing unless polled"]
9 pub struct Once<T, E>(Option<Result<T, E>>);
10 
11 /// Creates a stream of single element
12 ///
13 /// ```rust
14 /// use futures::*;
15 ///
16 /// let mut stream = stream::once::<(), _>(Err(17));
17 /// assert_eq!(Err(17), stream.poll());
18 /// assert_eq!(Ok(Async::Ready(None)), stream.poll());
19 /// ```
once<T, E>(item: Result<T, E>) -> Once<T, E>20 pub fn once<T, E>(item: Result<T, E>) -> Once<T, E> {
21     Once(Some(item))
22 }
23 
24 impl<T, E> Stream for Once<T, E> {
25     type Item = T;
26     type Error = E;
27 
poll(&mut self) -> Poll<Option<T>, E>28     fn poll(&mut self) -> Poll<Option<T>, E> {
29         match self.0.take() {
30             Some(Ok(e)) => Ok(Async::Ready(Some(e))),
31             Some(Err(e)) => Err(e),
32             None => Ok(Async::Ready(None)),
33         }
34     }
35 }
36