Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2016 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/16f82ec8acca2021f75a to your computer and use it in GitHub Desktop.
Save anonymous/16f82ec8acca2021f75a to your computer and use it in GitHub Desktop.
Shared via Rust Playground
macro_rules! unborrow {
(@parse () -> ($names:tt $lets:tt) $($thru:tt)*) => {
unborrow!(@out $names $lets $($thru)*)
};
(@parse ($arg:expr, $($rest:tt)*) -> ([$($names:ident),*] [$($lets:stmt);*]) $($thru:tt)*) => {
unborrow!(@parse ($($rest)*) -> ([$($names,)* arg] [$($lets;)* let arg = $arg]) $($thru)*)
};
(@out [$($names:ident),*] [$($lets:stmt);*] $obj:ident $meth:ident) => {{
$($lets;)*
$obj.$meth($($names),*)
}};
($obj:ident . $meth:ident ($($args:expr),*)) => {
unborrow!(@parse ($($args,)*) -> ([] []) $obj $meth)
}
}
fn main() {
let mut v = vec![1, 2, 3];
// v.reserve(v.capacity());
unborrow!(v.reserve(v.capacity()));
println!("{} {:?}", v.capacity(), v);
// v.insert(v.len() - 1, v[0] + 41);
unborrow!(v.insert(v.len() - 1, v[0] + 41));
println!("{:?}", v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment