Skip to main content

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            let ($($var),*) = ($($var.clone()),*);
11            async move {
12                $($closure_body)*
13            }
14        }
15    };
16    ($($var:ident),*, |$( $closure_param:ident $(: $closure_param_ty:ty)? ),*| { $($closure_body:tt)* }) => {
17        {
18            let ($($var),*) = ($($var.clone()),*);
19            move |$( $closure_param $(: $closure_param_ty)? ),*| {
20                let ($($var),*) = ($($var.clone()),*);
21                async move {
22                    $($closure_body)*
23                }
24            }
25        }
26    };
27}