derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Ignore zero-width fields like `PhantomData`

Open raklaptudirm opened this issue 10 months ago • 2 comments
trafficstars

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.

raklaptudirm avatar Jan 16 '25 10:01 raklaptudirm