Skip to content

Instantly share code, notes, and snippets.

Created August 30, 2015 16:45
Show Gist options
  • Save anonymous/5f646ed2380bac62a636 to your computer and use it in GitHub Desktop.
Save anonymous/5f646ed2380bac62a636 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::sync::Arc;
macro_rules! clone_army {
($vars:tt || $body:expr) => {
clone_army!(@emit $vars [] [] $body)
};
($vars:tt move || $body:expr) => {
clone_army!(@emit $vars [] [move] $body)
};
($vars:tt |$($param:ident),+| $body:expr) => {
clone_army!(@emit $vars [$($param),*] [] $body)
};
($vars:tt move |$($param:ident),+| $body:expr) => {
clone_army!(@emit $vars [$($param),*] [move] $body)
};
(@emit [$($var:ident),*] [$($param:ident),*] [$($mov:ident)*] $body:expr) => {{
$(let $var = $var.clone();)*
$($mov)* |$($param),*| $body
}}
}
#[derive(Debug)]
struct S;
impl Clone for S {
fn clone(&self) -> Self {
println!("cloning!");
S
}
}
fn main() {
let a = Arc::new(42);
let b = S;
clone_army!([a, b] move || println!("{} {:?}", a, b))();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment