Conflicting implementations

It's pinned but it gets unpinned whenever you take a look at it. An easy way to find it is to Search -> Only return topics/posts -> dropdown -> are pinned.


To your actual question:

There is a question which will determine how to fix this: do you intend on using the capabilities of std::error::Error?

  • If so, you can leverage the fact that stdlib does exactly what you're trying to do and bounce off of that: impl one and impl two.
    pub struct FooError<'a> {
        error: Box<dyn ErrorTrait + 'a>,
    }
    
    impl<'a, T: 'a> From<T> for FooError 
        where Box<dyn ErrorTrait + 'a>: From<T> {
        fn from(x: T) -> Self {
            Self { error: x.into() }
        }
    }
    
  • If not, then you can rely on the fact that any T: Error must also be Display, and just work off of that:
    // Your original FooError definition
    impl<T: Display> From<T> for FooError {
        fn from(x: T) -> Self {
            Self { error: format!("{}", x) }
        }
    }
    
    Note, however, that this will work for anything which is display.

One thing which might work is to combine the approaches:

// Your original FooError definition
impl<'a, T: 'a> From<T> for FooError 
    where Box<dyn ErrorTrait + 'a>: From<T> {
    fn from(x: T) -> Self {
        Self {
            error: format!("{:?}", x.into())
        }
    }
}

However if you're performing this from call very often, it might be slightly slower, due to the use of trait objects.


No, by design. In theory everyone follows semver, and it's not a semver-breaking change to implement a trait (usually, ignoring special things like !Send [when dealing with multithreaded code] and Drop [when dealing with unions]).