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
use futures::{Async, Future, Stream};

/// Runs a stream or future until some condition is met.
pub struct Until<T, C> {
    orig: T,
    condition: C,
}

impl<T, C> Until<T, C> {
    pub fn new(orig: T, condition: C) -> Until<T, C> {
        Until {
            orig,
            condition,
        }
    }
}

impl<T, C> Future for Until<T, C>
where
    T: Future,
    C: Future<Item=()>,
    T::Error: From<C::Error>
{
    type Item = Option<T::Item>;
    type Error = T::Error;

    fn poll(&mut self) -> Result<Async<Option<T::Item>>, T::Error> {
        if let Async::Ready(()) = self.condition.poll()? {
            return Ok(Async::Ready(None));
        }

        let res = try_ready!(self.orig.poll());
        Ok(Async::Ready(Some(res)))
    }
}

impl<T, C> Stream for Until<T, C>
where
    T: Stream,
    C: Future<Item=()>,
    T::Error: From<C::Error>
{
    type Item = T::Item;
    type Error = T::Error;

    fn poll(&mut self) -> Result<Async<Option<T::Item>>, T::Error> {
        if let Async::Ready(()) = self.condition.poll()? {
            return Ok(Async::Ready(None));
        }

        self.orig.poll()
    }
}