num-derive
num-derive copied to clipboard
Derive for newtypes with generics
#[derive(Float)]
struct A<T:Float>(T);
Here, float will try to implement Float for T, but T is a parameter.
Instead, it should:
impl<T:Float> Float for A<T>{...}
Here,
floatwill try to implement Float forT, butTis a parameter.
It is implementing for A, but it's missing the generic parameter. The expanded code looks like:
impl _num_traits::Float for A {
fn nan() -> Self { A(<T as _num_traits::Float>::nan()) }
...
error[E0412]: cannot find type `T` in this scope
--> src/lib.rs:8:19
|
8 | struct A<T:Float>(T);
| ^ did you mean `A`?
error[E0243]: wrong number of type arguments: expected 1, found 0
--> src/lib.rs:8:8
|
8 | struct A<T:Float>(T);
| ^ expected 1 type argument
error: aborting due to 2 previous errors
Instead, it should:
impl<T:Float> Float for A<T>{...}
Yes, that should work.
We should see if there are other derive crates we can use as an example of how to produce generic args and constraints properly.
https://crates.io/crates/derive_more This one performs well on this example for a handful of std-traits.
Any updates on this issue?