Skip to content

Instantly share code, notes, and snippets.

@durka
Forked from anonymous/playground.rs
Created June 25, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durka/766ae904a50683be3e93 to your computer and use it in GitHub Desktop.
Save durka/766ae904a50683be3e93 to your computer and use it in GitHub Desktop.
Macro capturing generic parameters
#![allow(dead_code)]
macro_rules! as_item { ($i:item) => ($i) }
macro_rules! s {
($t:ident) => {
impl $t {
}
};
($t:ident<$($x:tt),+>) => {
as_item!(
impl <$($x),+> $t <$($x),+> {
}
);
}
}
#[derive(Debug)] struct Thing { i: i32 }
#[derive(Debug)] struct OtherThing<'a, 'b, T> { p1: &'a i32, p2: &'b i32, t: T }
s!(Thing);
s!(OtherThing<'a, 'b, T>);
fn main() {
let a = Thing { i: 42 };
let b = OtherThing { p1: &a.i, p2: &42, t: "moo" };
println!("{:?} {:?}", a, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment