num-derive icon indicating copy to clipboard operation
num-derive copied to clipboard

Derive for newtypes with generics

Open adiba opened this issue 7 years ago • 3 comments

#[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>{...}

adiba avatar Oct 09 '18 03:10 adiba

Here, float will try to implement Float for T, but T is 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.

cuviper avatar Oct 09 '18 18:10 cuviper

https://crates.io/crates/derive_more This one performs well on this example for a handful of std-traits.

adiba avatar Oct 11 '18 19:10 adiba

Any updates on this issue?

asfishman avatar Sep 21 '19 02:09 asfishman