Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ignore zero-width fields like PhantomData #66

Open
raklaptudirm opened this issue Jan 16, 2025 · 2 comments
Open

Ignore zero-width fields like PhantomData #66

raklaptudirm opened this issue Jan 16, 2025 · 2 comments

Comments

@raklaptudirm
Copy link

Currently, the concept of "newtypes" only refers to structs with a single field. This prevents the derives for working when the structure might have zero-width types like PhantomData for complier hints.

// This should work.
#[derive(FromPrimitive)]
struct A<B>(u16, PhantomData<B>);

This behavior can be seen in #[repr(transparent)], where the annotation will work as long as there is only one non-zero-width type.

@cuviper
Copy link
Member

cuviper commented Jan 17, 2025

AFAIK, we don't have any type information like size available in a proc macro, nor even path resolution to know that we're looking at core or std::marker::PhantomData in particular.

Maybe we could use an explicit "ignore" attribute though, akin to #[derivative(Hash="ignore")] or #[serde(skip)].

PS: Note that the nomicon description is incomplete -- the reference mentions that the other ZST fields must also have alignment 1 for #[repr(transparent)]. Alignment doesn't really matter for newtype derives though.

@raklaptudirm
Copy link
Author

A similar resolution was suggested in JelteF/derive_more#437 so I am in favor of this approach.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants