futures_util/future/
err_into.rs

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