Skip to content

Implemented #[skip] for the Add derive #472

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ErmitaVulpe
Copy link

@ErmitaVulpe ErmitaVulpe commented May 22, 2025

Resolves #437

Synopsis

As outlined in #437 it isn't possible to derive Add for structs containing zero sized fields

Example:

#[derive(Add)] // method not found in `PhantomData<T>`
struct TupleWithZst<T>(i32, PhantomData<T>);

#[derive(Add)] // method not found in `PhantomData<T>`
struct StructWithZst<T> {
    x: i32
    _marker: PhantomData<T>,
}

Solution

Added a #[skip] attribute that allows to skip zero sized fields like so:

#[derive(Add)]
struct TupleWithZst<T>(i32, #[add(skip)] PhantomData<T>);

#[derive(Add)]
struct StructWithZst<T> {
    x: i32,
    #[add(skip)]
    _marker: PhantomData<T>,
}

Checklist

  • Documentation is updated (if required)
  • Tests are added/updated (if required)
  • CHANGELOG entry is added (if required)

@tyranron tyranron added this to the 2.1.0 milestone May 22, 2025
Copy link
Collaborator

@tyranron tyranron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ErmitaVulpe thanks!

However, as all our attributes are namespaced (e.g. #[from(skip)] and etc.), I would like to see this PR to stick with this style and have #[add(skip)] instead of just #[skip].

Also, it would be nice if the util::attr::Skip would be reused for this purpose.

Added `#[<method>(skip)]` for `Add` and `AddAssign` as well as
`Mul` and `MulAssign` with `#[mul(forward)]` attribute.

Generics assigned to a zero sized field are not required to impl the
derived method.
@ErmitaVulpe
Copy link
Author

To do this I needed to get a bit more familiar with the codebase and i noticed that Mul and MulAssign depended on add_helpers.rs for handling of the #[mul(forward)] attrib so I also implemented it for them :P

Also I changed the way that trait bounds are added to the generics so that the generics contained in the skipped fields do not require the derived trait to be implemented for them.

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

Successfully merging this pull request may close these issues.

Ignore zero-width fields like PhantomData
2 participants