I have a trait:
pub trait Parser<V>: Sized {
fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, V>;
}
It's implement for several things such a strings and chars and functions that fit a pattern:
impl<V, F: for<'a> Fn(&LCChars<'a>) -> ParseRes<'a, V>> Parser<V> for F {
fn parse<'b>(&self, i: &LCChars<'b>) -> ParseRes<'b, V> {
self(i)
}
}
I really want to make the parser able to return a borrowwed str with the lifetime associated to the lifetime of the LCCHars (a wrapper around std::str::CharIndices).
Here is one attempl
pub struct AsStr<P: Parser<V>, V> {
p: P,
phv: PhantomData<V>,
}
impl<P: Parser<V>, V> Parser<&str> for AsStr<P, V> {
// the lifetime of ^^^ needs to match LCChars<'a>
fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, &'a str> {
//run the parser ignore the result
let (it2, _) = self.p.parse(it)?;
//match against the index before and the index after
match (it.index(), it2.index()) {
(Some(s), Some(f)) => Ok((it2, &it.as_str()[0..(f - s)])),
_ => Ok((it2, &it.as_str())),
}
}
}
Currently the error is that the inner function does not match the trait.
If I remove the 'a from the ParseRes : eg ParseRes<'a,&str>
then there is alifetime missmatch with the function.
The str result should have a lifetime tied to the str that LCChars holds a reference to. And is only a parameter of the function call, This function could be caled many times with different LCChars, so it makes little sense to tie the lifetime to the actual parser.
Any ideas?