af_core/future/
try_future.rs1use super::*;
8
9pub 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
14pub trait TryFutureExt<T, E>: Sized {
16 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#[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}