facet_core/impls_core/
smartptr.rs

1use crate::{
2    Def, Facet, KnownSmartPointer, PtrConst, SmartPointerDef, SmartPointerFlags,
3    SmartPointerVTable, value_vtable,
4};
5
6unsafe impl<'a, T: Facet<'a>> Facet<'a> for core::ptr::NonNull<T> {
7    const SHAPE: &'static crate::Shape = &const {
8        crate::Shape::builder_for_sized::<Self>()
9            .type_params(&[crate::TypeParam {
10                name: "T",
11                shape: || T::SHAPE,
12            }])
13            .def(Def::SmartPointer(
14                SmartPointerDef::builder()
15                    .pointee(T::SHAPE)
16                    .flags(SmartPointerFlags::EMPTY)
17                    .known(KnownSmartPointer::NonNull)
18                    .vtable(
19                        &const {
20                            SmartPointerVTable::builder()
21                                .borrow_fn(|this| {
22                                    let ptr = unsafe { this.get::<Self>().as_ptr() };
23                                    PtrConst::new(ptr)
24                                })
25                                .new_into_fn(|this, ptr| {
26                                    let ptr = unsafe { ptr.read::<*mut T>() };
27                                    let non_null =
28                                        unsafe { core::ptr::NonNull::new_unchecked(ptr) };
29                                    unsafe { this.put(non_null) }
30                                })
31                                .build()
32                        },
33                    )
34                    .build(),
35            ))
36            .vtable(
37                &const { value_vtable!(core::ptr::NonNull<T>, |f, _opts| write!(f, "NonNull")) },
38            )
39            .build()
40    };
41}