Conflicting implementation when trying to impl TryFrom<T>

Hi everyone,

I'm trying to compile the code at play.rust-lang.org.

The snippet should contain two trait impls for a struct S,

  • impl<T: BufRead> TryFrom for S
  • impl<T: BufRead> From for S

The second impl compiles, whereas the first does not.
The error message

error[E0119]: conflicting implementations of trait `std::convert::TryFrom<_>` for type `S`

isn't helping much, to be honest.

I tried to reduce the snippet as much as possible.
Can you help my understand what is wrong here?

The issue is that TryFrom<T> already has a blanket implementation for any type that implements From<T>. Just removing the implementation for TryFrom works (playground).

Ok, get it.

If I remove the impl From I still get the same error.
See play.rust-lang.org

use std::convert::TryFrom;
use std::io::{self, BufRead};

struct S;

impl<T: BufRead> TryFrom<T> for S {
    type Error = ();
    fn try_from(_value: T) -> Result<Self, Self::Error> {
        Ok(S)
    }
}


fn main() {
    let _s1 = S::try_from(io::empty());
}

Edit: This seems to be this issue and specifically this blanket impl

impl<T, U> TryFrom<U> for T where U: Into<T> {
    type Error = Infallible;

    fn try_from(value: U) -> Result<Self, Self::Error> {
        Ok(U::into(value))
    }
}

Could you explain why this blanket impl applies here? Specifically I don't see where the where U: Into<T> applies to my struct S .

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.