Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- macro_rules! opt_field_struct {
- (
- $(#[$attr:meta])*
- $visibility:vis struct $struct_name:ident {
- $(
- $(#[$field_attr:meta])*
- $field_visibility:vis $field_name:ident: $field_ty:ty,
- )*
- }
- ) => {
- $(#[$attr])*
- $visibility struct $struct_name {
- $(
- $(#[$field_attr])*
- $field_visibility $field_name: $field_ty,
- )*
- }
- impl $struct_name {
- $visibility fn is_complete(&self) -> bool {
- $(
- if self.$field_name.is_none() {
- return false
- }
- )*
- true
- }
- }
- };
- }
- opt_field_struct!(
- struct MyStruct {
- pub optional_field: Option<usize>,
- another_optional_field: Option<String>,
- and_another: Option<String>,
- }
- );
- // Generated code:
- struct MyStruct {
- pub optional_field: Option<usize>,
- another_optional_field: Option<String>,
- and_another: Option<String>,
- }
- impl MyStruct {
- fn is_complete(&self) -> bool {
- if self.optional_field.is_none() {
- return false;
- }
- if self.another_optional_field.is_none() {
- return false;
- }
- if self.and_another.is_none() {
- return false;
- }
- true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement