Created
January 6, 2016 04:14
-
-
Save anonymous/16f82ec8acca2021f75a to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
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