Lifetime trouble implementing a trait for a function

I've created a trait for a parser, and I want to auto implement that trait for every function that matches the core function description. Not too hard, but unfortunately one of the parameters has a lifetime attached, (a wrapper around a Chars iterator)

pub type ParseRes<'a, V> = Result<(LCChars<'a>, V), ParseError>;

pub trait Parser<V>: Sized {
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, V>;
    //...
}

However when I create the auto implement:

impl<'a, V, F: Fn(&LCChars<'a>) -> ParseRes<'a, V>> Parser<V> for F {
    fn parse<'b>(&self, i: &LCChars<'b>) -> ParseRes<'b, V> {
        self(i)
    }
}

I can't make the "b"s match the "a"s to make the function work

If I make it all "a", then it complains that "a" is shaddowed.
If I get rid of the shaddow "a" abut leave it all as "a" then it doesn't match the trait.

Any ideas?

Thanks
Matt

Try for<'a>, search keyword is Higher Ranked Trait Bound(HRTB)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=35cc42c0bf6bafdfc86e2f458a6d2188

pub type ParseRes<'a, V> = Result<(LCChars<'a>, V), ParseError>;

pub trait Parser<V>: Sized {
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, V>;
    //...
}

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)
    }
}

Thanks That did it

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.