Hi,
I’ve just run into an issue with thiserror. It may just be my inexperience with Rust’s lifetimes, though. The issue occurred when I was trying to use the #[from] attribute on an error that requires a lifetime specifier. I managed to create a minimal working example:
fn main() -> Result<(), Error<'static>> {
Err(Error::SubError(SubError::Variant("some text")))
}
#[derive(Debug, thiserror::Error)]
enum Error<'a> {
#[error("sub error")]
SubError(#[from] SubError<'a>),
}
#[derive(Debug, thiserror::Error)]
enum SubError<'a> {
#[error("variant: {0}")]
Variant(&'a str),
}
This gives the following compilation error:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:5:17
|
5 | #[derive(Debug, thiserror::Error)]
| ^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 6:12...
--> src/main.rs:6:12
|
6 | enum Error<'a> {
| ^^
note: ...so that the types are compatible
--> src/main.rs:5:17
|
5 | #[derive(Debug, thiserror::Error)]
| ^^^^^^^^^^^^^^^^
= note: expected `&Error<'_>`
found `&Error<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `SubError<'_>` will meet its required lifetime bounds
--> src/main.rs:5:17
|
5 | #[derive(Debug, thiserror::Error)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error
Any ideas as to how this could be solved?
Thanks for your work on anyhow and thiserror; IMHO they are the best the Rust ecosystem has to offer in terms of error handling :)
Hi,
I’ve just run into an issue with thiserror. It may just be my inexperience with Rust’s lifetimes, though. The issue occurred when I was trying to use the
#[from]attribute on an error that requires a lifetime specifier. I managed to create a minimal working example:This gives the following compilation error:
Any ideas as to how this could be solved?
Thanks for your work on anyhow and thiserror; IMHO they are the best the Rust ecosystem has to offer in terms of error handling :)