I am implementing FromStr
for a type and for the error I want to return the passed &str
inside a wrapper struct. However, this requires the the struct to have a lifetime bound for the &str
but that will cause errors inside FromStr
since its associative type Err
does not have any lifetime bounds and cannot be modified either. Lastly, it's not possible to add a lifetime bound to the impl
since the lifetime does not get constrained by anything, at least in the ways I have tried. So, how can I squeeze in my wrapper into the associative type?
For more context, here are the definitions:
pub enum RarityTag {
// ...
}
pub struct ParseRarityTagError<'a>(&'a str);
impl FromStr for RarityTag {
type Err = ParseRarityTagError<'a>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
// ...
rest => Err(ParseRarityTagError(rest)),
}
}
}
You cannot return an error with a lifetime bound to the string that FromStr::from_str
gives you. The trait was not designed for it. It would have to define Err
a generic associated type with a lifetime parameter in order to support that, but it doesn't.
You need either a different error scheme that doesn't rely on the lifetime or use an inherent method / free function and not the FromStr
trait.