If you have a raw pointer to a struct like *mut Range<_>, such as one might obtain from MaybeUininit::<Range<_>>::as_mut_ptr then we incur an unsafe operations when projecting from the raw pointer to a field:
unsafe fn partial_default(self: &mut MaybeUninit<Self>) {
let self = self.as_mut_ptr();
let start = 0;
let start_ptr = unsafe { &mut (*self).start as *mut T }; // Maybe avoidable unsafe?
unsafe { core::ptr::write(start_ptr,start) }; // Unavoidable unsafe
}
Are there proc macro that facilitate this? Should rustc provide an easier method somehow? Could self.start be given type *mut T here? Or is projection too risky somehow?
Calling this when the content is not yet fully initialized causes undefined behavior
unsafe { &mut (*self).start as *mut T };
There's a better way to do this now, with std:::ptr::addr_of_mut. That line of code you wrote could be UB for uninitialized fields, I don't know if that's been decided yet.