I'm reading You Can't Do That: Abstracting over Ownership in Rust with Higher-Rank Type Bounds. Or Can You? | Armin Ronacher's Thoughts and Writings
For the following trait impl
impl<'a> TryConvertValue for &'a str {
fn try_convert_value(value: &'a Value) -> Option<&'a str> {
match value {
Value::String(s) => Some(s),
_ => None,
}
}
}
the rustc says it only impl for a specific lifetime 'a and impl trait for &str
is impling for any lifetime.
Following is the error from the blog
error: implementation of `TryConvertValue` is not general enough
--> src/main.rs:21:20
|
21 | let to_upper = ArgCallback::new(|a: &str| Value::String(a.to_uppercase()));
| ^^^^^^^^^^^^^^^^ implementation of `TryConvertValue` is not general enough
|
= note: `TryConvertValue<'0>` would have to be implemented for the type `&str`, for any lifetime `'0`...
= note: ...but `TryConvertValue<'1>` is actually implemented for the type `&'1 str`, for some specific lifetime
I understand that impl trait for &str
is for all lifetimes as there is no lifetime constraint.
But I don't get it why the impl<'a> TryConvertValue for &'a str
is not for all lifetimes.
Apparently, no matter which lifetime 'a is, the impl is valid, i.e, for any lifetime 'a, I can call the func &str.try_convert_value()
to get a `Option<&str>.
The code is in the reply
Appreciate any help to clear my doubt!