Hi guys.
I defined an error enum:
pub enum MyError<'a> {
MissingField(&'a str),
}
and defined an struct:
pub struct SimpleMessageError<'a>(pub &'a str);
then, I implemented From
trait for SimpleMessageError
:
impl<'a: 'b, 'b> From<MyError<'b>> for SimpleMessageError<'a> {
fn from(e: MyError<'b>) -> Self {
match e {
MyError::MissingField(f) => SimpleMessageError(f),
}
}
}
but rustc
said:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/err.rs:102:15
|
102 | match e {
| ^
|
note: first, the lifetime cannot outlive the lifetime 'b as defined on the impl at 100:14...
--> src/err.rs:100:14
|
100 | impl<'a: 'b, 'b> From<MyError<'b>> for SimpleMessageError<'a> {
| ^^
= note: ...so that the types are compatible:
expected err::MyError<'_>
found err::MyError<'b>
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 100:6...
--> src/err.rs:100:6
|
100 | impl<'a: 'b, 'b> From<MyError<'b>> for SimpleMessageError<'a> {
| ^^
= note: ...so that the expression is assignable:
expected err::SimpleMessageError<'a>
found err::SimpleMessageError<'_>
although rustc gave some explaination, but I still can not figure out how to solve this problem?
or maybe this is a program design issue?
Once I changed lifetime 'a
and 'b
both to 'static
, compile error was gone.