Is it even possible to implement `Deref` that dereferences to a type with a lifetime?

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

surdeus

Warning

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

I have a situation that looks something like this:

#[derive(yoke::Yokeable)]
pub struct Parsed<'a> {
    // ..
}

pub struct Owned {
    inner: yoke::Yoke<Parsed<'static>, Arc<[u8]>>,
}

impl Deref for Owned {
    type Target = Parsed<?>;

    fn deref(&self) -> &Self::Target {
        self.inner.get()
    }
}

For those who are not familiar, yoke allows to create self-referential data structures. In this case parsing is a non-negligible operation, so I'm storing the results in the data structure alongside the buffer it was parsed from.

<?> is the problem here. I tried to use for<'a> Parsed<'a>, but compiler doesn't seem to understand this syntax in that position. The lifetime is the same as &self, but I wasn't able to find a way to express this in a way that compiler understands.

It is possible to do at on nightly or did I hit the corner that is not implementable today?
I'd be much more ergonomic to have this kind of Deref implemented (for now I have an explicit method for this purpose).

1 post - 1 participant

Read full topic

🏷️ rust_feed