OwningRef for arbitrary GATs?

⚓ Rust    📅 2025-08-27    👤 surdeus    👁️ 3      

surdeus

Suppose I have a trait like

trait Foo {
    type Bar<'a> where Self: 'a;
    fn barify(&self) -> Self::Bar<'_>;
}

Is it possible to have some equivalent to the owning_ref crate where one can package together a F: Foo with an F::Bar<'a> that borrows from it? One option would be to create a struct like

struct FooAndItsBar<F>
where F: Foo
{
    foo: Pin<Box<F>>,
    bar: F::Bar<'static>,
}

and carefully use mem::transmute to lengthen/shorten bars lifetime, however, this would require F: 'static (although I guess this implicitly assumes that the representation of F::Bar<'a> is identical regardless of 'a. Is that sound, at least pending specialization?)

Is there any way to do this that doesn't impose an F: 'static bound? Or is this unsound in principle?

3 posts - 3 participants

Read full topic

🏷️ Rust_feed