Borrowing deref target of field

Hello, I want to ask, is there any chance something like that might get implemented in the future?

struct Foo {
    bar: Vec<u8>,
    baz: &'? [u8],
}

Here baz borrows from bar, but not directly but into allocated buffer, so Foo is still Unpin. That pattern is actually already implemented in yoke, and it looks like that:

struct Foo {
    bar_baz: Yoke<&'static [u8], Vec<u8>>,
}

Where 'static represents erased lifetime, like dyn Trait represents erased type. It works well, but I cannot use it anywhere I want to because the design and goals of maintainers are focused on zero copy deserialization etc, so they do not support mutable references. So it's impossible to get something like that working with yoke:

struct Foo {
    bar: Vec<u8>,
    baz: &'? mut [u8],
}

I don't really see something that really contradicts it - drop order could be managed by compiler, it also might hypothetically store that relationship and prohibit & or &mut access to bar, depending on the kind of borrow baz has etc

1 Like

This is called "self-referential struct". It's on a wishlist to support maybe someday, after the borrow checker is rewritten. Currently this is not allowed.

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