Skip to content

Implemented #[skip] for the Add derive#472

Merged
tyranron merged 27 commits intoJelteF:masterfrom
ErmitaVulpe:master
Dec 1, 2025
Merged

Implemented #[skip] for the Add derive#472
tyranron merged 27 commits intoJelteF:masterfrom
ErmitaVulpe:master

Conversation

@ErmitaVulpe
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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
Copy Markdown
Contributor 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.

@tyranron tyranron added feature New derive or feature k::api Related to API (application interface) and removed enhancement labels Oct 29, 2025
@tyranron tyranron linked an issue Nov 5, 2025 that may be closed by this pull request
@tyranron tyranron self-assigned this Nov 11, 2025
@tyranron tyranron merged commit 2d3805b into JelteF:master Dec 1, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New derive or feature k::api Related to API (application interface)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ignore zero-width fields like PhantomData Allow ignore in more derive macros

2 participants