Library to safely and fallibly initialize pinned structs in-place.
It also allows in-place initialization of big structs that would otherwise produce a stack overflow.
When writing self referential data structures in Rust, one runs into the issue of initializing them. For example we will create an intrusive, doubly linked, circular list in Rust:
rust
pub struct ListHead {
next: NonNull<Self>,
prev: NonNull<Self>,
// ListHead is `!Unpin` because `next.prev = self`
_pin: PhantomPinned,
}
But now, how would one go about creating a ListHead
? A valid initial state of
a singular ListHead is, with next
and prev
pointing to self
. But in Rust
we cannot get a hold of self
until we have selected a value for next
!
This library provides the means to achieve this safely: ```rust use simplesafeinit::*;
pub struct ListHead {
next: NonNull!Unpin
because next.prev = self
_pin: PhantomPinned,
}
impl ListHead {
pub fn newinplace() -> impl PinInit
Insead of writing
new() -> Selfwe write
newinplace() -> impl PinInit. This function now
returns an in-place initializer. The
pin