Macro Error: repeating depth

Hi fellow Rust enthusiasts, please assist debugging the error in my macro.

macro_rules! new_struct {
(struct name:ident { ($fname:ident : $ftype:ty),* }) => {
struct name { ($fname : $ftype),*
}

    impl $name {
        fn field_names() -> &'static [&'static str] {
            static NAMES: &'static [&'static str] = &[$(stringify!($fname)),*];
            NAMES
        }
    }
    
    impl $name {
        fn update(&mut self, key: $ftype, value: &'static str) { <- offending line
            match key {
                $(stringify!($fname) => self.$fname = value,)*
                _ => (),
            }
        }
    }
}

}

Error message: variable ‘ftype’ is still repeating at this depth

Thanks

Your missing quite a few dollar signs.

macro_rules! new_struct {
(struct name:ident { $($fname:ident : $ftype:ty),* }) => {
struct $name { $($fname : $ftype),*
}

    impl $name {
        fn field_names() -> &'static [&'static str] {
            static NAMES: &[&str] = &[$(stringify!($fname)),*];
            NAMES
        }
    }
    
    impl $name {
        $(
        fn $fname(&mut self, value: $ftype) {
           self.$fname = value;
        }
        )*
    }
}
}

The way you had it before won’t work even if you fixed the macro errors because of type errors you would have in update.

1 Like

Hi Krishna,

Thank you for your assistance.

I have the following comments concerning your solution:

1/ The ‘$’ is missing on the second line
2/ The update method is missing
3/ The match statement is missing
4/ The original error does not appear - thanks, progress

I would require both the update method and match statement.
Once again I an grateful for your time and expertise.

Can i ask what you are trying to do?

Of course you can.

My initial idea was to have a single function to update the fields, but I realize that this would be cumbersome with macros. I have accepted your solution as a better design.

Once again thank your for your responses. I have a long learning curve with macros, but have made a good start with this recent experience.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.