1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::mem;
use futures::{Async, Future, Stream};

/// Adapts a stream to a future by taking the first successful item yielded by the stream. If the
/// stream ends before yielding an `Ok` then all the errors that were yielded by the stream are
/// returned in a vector.
pub struct FirstOk<S>
where
    S: Stream,
{
    stream: S,
    errors: Vec<S::Error>,
}

impl<S> FirstOk<S>
where
    S: Stream,
{
    pub fn new(stream: S) -> FirstOk<S> {
        FirstOk {
            stream: stream,
            errors: Vec::new(),
        }
    }
}

impl<S> Future for FirstOk<S>
where
    S: Stream
{
    type Item = S::Item;
    type Error = Vec<S::Error>;

    fn poll(&mut self) -> Result<Async<S::Item>, Vec<S::Error>> {
        loop {
            match self.stream.poll() {
                Ok(Async::Ready(Some(val))) => {
                    self.errors.clear();
                    return Ok(Async::Ready(val));
                },
                Ok(Async::Ready(None)) => {
                    let errors = mem::replace(&mut self.errors, Vec::new());
                    return Err(errors);
                },
                Ok(Async::NotReady) => {
                    return Ok(Async::NotReady);
                },
                Err(e) => {
                    self.errors.push(e);
                },
            }
        }
    }
}