`#[derive(TransparentWrapper)]` doesn't work with `where` clause generic bounds.
JayWhite2357 opened this issue · 1 comments
Here is a MWE of code that I have:
trait MyTrait {}
#[derive(TransparentWrapper)]
#[repr(transparent)]
struct A<T>(T)
where
T: MyTrait;
However, this gives the error
the trait bound `T: MyTrait` is not satisfied
the trait `MyTrait` is not implemented for `T`
because the macro generates
unsafe impl<T> ::bytemuck::TransparentWrapper<T> for A<T> {}
rather than
unsafe impl<T> ::bytemuck::TransparentWrapper<T> for A<T> where T: MyTrait {}
The solution is to do this instead:
trait MyTrait {}
#[derive(TransparentWrapper)]
#[repr(transparent)]
struct A<T: MyTrait>(T);
However, as of Rust 1.78, clippy now gives the following message: bound is defined in more than one place
because #[warn(clippy::multiple_bound_locations)]
is on by default, and it can only be turned off at the module level because #[allow(clippy::multiple_bound_locations)]
needs to be applied to the macro generated code.
Suggestion 1: avoid using where
clauses for generics in the macro generated code.
Suggestion 2: add #[allow(clippy::multiple_bound_locations)]
to the macro generated code.
Suggestion 3: generate correct code when there are where
clause bounds for generics.