Skip to content

Instantly share code, notes, and snippets.

Created September 24, 2015 02:56
Show Gist options
  • Save anonymous/19383c16b5c3d417befb to your computer and use it in GitHub Desktop.
Save anonymous/19383c16b5c3d417befb to your computer and use it in GitHub Desktop.
Shared via Rust Playground
macro_rules! sticky_visibility {
(@as_item $i:item) => ($i);
(@parse [$typ:ident] $_cur:tt $members:tt [$($elemname:ident),*] [$($elemtyp:ty),*] { $(,)* }) => {
sticky_visibility!(@as_item pub struct $typ $members);
impl $typ { pub fn new($($elemname: $elemtyp),*) -> $typ { $typ { $($elemname: $elemname),* } } }
};
(@parse $t_:tt $_cur:tt $members:tt $en_:tt $et_:tt { pub: $($body:tt)* }) => {
sticky_visibility!(@parse $t_ [pub] $members $en_ $et_ { $($body)* });
};
(@parse $t_:tt $_cur:tt $members:tt $en_:tt $et_:tt { priv: $($body:tt)* }) => {
sticky_visibility!(@parse $t_ [] $members $en_ $et_ { $($body)* });
};
(@parse $t_:tt [$($cur:ident)*] { $($members:tt)* } [$($elemname:ident),*] [$($elemtyp:ty),*] { $n:ident: $t:ty, $($body:tt)* }) => {
sticky_visibility!(@parse $t_ [$($cur)*] { $($members)* $($cur)* $n: $t, } [$($elemname,)* $n] [$($elemtyp,)* $t] { $($body)* });
};
(@parse $t_:tt [$($cur:ident)*] { $($members:tt)* } [$($elemname:ident),*] [$($elemtyp:ty),*] { $n:ident: $t:ty }) => {
sticky_visibility!(@parse $t_ [$($cur)*] { $($members)* $($cur)* $n: $t, } [$($elemname,)* $n] [$($elemtyp,)* $t] { });
};
(pub struct $typ:ident { $($body:tt)* }) => {
sticky_visibility!(@parse [$typ] [] {} [] [] { $($body)* });
};
}
mod inner {
sticky_visibility! {
pub struct Foo {
pub:
a: i32,
b: bool,
priv:
c: (i32, bool),
d: char,
}
}
}
use inner::Foo;
fn main() {
let foo = Foo::new(42, true, (1, false), '\0');
println!("{:?} {:?}", foo.a, foo.b); // ok
println!("{:?} {:?}", foo.c, foo.d); // error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment