af_core/future/
try_future.rs

1// Copyright © 2021 Alexandra Frydl
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use super::*;
8
9/// A [`Future`] that returns a result.
10pub trait TryFuture<T, E>: Future<Output = Result<T, E>> {}
11
12impl<F, T, E> TryFuture<T, E> for F where F: Future<Output = Result<T, E>> {}
13
14/// Extension methods for futures that return a result.
15pub trait TryFutureExt<T, E>: Sized {
16  /// Returns a new future that converts the error by applying a function.
17  fn map_err<F, M>(self, map: M) -> MapErr<Self, M>
18  where
19    M: FnOnce(E) -> F,
20  {
21    MapErr { future: self, map: Some(map) }
22  }
23}
24
25impl<F, T, E> TryFutureExt<T, E> for F where F: TryFuture<T, E> {}
26
27/// A [`TryFuture`] that modifies the original error.
28#[pin_project]
29pub struct MapErr<F, M> {
30  #[pin]
31  future: F,
32  map: Option<M>,
33}
34
35impl<T, I, O, F, M> Future for MapErr<F, M>
36where
37  F: Future<Output = Result<T, I>>,
38  M: FnOnce(I) -> O,
39{
40  type Output = Result<T, O>;
41
42  fn poll(self: Pin<&mut Self>, cx: &mut future::Context<'_>) -> future::Poll<Self::Output> {
43    let this = self.project();
44
45    match this.future.poll(cx) {
46      future::Poll::Ready(res) => future::Poll::Ready(res.map_err(this.map.take().unwrap())),
47      future::Poll::Pending => future::Poll::Pending,
48    }
49  }
50}