facet_core/impls/alloc/
boxed.rs

1use core::ptr::NonNull;
2
3use alloc::boxed::Box;
4
5use crate::{
6    Def, Facet, KnownPointer, PointerDef, PointerFlags, PointerVTable, PtrConst, PtrMut, PtrUninit,
7    Shape, ShapeBuilder, TryFromError, Type, TypeNameFn, TypeNameOpts, TypeOpsIndirect, UserType,
8    VTableIndirect,
9};
10
11// Named function for try_from
12unsafe fn try_from(
13    src_ptr: PtrConst,
14    src_shape: &'static Shape,
15    dst: PtrUninit,
16) -> Result<PtrMut, TryFromError> {
17    let layout = src_shape.layout.sized_layout().unwrap();
18
19    unsafe {
20        let alloc = alloc::alloc::alloc(layout);
21        if alloc.is_null() {
22            alloc::alloc::handle_alloc_error(layout);
23        }
24
25        let src_ptr = src_ptr.as_ptr::<u8>();
26        core::ptr::copy_nonoverlapping(src_ptr, alloc, layout.size());
27
28        // layout of Box<T> == *mut T == *mut u8
29        Ok(dst.put(alloc))
30    }
31}
32
33// Named function for borrow_fn
34unsafe fn borrow_fn<'a, T: ?Sized + Facet<'a>>(this: PtrConst) -> PtrConst {
35    unsafe {
36        let concrete = this.get::<Box<T>>();
37        let t: &T = concrete.as_ref();
38        PtrConst::new(NonNull::from(t).as_ptr())
39    }
40}
41
42// Named function for new_into_fn
43unsafe fn new_into_fn<'a, 'src, T: ?Sized + Facet<'a>>(this: PtrUninit, ptr: PtrMut) -> PtrMut {
44    unsafe { try_from(ptr.as_const(), T::SHAPE, this).unwrap() }
45}
46
47unsafe impl<'a, T: ?Sized + Facet<'a>> Facet<'a> for Box<T> {
48    const SHAPE: &'static crate::Shape = &const {
49        const fn build_type_name<'a, T: ?Sized + Facet<'a>>() -> TypeNameFn {
50            fn type_name_impl<'a, T: ?Sized + Facet<'a>>(
51                _shape: &'static crate::Shape,
52                f: &mut core::fmt::Formatter<'_>,
53                opts: TypeNameOpts,
54            ) -> core::fmt::Result {
55                write!(f, "Box")?;
56                if let Some(opts) = opts.for_children() {
57                    write!(f, "<")?;
58                    T::SHAPE.write_type_name(f, opts)?;
59                    write!(f, ">")?;
60                } else {
61                    write!(f, "<…>")?;
62                }
63                Ok(())
64            }
65            type_name_impl::<T>
66        }
67
68        ShapeBuilder::for_sized::<Self>("Box")
69            .type_name(build_type_name::<T>())
70            .vtable_indirect(&VTableIndirect::EMPTY)
71            .type_ops_indirect(
72                &const {
73                    unsafe fn drop_in_place<'a, T: ?Sized + Facet<'a>>(ox: crate::OxPtrMut) {
74                        unsafe {
75                            core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<T>>() as *mut Box<T>)
76                        };
77                    }
78                    TypeOpsIndirect {
79                        drop_in_place: drop_in_place::<T>,
80                        default_in_place: None,
81                        clone_into: None,
82                        is_truthy: None,
83                    }
84                },
85            )
86            .ty(Type::User(UserType::Opaque))
87            .def(Def::Pointer(PointerDef {
88                vtable: &const {
89                    PointerVTable {
90                        borrow_fn: Some(borrow_fn::<T>),
91                        new_into_fn: if size_of::<*const T>() == size_of::<*const ()>() {
92                            Some(new_into_fn::<T>)
93                        } else {
94                            None
95                        },
96                        ..PointerVTable::new()
97                    }
98                },
99                pointee: Some(T::SHAPE),
100                weak: None,
101                strong: None,
102                flags: PointerFlags::EMPTY,
103                known: Some(KnownPointer::Box),
104            }))
105            .type_params(&[crate::TypeParam {
106                name: "T",
107                shape: T::SHAPE,
108            }])
109            .inner(T::SHAPE)
110            // Box<T> propagates T's variance
111            .variance(Shape::computed_variance)
112            .build()
113    };
114}
115
116#[cfg(test)]
117mod tests {
118    use core::mem::ManuallyDrop;
119
120    use alloc::boxed::Box;
121    use alloc::string::String;
122
123    use super::*;
124
125    #[test]
126    fn test_box_type_params() {
127        let [type_param_1] = <Box<i32>>::SHAPE.type_params else {
128            panic!("Box<T> should only have 1 type param")
129        };
130        assert_eq!(type_param_1.shape(), i32::SHAPE);
131    }
132
133    #[test]
134    fn test_box_vtable_1_new_borrow_drop() {
135        facet_testhelpers::setup();
136
137        let box_shape = <Box<String>>::SHAPE;
138        let box_def = box_shape
139            .def
140            .into_pointer()
141            .expect("Box<T> should have a smart pointer definition");
142
143        // Allocate memory for the Box
144        let box_uninit_ptr = box_shape.allocate().unwrap();
145
146        // Get the function pointer for creating a new Box from a value
147        let new_into_fn = box_def
148            .vtable
149            .new_into_fn
150            .expect("Box<T> should have new_into_fn");
151
152        // Create the value and initialize the Box
153        let mut value = ManuallyDrop::new(String::from("example"));
154        let box_ptr = unsafe {
155            new_into_fn(
156                box_uninit_ptr,
157                PtrMut::new(NonNull::from(&mut value).as_ptr()),
158            )
159        };
160        // The value now belongs to the Box, prevent its drop
161
162        // Get the function pointer for borrowing the inner value
163        let borrow_fn = box_def
164            .vtable
165            .borrow_fn
166            .expect("Box<T> should have borrow_fn");
167
168        // Borrow the inner value and check it
169        let borrowed_ptr = unsafe { borrow_fn(box_ptr.as_const()) };
170        // SAFETY: borrowed_ptr points to a valid String within the Box
171        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
172
173        // Drop the Box in place
174        // SAFETY: box_ptr points to a valid Box<String>
175        unsafe {
176            box_shape
177                .call_drop_in_place(box_ptr)
178                .expect("Box<T> should have drop_in_place");
179        }
180
181        // Deallocate the memory
182        // SAFETY: box_ptr was allocated by box_shape and is now dropped (but memory is still valid)
183        unsafe { box_shape.deallocate_mut(box_ptr).unwrap() };
184    }
185}