The web (and this forum) are full of questions and discussions related to the difficulty to create self-referential structs in Rust. There are crates to ease such creations, but seem to all predate the stabilisation of Pin
and do not seem to be very actively maintained.
So my question is whether there is a plan to add minimal support for self-referential structures using Pin
in the near future. I imagine a world where this would be valid Rust code:
struct S<'l> {
value: u32,
value_ref: RefCell<Option<&'l u32>>,
_pin: PhantomPinned
}
impl<'l> S<'l> {
fn do_something(&'l self) {
*self.value_ref.borrow_mut() = Some(&self.value);
}
}
fn create_s<'l>() -> Pin<Box<S<'l>>> {
let s = Box::pin(S {
value: 1,
value_ref: RefCell::new(None),
_pin: PhantomPinned
});
s.do_something();
s
}
Currently, the line s.do_something();
creates a borrow that prevents the move of s
at the next line. However, I can imagine there might a way for the compiler to reason that, as the instance of S
referred by s
will never ever move, calling that s.do_something();
is valid.
Should the compiler accept that, we could very easily create trees and graphs without unsafe code, just using one of the many arena crates (such as bumpalo). Currently, that only works if the reference to the self-referential structure is never moved. This constrains a lot the architectural design of an application, not mentioning libraries.