#Impl `From` for generic type

3 messages · Page 1 of 1 (latest)

simple galleon
#

I have a wrapper type and I want to implement From for it for all types that the inner type has From implemented for.

Unfortunately it seems that this conflicts with a blanket implementation from the standard library that implements From for itself.
Is there a way to work around this?

struct Outer<T> {
    inner: T
}

impl <E, T> From<E> for Outer<T> where T: From<E> {
    fn from(source: E) -> Self {
        Self {
          inner: T::from(source).into()
        }
    }      
}
error[E0119]: conflicting implementations of trait `std::convert::From<Outer<_>>` for type `Outer<_>`
 --> src/lib.rs:5:1
  |
5 | impl <E, T> From<E> for Outer<T> where T: From<E> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> From<T> for T;

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=struct Outer<T> { inner%3A T } impl <E%2C T> From<E> for Outer<T> where T%3A From<E> { fn from(source%3A E) -> Self { Self { inner%3A T%3A%3Afrom(source).into() } } }

Concretely I'm trying to copy this pattern (from the example) https://docs.rs/tracing-error/latest/tracing_error/struct.TracedError.html, but I want the Kind to be generic so that I don't have to copy paste the wrapper struct

willow zinc
#

no, you cannot do that

simple galleon
#

ok