futures_util/future/
into_stream.rs

1use futures_core::{Async, Poll, Future, Stream};
2use futures_core::task;
3
4/// A type which converts a `Future` into a `Stream`
5/// containing a single element.
6#[must_use = "futures do nothing unless polled"]
7#[derive(Debug)]
8pub struct IntoStream<F: Future> {
9    future: Option<F>
10}
11
12pub fn new<F: Future>(future: F) -> IntoStream<F> {
13    IntoStream {
14        future: Some(future)
15    }
16}
17
18impl<F: Future> Stream for IntoStream<F> {
19    type Item = F::Item;
20    type Error = F::Error;
21
22    fn poll_next(&mut self, cx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
23        let ret = match self.future {
24            None => return Ok(Async::Ready(None)),
25            Some(ref mut future) => {
26                match future.poll(cx) {
27                    Ok(Async::Pending) => return Ok(Async::Pending),
28                    Err(e) => Err(e),
29                    Ok(Async::Ready(r)) => Ok(r),
30                }
31            }
32        };
33        self.future = None;
34        ret.map(|r| Async::Ready(Some(r)))
35    }
36}