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

Implement derives for generic wrapper types

Open oli-cosmian opened this issue 4 years ago • 4 comments

fixes #19

oli-cosmian avatar Jun 26 '20 15:06 oli-cosmian

Rebased

oli-cosmian avatar Jun 29 '20 07:06 oli-cosmian

Just missed a couple spots.

oops, resolved and grepped to see if there are any others (there weren't).

oli-cosmian avatar Jun 30 '20 08:06 oli-cosmian

Oh. I just realized that

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    PartialOrd,
    ToPrimitive,
    FromPrimitive,
    NumOps,
    NumCast,
    One,
    Zero,
    Num,
)]
struct MyThing<T>(Wrapping<T>);

does not work, because there's no T: Num bound on the Num impl, causing rustc to fail to infer that Wrapping<T>: Num holds. If we add T: Num bounds, that makes struct MyThing<T, U>(SomeTypeImplingNumEvenIfTDoesNot<T>); not work anymore.

oli-cosmian avatar Jun 30 '20 09:06 oli-cosmian

because there's no T: Num bound on the Num impl, causing rustc to fail to infer that Wrapping<T>: Num holds. If we add T: Num bounds, that makes struct MyThing<T, U>(SomeTypeImplingNumEvenIfTDoesNot<T>); not work anymore.

Hmm, now it seems like we're in that gray area of https://github.com/rust-lang/rust/issues/26925, where it's not really obvious whether you should want bounds on the generic parameters or on the structural types. By adding #inner_ty: Trait, we're leaking that private type in a way that may not be desirable. I think we should probably only constrain the generic parameters, as the compiler does.

So far we've avoided this question because the newtype derivations are unconditional.

cuviper avatar Jul 05 '20 18:07 cuviper