field_project/
maybe_uninit.rs

1use core::mem::MaybeUninit;
2use crate::{Project, ProjectMut};
3
4impl<T> Project for MaybeUninit<T> {
5    type Base = T;
6    type Output<'a, Field: 'a> where Self: 'a = &'a MaybeUninit<Field>;
7
8    unsafe fn project<'a, Field>(&'a self, project_field: fn(*const Self::Base) -> *const Field) -> &'a MaybeUninit<Field> {
9        unsafe {
10            &*(project_field(self.as_ptr()) as *const MaybeUninit<_>)
11        }
12    }
13}
14
15impl<T> ProjectMut for MaybeUninit<T> {
16    type OutputMut<'a, Field: 'a> where Self: 'a = &'a mut MaybeUninit<Field>;
17
18    unsafe fn project_mut<'a, Field>(&'a mut self, project_field: fn(*mut Self::Base) -> *mut Field) -> Self::OutputMut<'a, Field> {
19        unsafe {
20            &mut *(project_field(self.as_mut_ptr()) as *mut MaybeUninit<_>)
21        }
22    }
23}