fmap/impls/
future.rs

1//! Implementation for boxed [`Future`]
2
3use super::*;
4
5use std::future::Future;
6use std::pin::Pin;
7
8impl<'a, A, B> Functor<'a, B> for Pin<Box<dyn 'a + Future<Output = A>>>
9where
10    A: 'a,
11    B: 'a,
12{
13    type Inner = A;
14    type Mapped = Pin<Box<dyn 'a + Future<Output = B>>>;
15    fn fmap<F>(self, mut f: F) -> Self::Mapped
16    where
17        F: 'a + Send + FnMut(Self::Inner) -> B,
18    {
19        Box::pin(async move { f(self.await) })
20    }
21}
22impl<'a, A, B> Functor<'a, B>
23    for Pin<Box<dyn 'a + Future<Output = A> + Send>>
24where
25    A: 'a,
26    B: 'a,
27{
28    type Inner = A;
29    type Mapped = Pin<Box<dyn 'a + Future<Output = B> + Send>>;
30    fn fmap<F>(self, mut f: F) -> Self::Mapped
31    where
32        F: 'a + Send + FnMut(Self::Inner) -> B,
33    {
34        Box::pin(async move { f(self.await) })
35    }
36}
37
38impl<'a, A> FunctorMut<'a, A> for Pin<Box<dyn 'a + Future<Output = A>>>
39where
40    A: 'a,
41{
42    fn fmap_mut<F>(&mut self, f: F)
43    where
44        F: 'a + Send + FnMut(&mut Self::Inner),
45    {
46        let this = std::mem::replace(
47            self,
48            Box::pin(async move { panic!("poisoned FunctorMut") }),
49        );
50        *self = this.fmap_fn_mutref(f);
51    }
52}
53impl<'a, A> FunctorMut<'a, A>
54    for Pin<Box<dyn 'a + Future<Output = A> + Send>>
55where
56    A: 'a,
57{
58    fn fmap_mut<F>(&mut self, f: F)
59    where
60        F: 'a + Send + FnMut(&mut Self::Inner),
61    {
62        let this = std::mem::replace(
63            self,
64            Box::pin(async move { panic!("poisoned FunctorMut") }),
65        );
66        *self = this.fmap_fn_mutref(f);
67    }
68}
69
70impl<'a, A, B> Pure<'a, B> for Pin<Box<dyn 'a + Future<Output = A>>>
71where
72    A: 'a,
73    B: 'a,
74{
75    fn pure(b: B) -> Self::Mapped {
76        Box::pin(std::future::ready(b))
77    }
78}
79impl<'a, A, B> Pure<'a, B>
80    for Pin<Box<dyn 'a + Future<Output = A> + Send>>
81where
82    A: 'a,
83    B: 'a + Send,
84{
85    fn pure(b: B) -> Self::Mapped {
86        Box::pin(std::future::ready(b))
87    }
88}
89
90impl<'a, A, B> Monad<'a, B> for Pin<Box<dyn 'a + Future<Output = A>>>
91where
92    A: 'a,
93    B: 'a,
94{
95    fn bind<F>(self, mut f: F) -> Self::Mapped
96    where
97        F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,
98    {
99        Box::pin(async move { f(self.await).await })
100    }
101}
102impl<'a, A, B> Monad<'a, B>
103    for Pin<Box<dyn 'a + Future<Output = A> + Send>>
104where
105    A: 'a + Send,
106    B: 'a + Send,
107{
108    fn bind<F>(self, mut f: F) -> Self::Mapped
109    where
110        F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,
111    {
112        Box::pin(async move { f(self.await).await })
113    }
114}
115
116impl<'a, A, B> Applicative<'a, B>
117    for Pin<Box<dyn 'a + Future<Output = A>>>
118where
119    A: 'a,
120    B: 'a,
121{
122    fn apply(
123        self,
124        f: Pin<Box<dyn 'a + Future<Output = BoxMapper<'a, Self, B>>>>,
125    ) -> Pin<Box<dyn 'a + Future<Output = B>>> {
126        Box::pin(async move {
127            // TODO: add test for `await` order
128            let mut mapper = f.await;
129            let a = self.await;
130            mapper(a)
131        })
132    }
133}
134impl<'a, A, B> Applicative<'a, B>
135    for Pin<Box<dyn 'a + Future<Output = A> + Send>>
136where
137    A: 'a,
138    B: 'a + Send,
139{
140    fn apply(
141        self,
142        f: Pin<
143            Box<
144                dyn 'a + Future<Output = BoxMapper<'a, Self, B>> + Send,
145            >,
146        >,
147    ) -> Pin<Box<dyn 'a + Future<Output = B> + Send>> {
148        Box::pin(async move {
149            // TODO: add test for `await` order
150            let mut mapper = f.await;
151            let a = self.await;
152            mapper(a)
153        })
154    }
155}