derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

For generic types, allow to specify concrete types for which to derive for

Open glandium opened this issue 6 years ago • 4 comments

I'm not sure this would be necessary for all traits, but I've had enough trouble with at least From and Display vs. generics that it might be useful for at least those two.

My immediate problem is with From. I have a generic enum, that uses associated types for its variants. e.g.:

enum Foo<T: Trait> {
    A(T::Type),
}

The compiler won't let #[derive(From)] work because of the conflict with impl From<T> for T (because something downstream could impl Trait for S { type Type = Foo }). The compiler doesn't care about what bounds you place on Trait::Type.

TTBOMK, there are two work arounds for this:

  • Wrap T::Type in the enum variant, so that we end up with impl<T> From<Wrap<T::Type>>
  • Define impl From<<concrete-type as Trait>::Type> for a set of defined concrete types, which in my case would work out because I actually only mean that enum to be used with well defined types.

It would be nice if derive_more allowed the latter, but in the meanwhile I'll go with the former anyways.

glandium avatar Mar 19 '18 02:03 glandium

Thanks for the bugreport, I have not used the deriving From with associated types before so it's definitely possible that it's broken. Could you run cargo expand and paste it's output in the issue, so I can see what code is generated? After that could you show the desired implementation?

JelteF avatar Mar 19 '18 09:03 JelteF

See this playpen: https://play.rust-lang.org/?gist=3c29ab10036a77bca20e3b1517fd3eaf&version=stable #[derive(From)] generates generic impls like the one in there.

glandium avatar Mar 19 '18 12:03 glandium

And what does a working implementation look like?

JelteF avatar Mar 19 '18 13:03 JelteF

Need some concrete types, like so: https://play.rust-lang.org/?gist=c03729162b837b27842233cc98162003&version=stable

Please note that in the end, I opted to not use trait associated types at all, so it's not going to be a problem for me anymore.

glandium avatar Mar 20 '18 00:03 glandium