Can I impl a trait for all associated item of another trait?

For example, I define an custom Error in my crate so the function in my crate can return this Type using ? no matter how many kind of errors it actully produce inside. So I should impl Fom trait for my error type to make this work. Some error are come from std::str::parse(). Since there are many kind of errors return from parse(), I have to write code like this:

pub enum Error{
    IO(std::io::Error),
    Int(std::num::ParseIntError),
    Bool(std::str::ParseBoolError),
   // ...
}

impl From<std::num::ParseIntError> for Error { /* some code */}
impl From<std::str::ParseBoolError> for Error { /* some code */}
impl From<std::io::Error> for Error { /* some code */}

I'm not sure how many kind of error I may produce actually, and the failure detail is not important to me. So I wish I can do this:

pub enum Error {
    IO(std::io::Error),
    Parse(<std::str::FromStr::Err>)
}

impl From<std::str::FromStr::Err> for Error { /* ... */}

I tried some ways to do this but none of them work.

No but there are quite a few crates that can help generating the From impls. Remember that the associated type of a trait can be infinitely many things. Perhaps you should just use Box<dyn Error>?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.