owned_future/
macros.rs

1/// A macro for quickly implementing [`crate::GetFut`] without capturing.
2///
3/// If you need capturing, you will have to manually capture what you want in a user-defined struct
4/// and implement [`crate::GetFut`] for that struct.
5#[macro_export]
6macro_rules! get {
7    (fn($input:ident: &mut $ity:ty) -> $oty:ty { $($v:tt)+ }) => {{
8        $crate::get!(fn<'a>($input: &mut $ity) -> $oty {$($v)+})
9    }};
10    (fn<$a:lifetime>($input:ident: &mut $ity:ty) -> $oty:ty { $($v:tt)+ }) => {{
11        #[derive(Clone, Copy)]
12        struct Impl;
13        impl $crate::GetFut for Impl {
14            type Input = $ity;
15            type Output = $oty;
16            fn get_fut<$a>(self, $input: &$a mut Self::Input) -> impl $a+Future<Output = Self::Output> {
17                $($v)+
18            }
19        }
20        Impl
21    }};
22}
23
24/// A macro for quickly implementing [`crate::TryGetFut`] without capturing.
25///
26/// If you need capturing, you will have to manually capture what you want in a user-defined struct
27/// and implement [`crate::TryGetFut`] for that struct.
28#[macro_export]
29macro_rules! try_get {
30    (fn($input:ident: &mut $ity:ty) -> Result<($oty:ty, $aty:ty), $ety:ty> { $($v:tt)+ }) => {{
31        $crate::try_get!(fn<'a>($input: &mut $ity) -> Result<($oty, $aty), $ety> {$($v)+})
32    }};
33    (fn<$a:lifetime>($input:ident: &mut $ity:ty) -> Result<($oty:ty, $aty:ty), $ety:ty> { $($v:tt)+ }) => {{
34        #[derive(Clone, Copy)]
35        struct Impl;
36        impl $crate::TryGetFut for Impl {
37            type Input = $ity;
38            type Output = $oty;
39            type Aux = $aty;
40            type Error = $ety;
41            fn try_get_fut<$a>(self, $input: &$a mut Self::Input) -> Result<(impl $a+Future<Output = Self::Output>, Self::Aux), Self::Error> {
42                $($v)+
43            }
44        }
45        Impl
46    }};
47}
48
49/// A macro for quickly implementing [`crate::AsyncTryGetFut`] without capturing.
50///
51/// If you need capturing, you will have to manually capture what you want in a user-defined struct
52/// and implement [`crate::AsyncTryGetFut`] for that struct.
53#[cfg(feature = "async")]
54#[macro_export]
55macro_rules! async_try_get {
56    (async fn($input:ident: &mut $ity:ty) -> Result<($oty:ty, $aty:ty), $ety:ty> { $($v:tt)+ }) => {{
57        $crate::async_try_get!(async fn<'a, 'b>($input: &mut $ity) -> Result<($oty, $aty), $ety> {$($v)+})
58    }};
59    (async fn<$a:lifetime, $b:lifetime>($input:ident: &mut $ity:ty) -> Result<($oty:ty, $aty:ty), $ety:ty> { $($v:tt)+ }) => {{
60        #[derive(Clone, Copy)]
61        struct Impl;
62        impl<$a> $crate::AsyncTryGetFut<$a> for Impl {
63            type Input = $ity;
64            type Output = $oty;
65            type Aux = $aty;
66            type Error = $ety;
67            async fn async_try_get_fut<$b>(self, $input: &$b mut Self::Input) -> Result<(impl $b+Future<Output = Self::Output>, Self::Aux), Self::Error> {
68
69                $($v)+
70            }
71        }
72        Impl
73    }};
74}
75/// A macro for quickly implementing [`crate::AsyncSendTryGetFut`] without capturing.
76///
77/// If you need capturing, you will have to manually capture what you want in a user-defined struct
78/// and implement [`crate::AsyncSendTryGetFut`] for that struct.
79///
80/// For now this has to use `Pin<Box<dyn Future>>` due a compiler bug
81/// ([rust-lang/rust#100013](https://github.com/rust-lang/rust/issues/100013)).This will
82/// change in a future release when this bug is fixed.
83#[cfg(feature = "async")]
84#[macro_export]
85macro_rules! async_send_try_get {
86    (fn($input:ident: &mut $ity:ty) -> Result<($oty:ty, $aty:ty), $ety:ty> { $($v:tt)+ }) => {{
87        $crate::async_send_try_get!(fn<'a, 'b>($input: &mut $ity) -> Result<($oty, $aty), $ety> {$($v)+})
88    }};
89    (fn<$a:lifetime, $b:lifetime>($input:ident: &mut $ity:ty) -> Result<($oty:ty, $aty:ty), $ety:ty> { $($v:tt)+ }) => {{
90
91        use alloc::boxed::Box;
92        use core::pin::Pin;
93
94        #[derive(Clone, Copy)]
95        struct Impl;
96        impl<$a> $crate::AsyncSendTryGetFut<$a> for Impl {
97            type Input = $ity;
98            type Output = $oty;
99            type Aux = $aty;
100            type Error = $ety;
101            fn async_send_try_get_fut<$b>(self, $input: &$b mut Self::Input) -> Pin<Box<dyn $b + Send + Future<Output = Result<(Pin<Box<dyn $b + Send + Future<Output = Self::Output>>>, Self::Aux), Self::Error>>>> {
102                Box::pin({$($v)+})
103            }
104        }
105        Impl
106    }};
107}