Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Ignore zero-width fields like PhantomData #437

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

Ignore zero-width fields like PhantomData #437

raklaptudirm opened this issue Jan 16, 2025 · 2 comments
Assignees
Milestone

Comments

@raklaptudirm
Copy link

Ignore zero-width fields to allow complier hinting with types like PhantomData while deriving trait imlementations.

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

// Equivalent to:
impl<B> Add for A<B> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0, PhantomData)
    }
}

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

@tyranron tyranron self-assigned this Jan 16, 2025
@tyranron tyranron added this to the 2.1.0 milestone Jan 16, 2025
@tyranron
Copy link
Collaborator

tyranron commented Jan 16, 2025

@raklaptudirm since Rust doesn't have any compile-time reflection, we cannot know whether a type is zero-sized during macro expansion. Of course, we can try to match them by name, but that will break shortly if the type is aliased by an unknown name for us.

What we can do instead, is to allow user specifying that:

#[derive(derive_more::Add)]
struct A<B>(u16, #[add(skip)] PhantomData<B>);

// will expand to:
impl<B> Add for A<B> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0, self.1)
    }
}

@raklaptudirm
Copy link
Author

Makes sense, #[add(skip)] or even a more general #[skip] would be perfect.

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

No branches or pull requests

2 participants