Lifetime bound on HRTB in trait bounds?

⚓ Rust    📅 2025-06-21    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 52 days ago. The information described in this article may have changed.

I'm writing a parser that accepts both slices (which have 'src sub-slices that can pass out of parser) and buffers (which have 'tmp sub-slices that can not pass out of parser, e.g. an IO wrapper that discards previous bytes), encountered lifetime issues again... Here's the simplified code:

pub trait Foo<'src> {
    type View<'tmp>
    where 'src: 'tmp;
}

pub trait Bar<'src, F: Foo<'src>> {
    type Viewed<'tmp>
    where 'src: 'tmp;
}

impl<'src, F> Bar<'src, F> for ()
where                       // ^^ error[E0478]: lifetime bound not satisfied
    F: for<'tmp> Foo<'src, View<'tmp> = &'tmp str>,
    // How can I tell rustc `'src: 'tmp` there?
{
    type Viewed<'tmp> = F::View<'tmp>
    where 'src: 'tmp;
}

Add 'src: 'tmp bound on HRTB seems the only way to get this code compile. Is there any way to do this currently?

1 post - 1 participant

Read full topic

🏷️ rust_feed