I want a struct that holds a &str
. I have a trait
Parse
that wants to parse a &str
and return a T
.
I'm now lost in a twisty maze of lifetimes, all unlike. The basic issue is that my parse
method and my struct
both want to hold on to declared lifetimes, and I can provide no guarantees to the complier that the lifetimes are compatible. At the same time, I can't tell the complier that parse
want to return a borrow that lasts as long as the struct
.
pub trait Parse<T> {
fn parse<'a>(v: &'a str) -> Result<T,()>;
}
pub struct Field<'a> {
pub name: Fields,
pub value: &'a str
}
impl<'a> Parse<Field<'a>> for Field<'a> {
fn parse<'b>(v: &'b str) -> Result<Self,()> {
let fields: Vec<&'b str> = v.split(":").collect();
if fields.len() < 2 {
Err(())
} else {
Ok(Field { name: Fields::from(fields[0]),
value: fields[1]})
}
}
}
lib.rs:66:35: 66:44 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
lib.rs:66 value: fields[1]})
^~~~~~~~~
lib.rs:60:9: 68:10 help: consider using an explicit lifetime parameter as shown: fn parse(v: &'a str) -> Result<Self, MailError>
Note that the error message asks for an incorrect and invalid fix.
I've tried to cast the lifetime to 'a
, but that gives me an outlives error.