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
74
75
76
77
use futures::{Async, Future, Poll};
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct InspectErr<A, F>
where
A: Future,
{
future: A,
f: Option<F>,
}
impl<A, F> InspectErr<A, F>
where
A: Future,
for<'r> F: FnOnce(&'r A::Error) -> (),
{
fn new(future: A, f: F) -> Self {
Self { future, f: Some(f) }
}
}
impl<A, F> Future for InspectErr<A, F>
where
A: Future,
F: FnOnce(&A::Error),
{
type Item = A::Item;
type Error = A::Error;
fn poll(&mut self) -> Poll<A::Item, A::Error> {
match self.future.poll() {
Ok(Async::NotReady) => Ok(Async::NotReady),
Ok(Async::Ready(e)) => Ok(Async::Ready(e)),
Err(e) => {
if cfg!(any(debug_assertions, not(feature = "debug-only"))) {
(self.f.take().expect("cannot poll InspectErr twice"))(&e);
}
Err(e)
}
}
}
}
#[allow(clippy::module_name_repetitions)]
pub trait FutureInspector<I, E>: Future<Item = I, Error = E> {
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
where
for<'r> F: FnOnce(&'r Self::Error) -> (),
Self: Sized,
{
assert_future::<Self::Item, Self::Error, _>(InspectErr::new(self, f))
}
}
impl<I, E, T> FutureInspector<I, E> for T where T: Future<Item = I, Error = E> {}
fn assert_future<A, B, F>(f: F) -> F
where
F: Future<Item = A, Error = B>,
{
f
}