3 ms·
I think they should just implement position-independent borrows. So instead of the borrow being an absolute pointer that gets broken if you move the self-borrow
by IshKebab 1y ago
I think they should just implement position-independent borrows. So instead of the borrow being an absolute pointer that gets broken if you move the self-borrowing struct, you can move it just fine.
Yes it would add like one extra add to every access, but you hardly ever need self-borrows so I think it's probably an acceptable cost in most cases.
- tux3 1y agoSay I have this type: struct A { raw_data: Vec<u8>, parsed_data: B<&pie raw_data>, parsed_data2: B<&pie raw_data> } struct B<T> { foo: &pie T [u8], } Ignoring that my made up notation doesn't make much sense, is the idea that B.foo would be an offset relative to its own adress? So B.method(&self) might do addr(&self.foo) + self.foo, which is stable even if the parent struct A and its raw data field moves? Then I wonder how to handle the case where the relative &pie reference itself moves. Maybe parsed_data is std::mem::replaced with parsed_data2 (or maybe one of them is an Option<B> and we Option.take() it somewhere else.)
- SkiFire13 1y agoThis has been proposed at the time, but it doesn't work for the case where the borrow points to stable memory (e.g. a `&str` pointing to the contents of a `String` in the same struct). In general case a reference might point to either stable or unstable memory at runtime, so there's no way to make this always work (e.g. in async functions)
- IshKebab 1y agoGood point.