future_fn/
macro.rs

1/// Generates an asynchronous closure that clones specified external variables and executes the given closure body asynchronously.
2///
3/// This macro supports two forms:
4/// 1. An async closure with no parameters.
5/// 2. An async closure with specified parameters.
6#[macro_export]
7macro_rules! future_fn {
8    ($($var:ident),*, { $($closure_body:tt)* }) => {
9        || {
10            #[allow(unused_parens)]
11            let ($($var),*) = ($($var.clone()),*);
12            async move {
13                $($closure_body)*
14            }
15        }
16    };
17    ($($var:ident),*, |$( $closure_param:ident $(: $closure_param_ty:ty)? ),*| { $($closure_body:tt)* }) => {
18        {
19            #[allow(unused_parens)]
20            let ($($var),*) = ($($var.clone()),*);
21            move |$( $closure_param $(: $closure_param_ty)? ),*| {
22                let ($($var),*) = ($($var.clone()),*);
23                async move {
24                    $($closure_body)*
25                }
26            }
27        }
28    };
29}