owned_future/
traits.rs

1use core::pin::Pin;
2
3use alloc::boxed::Box;
4
5/// A functor trait for getting a simple future from an input.
6pub trait GetFut {
7    /// The input type of the functor.
8    type Input;
9    /// The output type of the resulting future.
10    type Output;
11
12    /// The functor applicator.
13    ///
14    /// Takes a mutable reference some input and returns a future
15    fn get_fut<'a>(self, input: &'a mut Self::Input) -> impl 'a + Future<Output = Self::Output>;
16}
17
18/// An extension trait to call [`crate::get`]
19pub trait ApplyGetFut: GetFut {
20    /// Just calls [`crate::get`]
21    fn apply_to(self, val: Self::Input) -> Pin<Box<impl Future<Output = Self::Output>>>;
22}
23
24impl<G: GetFut> ApplyGetFut for G {
25    fn apply_to(self, val: Self::Input) -> Pin<Box<impl Future<Output = Self::Output>>> {
26        crate::make(val, self)
27    }
28}
29
30/// A functor trait for trying to get a complex future from an input.
31pub trait TryGetFut {
32    /// The input type of the functor. Will be returned to the caller if unsuccessful.
33    type Input;
34    /// The output type of the resulting future if successful.
35    type Output;
36    /// Auxiliary data that should be returned to the caller if successful.
37    type Aux;
38    /// Error data that should be returned to the caller if unsuccessful.
39    type Error;
40
41    /// The functor applicator.
42    ///
43    /// Takes a mutable reference some input and returns either a future and some auxiliary data
44    /// or an error.
45    fn try_get_fut<'a>(
46        self,
47        input: &'a mut Self::Input,
48    ) -> Result<(impl 'a + Future<Output = Self::Output>, Self::Aux), Self::Error>;
49}
50
51/// An extension trait to call [`crate::try_get`]
52pub trait TryApplyGetFut: TryGetFut {
53    /// Just calls [`crate::try_get`]
54    fn apply_to(
55        self,
56        val: Self::Input,
57    ) -> Result<(Pin<Box<impl Future<Output = Self::Output>>>, Self::Aux), (Self::Input, Self::Error)>;
58}
59
60impl<G: TryGetFut> TryApplyGetFut for G {
61    fn apply_to(
62        self,
63        val: Self::Input,
64    ) -> Result<(Pin<Box<impl Future<Output = Self::Output>>>, Self::Aux), (Self::Input, Self::Error)>
65    {
66        crate::try_make(val, self)
67    }
68}
69
70#[cfg(feature = "async")]
71mod async_feature {
72    use alloc::boxed::Box;
73    use core::pin::Pin;
74
75    use crate::{AsyncSendTryOutput, AsyncTryOutput};
76
77    /// A functor trait for trying to get a complex future from an input.
78    pub trait AsyncTryGetFut<'a>: 'a {
79        /// The input type of the functor. Will be returned to the caller if unsuccessful.
80        type Input: 'a;
81        /// The output type of the resulting future if successful.
82        type Output: 'a;
83        /// Auxiliary data that should be returned to the caller if successful.
84        type Aux: 'a;
85        /// Error data that should be returned to the caller if unsuccessful.
86        type Error: 'a;
87
88        /// The functor applicator.
89        ///
90        /// Takes a mutable reference some input and returns either a future and some auxiliary data
91        /// or an error.
92        #[allow(async_fn_in_trait)]
93        async fn async_try_get_fut<'b>(
94            self,
95            input: &'b mut Self::Input,
96        ) -> Result<(impl 'b + Future<Output = Self::Output>, Self::Aux), Self::Error>;
97    }
98
99    /// An extension trait to call [`crate::AsyncTry`]
100    pub trait AsyncTryApplyGetFut<'a>: AsyncTryGetFut<'a> {
101        /// Just calls [`crate::try_get`]
102        #[allow(async_fn_in_trait)]
103        async fn apply_to(self, val: Self::Input) -> AsyncTryOutput<'a, Self>;
104    }
105
106    impl<'a, G: AsyncTryGetFut<'a>> AsyncTryApplyGetFut<'a> for G {
107        async fn apply_to(self, val: Self::Input) -> AsyncTryOutput<'a, Self> {
108            crate::AsyncTry::new(val, self).await
109        }
110    }
111
112    /// A functor trait for trying to get a complex future from an input.
113    pub trait AsyncSendTryGetFut<'a>: 'a + Send {
114        /// The input type of the functor. Will be returned to the caller if unsuccessful.
115        type Input: 'a + Sync + Send;
116        /// The output type of the resulting future if successful.
117        type Output: 'a + Send;
118        /// Auxiliary data that should be returned to the caller if successful.
119        type Aux: 'a + Send;
120        /// Error data that should be returned to the caller if unsuccessful.
121        type Error: 'a + Send;
122
123        /// The functor applicator.
124        ///
125        /// Takes a mutable reference some input and returns either a future and some auxiliary data
126        /// or an error.
127        ///
128        /// For now this has to use `Pin<Box<dyn Future>>` due a compiler bug
129        /// ([rust-lang/rust#100013](https://github.com/rust-lang/rust/issues/100013)). This will
130        /// change in a future release when this bug is fixed.
131        fn async_send_try_get_fut<'b>(
132            self,
133            input: &'b mut Self::Input,
134        ) -> Pin<
135            Box<
136                dyn 'b
137                    + Send
138                    + Future<
139                        Output = Result<
140                            (
141                                Pin<Box<dyn 'b + Send + Future<Output = Self::Output>>>,
142                                Self::Aux,
143                            ),
144                            Self::Error,
145                        >,
146                    >,
147            >,
148        >;
149    }
150
151    /// An extension trait to call [`crate::AsyncSendTry`]
152    pub trait AsyncSendTryApplyGetFut<'a>: AsyncSendTryGetFut<'a> {
153        /// Just calls [`crate::AsyncSendTry`]
154        #[allow(async_fn_in_trait)]
155        async fn apply_to(self, val: Self::Input) -> AsyncSendTryOutput<'a, Self>;
156    }
157
158    impl<'a, G: AsyncSendTryGetFut<'a>> AsyncSendTryApplyGetFut<'a> for G {
159        async fn apply_to(self, val: Self::Input) -> AsyncSendTryOutput<'a, Self> {
160            crate::AsyncSendTry::new(val, self).await
161        }
162    }
163}
164
165#[cfg(feature = "async")]
166pub use async_feature::*;