Associated type accepting anything implementing `Into<StdError>`

How can I express "anything that converts into StdError" for the associated type?

trait MyTrait {
    // I can't use just this, as `anyhow::Error` doesn't implement `StdError`
    type Error: std::error::Error;

    // so I'm looking for something like this
    type Error: Into<StdError> // the trait `Sized` is not implemented for `(dyn StdError + 'static)`
}

Well nothing can be converted into an StdError as it is a trait, not a type. Are you perhaps looking for things convertible into Box<dyn Error> or Box<dyn Error + Send + Sync>?

Alternatively, consider if you need any restrictions at all? You often don't.

1 Like

Thanks, Into<Box<dyn std::error::Error>> will do in my case (I need the restriction to have access to .source())

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.