-
-
Save durka/01467b727bff4fcdec01 to your computer and use it in GitHub Desktop.
Rust clone_army! macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || $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