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
use core::marker::PhantomData;
use core::mem::PinMut;
use futures_core::future::{Future, TryFuture};
use futures_core::task::{self, Poll};

/// Future for the `err_into` combinator, changing the error type of a future.
///
/// This is created by the `Future::err_into` method.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct ErrInto<A, E> {
    future: A,
    _marker: PhantomData<E>,
}

impl<A, E> ErrInto<A, E> {
    unsafe_pinned!(future: A);
}

pub fn new<A, E>(future: A) -> ErrInto<A, E> {
    ErrInto {
        future,
        _marker: PhantomData,
    }
}

impl<A, E> Future for ErrInto<A, E>
    where A: TryFuture,
          A::Error: Into<E>,
{
    type Output = Result<A::Ok, E>;

    fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
        match self.future().try_poll(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(output) => {
                Poll::Ready(output.map_err(Into::into))
            }
        }
    }
}