Skip to content

Instantly share code, notes, and snippets.

@TuckerBMorgan
Created August 3, 2017 20:45
Show Gist options
  • Select an option

  • Save TuckerBMorgan/b7e5963b7a5aa4126b4d8708f3de8326 to your computer and use it in GitHub Desktop.

Select an option

Save TuckerBMorgan/b7e5963b7a5aa4126b4d8708f3de8326 to your computer and use it in GitHub Desktop.
a macro using the interpolate_idents macro to create boiler plate getters and setters for a struct
#[macro_export]
macro_rules! struct_builder {
(
$(
$struct_id:ident; public - [$($var:ident : $var_type:ty), *]; private - [$($pri_var:ident : $pri_var_type:ty), *];
),*
) => (
$(
pub struct $struct_id {
$(
pub $var : $var_type,
)*
$(
$pri_var: $pri_var_type,
)*
}
impl $struct_id {
$(
interpolate_idents!{
pub fn [get_ $var](&self) -> $var_type {
self.$var
}
}
interpolate_idents!{
pub fn [set_ $var](&mut self, in_var: $var_type){
self.$var = in_var;
}
}
)*
}
)*
);
}
@achadha235
Copy link

badass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment