Lokathor / bytemuck

A crate for mucking around with piles of bytes
https://docs.rs/bytemuck
Apache License 2.0
697 stars 77 forks source link

`#[derive(TransparentWrapper)]` doesn't work with `where` clause generic bounds. #241

Open JayWhite2357 opened 3 months ago

JayWhite2357 commented 3 months ago

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.

Lokathor commented 3 months ago

Since #242 is merged, can we close this, or was that just a partial fix?