casey icon indicating copy to clipboard operation
casey copied to clipboard

Can this be used in `macro_rules` ?

Open wucke13 opened this issue 4 years ago • 1 comments

I want this macro to

macro_rules! enum_with_associated_type {
    ($($name:ident = $struct:ty, $id:expr ),+) =>{
        // ...
    }
}

enum_with_asssociated_type!{
    MSP_API_VERSION = 1,
}

to expand to this code

impl SomeTrait for MspApiVersion{
    fn id()->i16{1}
}

Is that possible with casey?

wucke13 avatar Oct 03 '20 12:10 wucke13

Hey @wucke13,

Something like this should work out of the box:

use casey::pascal;

macro_rules! enum_with_associated_type {
    ($name:ident = $id:expr) => {
        pascal!(struct $name {});
        impl SomeTrait for pascal!($name) {
            fn id() -> i16 { $id }
        }
    }
}


fn main() {
    trait SomeTrait { fn id() -> i16; };
    enum_with_associated_type!(
        MSP_API_VERSION = 1
    );
}

jordy25519 avatar Oct 06 '20 06:10 jordy25519

Should this issue be closed?

wt avatar Sep 27 '23 19:09 wt