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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Tap operations for `Future`, requires the feature `future`.
extern crate futures;

use self::futures::{Async, Future};

/// Tap operations for `Future`, requires the feature `future`.
pub trait TapFutureOps<T, E> {
    /// Executes a closure if the value is `Async::Ready(T)`.
    fn tap_ready<R, F: FnOnce(&T) -> R>(self, f: F) -> Self;

    // Executes a closure if the value is `Async::NotReady`.
    fn tap_not_ready<R, F: FnOnce() -> R>(self, f: F) -> Self;

    // Executes a closure if the value is `Err(E)`.
    fn tap_err<R, F: FnOnce(&E) -> R>(self, f: F) -> Self;
}

impl<T, E, FUT: Future<Item = T, Error = E>> TapFutureOps<T, E> for FUT {
    fn tap_ready<R, F: FnOnce(&T) -> R>(mut self, f: F) -> Self {
        if let Ok(Async::Ready(ref val)) = self.poll() {
            let _ = f(val);
        }
        self
    }

    fn tap_not_ready<R, F: FnOnce() -> R>(mut self, f: F) -> Self {
        if let Ok(Async::NotReady) = self.poll() {
            let _ = f();
        }
        self
    }

    fn tap_err<R, F: FnOnce(&E) -> R>(mut self, f: F) -> Self {
        if let Err(ref val) = self.poll() {
            let _ = f(val);
        }
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ready() {
        let mut foo = 0;
        let future = futures::future::result::<i32, i32>(Ok(5));

        let _ = future.tap_ready(|x| foo += *x);
        assert_eq!(foo, 5);
    }

    #[test]
    fn not_ready() {
        let mut foo = 0;
        let future = futures::future::empty::<i32, i32>();

        match future.tap_not_ready(|| foo += 5).poll() {
            Ok(Async::NotReady) => assert_eq!(foo, 5),
            _ => unreachable!()
        }
    }

    #[test]
    fn error() {
        let mut foo = 0;
        let future = futures::future::result::<i32, i32>(Err(5));

        let _ = future.tap_err(|x| foo += *x);
        assert_eq!(foo, 5);
    }
}