Derive ZeroableInOption, PodInOption traits for transparent wrapper types
reinerp opened this issue · 3 comments
For
#[repr(transparent)]
struct Id(NonZeroU32);
this type is indeed zeroable in Option, but implementing it requires the user to write the word unsafe
. Would be helpful for bytemuck
to automatically derive it, perhaps via the TransparentWrapper
deriving.
I honestly don't think that this can be done, because the proc-macro just gets the input on a token level. This means that all the proc-macro sees is that there's an identifier it got as input which says "NonZeroU32", but the proc-macro is not able to ask the compiler "does this token mean the real ::core::num::NonZeroU32? or did they put a type alias on something to trick me?"
I'd love to be wrong! I can leave this issue open a while and link it in the dark-arts discord channel, maybe someone there will have some way to make it work.
Here's a possible way. Given this:
#[derive(ZeroableInOption)]
#[repr(transparent)]
struct Foo(Bar);
the proc-macro should check that #[repr(transparent)]
is present, and if so generate the following code:
unsafe impl ZeroableInOption for Foo where Bar: ZeroableInOption {}
Likewise for PodInOption. See playground.
I always forget that you can put a where clause on a concrete type.
Okay, that seems solid. If you do the PR (or find someone to do it for you) then I can have the proc-macro experts check it out and we can probably add this.
But i am not a proc-macro person, so I definitely won't be implementing this myself.