Std::mem::size_of() of an enum in a derive macro

Hi all,

I'm creating a derive macro for implementing the FromStr and TryFrom traits for my enums. But I'd like to only create TryFrom<u8> or TryFrom<u16> depending on the memory size of the enum, tagged with #[repr(u8)] or #[repr(u16)].

I can get the ident of the enum and can create both TryFrom<u8> and TryFrom<u16> traits, but I'd like to create them depending on the enum's size.

Any easy solution other than diving into the DeriveInput structure and getting back the value inside the #[repr] ?

Enum example:

#[repr(u8)]
enum Foo {
 A=1,
 B=2
}

Thanks for your help.

Macros don't have access to type information (macro expansion runs before typechecking even starts), so no, it's not possible in general.

If you want an easy(er) way of parsing attributes, check out Darling.

@H2CO3 thanks for your answer, I'll check it out !