futures_util/future/
inspect_err.rs

1use futures_core::{Future, Poll, Async};
2use futures_core::task;
3
4/// Do something with the error of a future, passing it on.
5///
6/// This is created by the [`FutureExt::inspect_err`] method.
7#[derive(Debug)]
8#[must_use = "futures do nothing unless polled"]
9pub struct InspectErr<A, F> where A: Future {
10    future: A,
11    f: Option<F>,
12}
13
14pub fn new<A, F>(future: A, f: F) -> InspectErr<A, F>
15    where A: Future,
16          F: FnOnce(&A::Error),
17{
18    InspectErr {
19        future: future,
20        f: Some(f),
21    }
22}
23
24impl<A, F> Future for InspectErr<A, F>
25    where A: Future,
26          F: FnOnce(&A::Error),
27{
28    type Item = A::Item;
29    type Error = A::Error;
30
31    fn poll(&mut self, cx: &mut task::Context) -> Poll<A::Item, A::Error> {
32        match self.future.poll(cx) {
33            Ok(Async::Pending) => Ok(Async::Pending),
34            Ok(Async::Ready(e)) => Ok(Async::Ready(e)),
35            Err(e) => {
36                (self.f.take().expect("cannot poll InspectErr twice"))(&e);
37                Err(e)
38            },
39        }
40    }
41}