Search Results

Found 1 results for "0a5aa801e16d849ebcc2783c6ecc45c9" across all boards searching md5.

Anonymous /g/105831578#105852901
7/10/2025, 1:16:56 AM
>>105848941
>>105849016
>>105850220
>>105851434
Oh, I just realized you don't even need unsafe lmao.
I present you: a cyclic data structure in Rust without unsafe, "the borrow checker" and smart pointers, and with zero overhead.

use std::ptr::NonNull;
use std::pin::Pin;
use std::mem::MaybeUninit;

pub struct SelfRef {
self_ref: NonNull<SelfRef>,
data: usize,
}

impl SelfRef {
pub fn new() -> Pin<Box<SelfRef>> {
let mut boxed = Box::new(MaybeUninit::<SelfRef>::uninit());
let self_ref = NonNull::new(boxed.as_mut_ptr()).unwrap();
let boxed = Box::write(boxed, SelfRef { self_ref, data: 0 });
Box::into_pin(boxed)
}
}