Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Existing blanket impl From<T> for T makes real uses for generic types impossible #53282

Closed
mqudsi opened this issue Aug 12, 2018 · 0 comments
Closed

Comments

@mqudsi
Copy link
Contributor

mqudsi commented Aug 12, 2018

The blanket impl From<T> for T definition in core makes actual impls of std::convert::From for generic types extremely difficult.

Consider:

enum Subtype {
    Type1,
    Type2
}

struct Foo<T> where T: Subtype {
// ...
}

It is natural to wish to implement conversion between the subtypes, and to want to do so with as little repetition as possible. But

impl<Src: Subtype, Dst: Subtype> From<Foo<Src>> for Foo<Dst> {
...
}

is not possible:

   Compiling chrono v0.4.5 (file:///mnt/d/rand/chrono)
error[E0119]: conflicting implementations of trait `std::convert::From<datetime::DateTime<_>>` for type `datetime::DateTime<_>`:
   --> src/datetime.rs:238:1
    |
238 | impl<Src: TimeZone, Dst: TimeZone> From<DateTime<Src>> for DateTime<Dst> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: conflicting implementation in crate `core`:
            - impl<T> std::convert::From<T> for T;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0119`.
error: Could not compile `chrono`.

To learn more, run the command again with --verbose.
Makefile:17: recipe for target 'build' failed
make: *** [build] Error 101

Could the generic impl be made more specific so that it accounts for the fact that Foo<T1> and Foo<T2> are not the same, to permit user-defined conversion between them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant