Skip to content

Blanket impl resolution vs. trait bounds #58904

Closed
@jcape

Description

@jcape

I have run into similar errors to #50133, so that may share the same ultimate cause as mine.

I'm having an issue with the impl<T> From<T> for T blanket implementation causing conflicts with my own blanket implementation of a From trait. For example, if I have a type which implements AsRef<u8>, and From<T: AsRef<[u8]>>, that blanket implementation conflicts, because my implementation of the trait and the trait-bound-from obviously provide a different path for T::from(T). I think this is wrong, though in my specific case I have worked around it by doing From<&'bytes [u8]>, but wrong is wrong :-)

Some sample code:

struct MyType(Vec<u8>);

impl AsRef<[u8]> for MyType {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl<B: AsRef<[u8]> From<B> for MyType {
    fn from(src: B) -> Self {
        Self(Vec::from(src))
    }
}

Resulting error:

error[E0119]: conflicting implementations of trait `std::convert::From<MyType>` for type `MyType`:
  --> src/main.rs:10:1
   |
10 | impl<B: AsRef<[u8]>> From<B> for MyType {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::convert::From<T> for T;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions