clonelicious/macro.rs
1/// The `clone!` macro automatically clones the variables passed to it and immediately executes the closure.
2///
3/// This macro simplifies the process of cloning variables and passing them into a closure, while also
4/// ensuring the closure is executed right away with the cloned values.
5///
6/// # Parameters
7/// - `$( $var:ident ),*`: A list of variables to be cloned. These variables will be cloned before
8/// being passed to the closure. Each variable is cloned individually.
9/// - `move |$($arg:ident : $typ:ty),*| $body:block`: A closure that takes the cloned variables as arguments.
10/// The closure's parameters should be annotated with types. The closure body will be executed with the
11/// cloned values.
12///
13/// # Behavior
14/// - The macro first clones the provided variables (`s1`, `s2`, etc.).
15/// - It then constructs and immediately calls a `move` closure, passing the cloned values as arguments.
16/// - The closure is executed right away with the cloned data, ensuring no ownership or borrowing issues.
17///
18/// # Notes
19/// This macro is useful for cases where you need to clone values and pass them to a closure,
20/// but don't want to manually clone each value before passing it into the closure.
21#[macro_export]
22macro_rules! clone {
23 ( $( $var:ident ),*, $body:block ) => {{
24 $(
25 let $var = $var.clone();
26 )*
27 $body
28 }};
29}