Generic source compatible with anyhow (or similar)
nthuemmel opened this issue · comments
Sometimes, it may make sense to compose an error enum of generic sources. This is easily possible by using std::error::Error + 'static
trait bounds:
#[derive(Debug, Error)]
pub enum FetchError<
L: std::error::Error + 'static,
S: std::error::Error + 'static,
> {
#[error(transparent)]
Load(L),
#[error("Failed to save new value")]
Save(#[source] S),
}
This supports errors which implement std::error::Error
directly, however, it can't be used when the sources are, say, an anyhow::Error
:
FetchError<anyhow::Error, anyhow::Error>> =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StdError` is not implemented for `anyhow::Error`
I have seen that thiserror internally uses thiserror::private::AsDynError<'a>
, but that's (a) a private API and (b) I wouldn't know what to supply as a lifetime parameter when using this as a trait bound.
Could you help me find a way to support both implementations of StdError
as well as anyhow::Error
(or other types that dereference to a dyn StdError
) as generic arguments?
Would it be possible for thiserror
to simplify these trait bounds in some way?
Hi @nthuemmel, sorry that no one was able to provide guidance here. If this is still an issue, you could try taking this question to any of the resources shown in https://www.rust-lang.org/community.