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
18    ($($var:ident),*, |$( $closure_param:ident $(: $closure_param_ty:ty)? ),*| { $($closure_body:tt)* }) => {
19        {
20            #[allow(unused_parens)]
21            let ($($var),*) = ($($var.clone()),*);
22            move |$( $closure_param $(: $closure_param_ty)? ),*| {
23                let ($($var),*) = ($($var.clone()),*);
24                async move {
25                    $($closure_body)*
26                }
27            }
28        }
29    };
30}