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
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future, TryFuture};
use futures_core::task::{Context, Poll};
use pin_utils::{unsafe_pinned, unsafe_unpinned};

/// Future for the [`map_ok_or_else`](super::TryFutureExt::map_ok_or_else) method.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct MapOkOrElse<Fut, F, E> {
    future: Fut,
    f: Option<F>,
    e: Option<E>,
}

impl<Fut, F, E> MapOkOrElse<Fut, F, E> {
    unsafe_pinned!(future: Fut);
    unsafe_unpinned!(f: Option<F>);
    unsafe_unpinned!(e: Option<E>);

    /// Creates a new MapOkOrElse.
    pub(super) fn new(future: Fut, e: E, f: F) -> Self {
        Self { future, f: Some(f), e: Some(e) }
    }
}

impl<Fut: Unpin, F, E> Unpin for MapOkOrElse<Fut, F, E> {}

impl<Fut, F, E, T> FusedFuture for MapOkOrElse<Fut, F, E>
    where Fut: TryFuture,
          F: FnOnce(Fut::Ok) -> T,
          E: FnOnce(Fut::Error) -> T,
{
    fn is_terminated(&self) -> bool {
        self.f.is_none() || self.e.is_none()
    }
}

impl<Fut, F, E, T> Future for MapOkOrElse<Fut, F, E>
    where Fut: TryFuture,
          F: FnOnce(Fut::Ok) -> T,
          E: FnOnce(Fut::Error) -> T,
{
    type Output = T;

    fn poll(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Self::Output> {
        self.as_mut()
            .future()
            .try_poll(cx)
            .map(|result| {
                match result {
                    Ok(i) => (self.as_mut().f().take().expect("MapOkOrElse must not be polled after it returned `Poll::Ready`"))(i),
                    Err(e) => (self.as_mut().e().take().expect("MapOkOrElse must not be polled after it returned `Poll::Ready`"))(e),
                }
            })
    }
}