# Is it possible to implement TryFrom with a generic type?

**URL:** <https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658>\
**Category:** help\
**Created:** [June 12, 2026, 2:18am UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658 "2026-06-12T02:18:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezcore](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pezcore/32/37480_2.png) [@pezcore](https://users.rust-lang.org/u/pezcore)\
**Post date:** [June 12, 2026, 2:18am UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/1 "2026-06-12T02:18:34Z")

</div>

Is it possible to implement `TryFrom<T>` for a generic type `T` like this?

```rust
#[derive(Debug)]
struct A;

impl<T> TryFrom<T> for A {
    type Error = Infallible;
    fn try_from(_input: T) -> Result<Self, Self::Error> { Ok(A) }
}

```

([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=62971e59a21b1cb36de4c98be9e2d0d6))

Attempting to compile this as-is produces the following compile error:

```rust
error[E0119]: conflicting implementations of trait `TryFrom<_>` for type `A`
 --> src/lib.rs:7:1
  |
7 | impl<T> TryFrom<T> for A {
  | ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T, U> TryFrom<U> for T
            where U: Into<T>;

```

My current understanding is that it's not possible to implement `TryFrom<T>` using a generic type parameter `T` like this because this `impl` means that for any and every type `T` that could possibly exist, `TryFrom<T>` is implemented for `A`. Some of these types `T` might also implement `Into<A>`and for those that do, an implementation of `TryFrom<T> for A` is already provided by the blanket impl defined in `std`: [`impl<T, U> TryFrom<U> for T where U: Into<T>`](https://doc.rust-lang.org/std/convert/trait.TryFrom.html#impl-TryFrom%3CU%3E-for-T).

Is this explanation correct? And if so, why isn't it possible to exclude all types `T` which might implement `Into<A>` using a trait bound like this?

```rust
trait MyTrait;
struct A;

impl<T: MyTrait> TryFrom<T> for A {
    type Error = Infallible;
    fn try_from(_input: T) -> Result<Self, Self::Error> { Ok(A) }
}

```

It seems to me that since `MyTrait` is a private trait, only types defined the same crate can implement it, and therefore `impl<T: MyTrait> TryFrom<T> for A` only applies to a finite set of types `T` which can all be proven not to implement `Into<A>` and therefore not introduce a trait impl conflict via the blanket impl. If this is not possible why not?

---

<div class="post-metadata">

**Author:** ![consistent-milk12](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/consistent-milk12/32/50324_2.png) [@consistent-milk12](https://users.rust-lang.org/u/consistent-milk12)\
**Post date:** [June 12, 2026, 2:44am UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/2 "2026-06-12T02:44:37Z")

</div>

It's because of the coherence checking. Going through this may help:

[https://rustc-dev-guide.rust-lang.org/coherence.html](https://rustc-dev-guide.rust-lang.org/coherence.html)

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kpreid/32/22420_2.png) [@kpreid](https://users.rust-lang.org/u/kpreid)\
**Post date:** [June 12, 2026, 3:33am UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/3 "2026-06-12T03:33:11Z")

</div>

> [@pezcore](#):
>
> ```rust
> trait MyTrait;
> struct A;
> 
> impl<T: MyTrait> TryFrom<T> for A {
> type Error = Infallible;
> fn try_from(_input: T) -> Result<Self, Self::Error> { Ok(A) }
> }
> 
> ```
> 
> It seems to me that since `MyTrait` is a private trait, only types defined the same crate can implement it, and therefore `impl<T: MyTrait> TryFrom<T> for A` only applies to a finite set of types `T` which can all be proven not to implement `Into<A>`

`A` implements `From<A>` via `From`’s blanket implementation, and therefore the standard library implements `TryFrom<A> for A`, ~~which overlaps with your implementation when `T = A`. Therefore, your implementation _does_ overlap and even a perfect compiler must not allow it.~~ (Update: this is incorrect, because there is no `impl MyTrait for A`.)

(If Rust had “not equals” bounds on type parameters, then you could require `T != A`, but this has the same kinds of issues as specialization.)

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [June 12, 2026, 5:06am UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/4 "2026-06-12T05:06:08Z")

</div>

> [@pezcore](#):
>
> It seems to me that since `MyTrait` is a private trait, only types defined the same crate can implement it

It would be convenient if there was a mechanism for "all `T` local to this crate". (Those are allowed if there is no actual overlap.)

In the meanwhile, if that's truly all you need, a macro can lighten the tedium of an implementation per local type.

---

<div class="post-metadata">

**Author:** ![pezcore](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pezcore/32/37480_2.png) [@pezcore](https://users.rust-lang.org/u/pezcore)\
**Post date:** [June 12, 2026, 2:26pm UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/5 "2026-06-12T14:26:21Z")

</div>

> [@kpreid](#):
>
> `A` implements `From<A>` via `From`’s blanket implementation, and therefore the standard library implements `TryFrom<A> for A`, which overlaps with your implementation when `T = A`. Therefore, your implementation _does_ overlap and even a perfect compiler must not allow it.

True, but `A` does not implement `MyTrait`, so shouldn't `A` be excluded from the possible types of `T` via the trait bound `T: MyTrait`? If the entire crate was just

```rust
trait MyTrait {}
struct A;
struct B;
impl MyTrait for B {}

impl<T: MyTrait> TryFrom<T> for A {
    type Error = Infallible;
    fn try_from(_input: T) -> Result<Self, Self::Error> { Ok(A) }
}

```

Wouldn't `B` be the only possible type in the universe that could match the generic type parameter `T` in the `impl<T: MyTrait> TryFrom<T> for A`? If `B` is the only possible `T` and `B` does not implement `Into<A>`, it seems that there should be no trait impl conflict with the blanket impl from `std`.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kpreid/32/22420_2.png) [@kpreid](https://users.rust-lang.org/u/kpreid)\
**Post date:** [June 12, 2026, 2:44pm UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/6 "2026-06-12T14:44:11Z")

</div>

> [@pezcore](#):
>
> True, but `A` does not implement `MyTrait`, so shouldn't `A` be excluded from the possible types of `T` via the trait bound `T: MyTrait`?

I’m sorry, I missed that. My previous post is incorrect, then.

---

<div class="post-metadata">

**Author:** ![pezcore](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pezcore/32/37480_2.png) [@pezcore](https://users.rust-lang.org/u/pezcore)\
**Post date:** [June 12, 2026, 5:05pm UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/7 "2026-06-12T17:05:29Z")

</div>

Hmmm. When I try to compile this:

```rust
use std::convert::Infallible;

trait MyTrait {}
pub struct A;
pub struct B;
impl MyTrait for B {}

impl<T: MyTrait> TryFrom<T> for A {
    type Error = Infallible;
    fn try_from(_input: T) -> Result<Self, Self::Error> { Ok(A) }
}

```

([Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=062a5efa52a3b8c0b6932ff25d234eb4))

I still get the conflict with the blanket impl from `std` (`impl<T, U> TryFrom<U> for T where U: Into<T>`). Does anyone know why restricting the type of `T` in with the trait bound `: MyTrait` in `impl<T: MyTrait> TryFrom<T> for A` is not sufficient to satisfy the coherence checker? The way I see it, the only type that could ever match `T` is `B` which does not implement `Into<A>`, so I'm not seeing where the trait impl conflict is coming from.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [June 12, 2026, 6:02pm UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/8 "2026-06-12T18:02:39Z")

</div>

> [@pezcore](#):
>
> Does anyone know why restricting the type of `T` in with the trait bound `: MyTrait` in `impl<T: MyTrait> TryFrom<T> for A` is not sufficient to satisfy the coherence checker?

Because privacy is not considered for coherence; there is no mechanism for "all `T` local to this crate".

---

<div class="post-metadata">

**Author:** ![pezcore](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pezcore/32/37480_2.png) [@pezcore](https://users.rust-lang.org/u/pezcore)\
**Post date:** [June 13, 2026, 2:08am UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/9 "2026-06-13T02:08:50Z")

</div>

hmmm I see. So does that make `impl<T: AnyBound> TryFrom<T> for LocalType` impossible?

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [June 14, 2026, 8:45pm UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/10 "2026-06-14T20:45:29Z")

</div>

There's no way to do that in the language currently, correct.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 12, 2026, 8:46pm UTC](https://users.rust-lang.org/t/is-it-possible-to-implement-tryfrom-with-a-generic-type/140658/11 "2026-09-12T20:46:04Z")

</div>

Closed on September 12, 2026, 8:46pm UTC

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.
