Created
August 3, 2017 20:45
-
-
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
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_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; | |
| } | |
| } | |
| )* | |
| } | |
| )* | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
badass