futures_util/future/
map_err.rs

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