facet_core/impls/alloc/
boxed.rs

1use alloc::alloc::Layout;
2use alloc::vec::Vec;
3use core::ptr::NonNull;
4
5use alloc::boxed::Box;
6
7use crate::{
8    Def, Facet, KnownPointer, OxPtrMut, PointerDef, PointerFlags, PointerVTable, PtrConst, PtrMut,
9    PtrUninit, Shape, ShapeBuilder, SliceBuilderVTable, TryFromError, Type, TypeNameFn,
10    TypeNameOpts, TypeOpsIndirect, UserType, VTableIndirect,
11};
12
13// Named function for try_from
14unsafe fn try_from(
15    src_ptr: PtrConst,
16    src_shape: &'static Shape,
17    dst: PtrUninit,
18) -> Result<PtrMut, TryFromError> {
19    let layout = src_shape.layout.sized_layout().unwrap();
20
21    unsafe {
22        let alloc = alloc::alloc::alloc(layout);
23        if alloc.is_null() {
24            alloc::alloc::handle_alloc_error(layout);
25        }
26
27        let src_ptr = src_ptr.as_ptr::<u8>();
28        core::ptr::copy_nonoverlapping(src_ptr, alloc, layout.size());
29
30        // layout of Box<T> == *mut T == *mut u8
31        Ok(dst.put(alloc))
32    }
33}
34
35// Named function for borrow_fn (sized types)
36unsafe fn borrow_fn<'a, T: Facet<'a>>(this: PtrConst) -> PtrConst {
37    unsafe {
38        let concrete = this.get::<Box<T>>();
39        let t: &T = concrete.as_ref();
40        PtrConst::new(NonNull::from(t).as_ptr() as *const u8)
41    }
42}
43
44// Named function for new_into_fn (sized types)
45unsafe fn new_into_fn<'a, 'src, T: Facet<'a>>(this: PtrUninit, ptr: PtrMut) -> PtrMut {
46    unsafe { try_from(ptr.as_const(), T::SHAPE, this).unwrap() }
47}
48
49// Note: This impl is for sized T only. Box<[U]> and Box<str> have separate impls below.
50unsafe impl<'a, T: Facet<'a>> Facet<'a> for Box<T> {
51    const SHAPE: &'static crate::Shape = &const {
52        const fn build_type_name<'a, T: Facet<'a>>() -> TypeNameFn {
53            fn type_name_impl<'a, T: Facet<'a>>(
54                _shape: &'static crate::Shape,
55                f: &mut core::fmt::Formatter<'_>,
56                opts: TypeNameOpts,
57            ) -> core::fmt::Result {
58                write!(f, "Box")?;
59                if let Some(opts) = opts.for_children() {
60                    write!(f, "<")?;
61                    T::SHAPE.write_type_name(f, opts)?;
62                    write!(f, ">")?;
63                } else {
64                    write!(f, "<…>")?;
65                }
66                Ok(())
67            }
68            type_name_impl::<T>
69        }
70
71        ShapeBuilder::for_sized::<Self>("Box")
72            .type_name(build_type_name::<T>())
73            .vtable_indirect(&VTableIndirect::EMPTY)
74            .type_ops_indirect(
75                &const {
76                    unsafe fn drop_in_place<'a, T: Facet<'a>>(ox: OxPtrMut) {
77                        unsafe {
78                            core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<T>>() as *mut Box<T>)
79                        };
80                    }
81                    TypeOpsIndirect {
82                        drop_in_place: drop_in_place::<T>,
83                        default_in_place: None,
84                        clone_into: None,
85                        is_truthy: None,
86                    }
87                },
88            )
89            .ty(Type::User(UserType::Opaque))
90            .def(Def::Pointer(PointerDef {
91                vtable: &const {
92                    PointerVTable {
93                        borrow_fn: Some(borrow_fn::<T>),
94                        new_into_fn: Some(new_into_fn::<T>),
95                        ..PointerVTable::new()
96                    }
97                },
98                pointee: Some(T::SHAPE),
99                weak: None,
100                strong: None,
101                flags: PointerFlags::EMPTY,
102                known: Some(KnownPointer::Box),
103            }))
104            .type_params(&[crate::TypeParam {
105                name: "T",
106                shape: T::SHAPE,
107            }])
108            .inner(T::SHAPE)
109            // Box<T> propagates T's variance
110            .variance(Shape::computed_variance)
111            .build()
112    };
113}
114
115// ============================================================================
116// Box<[U]> implementation with SliceBuilderVTable
117// ============================================================================
118
119// Drop function for Box<[U]>
120unsafe fn box_slice_drop<U>(ox: OxPtrMut) {
121    unsafe { core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<[U]>>() as *mut Box<[U]>) };
122}
123
124// Borrow function for Box<[U]>
125unsafe fn box_slice_borrow<'a, U: Facet<'a>>(this: PtrConst) -> PtrConst {
126    unsafe {
127        let concrete = this.get::<Box<[U]>>();
128        let slice: &[U] = concrete.as_ref();
129        PtrConst::new(NonNull::from(slice).as_ptr())
130    }
131}
132
133// Slice builder functions for Box<[U]>
134fn box_slice_builder_new<'a, U: Facet<'a>>() -> PtrMut {
135    let v = Box::new(Vec::<U>::new());
136    let raw = Box::into_raw(v);
137    PtrMut::new(raw as *mut u8)
138}
139
140unsafe fn box_slice_builder_push<'a, U: Facet<'a>>(builder: PtrMut, item: PtrMut) {
141    unsafe {
142        let vec = builder.as_mut::<Vec<U>>();
143        let value = item.read::<U>();
144        vec.push(value);
145    }
146}
147
148unsafe fn box_slice_builder_convert<'a, U: Facet<'a>>(builder: PtrMut) -> PtrConst {
149    unsafe {
150        let vec_box = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
151        let boxed_slice: Box<[U]> = (*vec_box).into_boxed_slice();
152
153        // Allocate memory for the Box<[U]> (which is a fat pointer, 16 bytes on 64-bit)
154        let layout = Layout::new::<Box<[U]>>();
155        let ptr = alloc::alloc::alloc(layout) as *mut Box<[U]>;
156        if ptr.is_null() {
157            alloc::alloc::handle_alloc_error(layout);
158        }
159
160        // Write the Box<[U]> into the allocation
161        ptr.write(boxed_slice);
162
163        PtrConst::new(ptr as *const u8)
164    }
165}
166
167unsafe fn box_slice_builder_free<'a, U: Facet<'a>>(builder: PtrMut) {
168    unsafe {
169        let _ = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
170    }
171}
172
173// ============================================================================
174// Box<str> implementation
175// ============================================================================
176
177// Drop function for Box<str>
178unsafe fn box_str_drop(ox: OxPtrMut) {
179    unsafe { core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<str>>() as *mut Box<str>) };
180}
181
182// Borrow function for Box<str>
183unsafe fn box_str_borrow(this: PtrConst) -> PtrConst {
184    unsafe {
185        let concrete = this.get::<Box<str>>();
186        let s: &str = concrete;
187        PtrConst::new(NonNull::from(s).as_ptr())
188    }
189}
190
191// Module-level static for Box<str> type ops
192static BOX_STR_TYPE_OPS: TypeOpsIndirect = TypeOpsIndirect {
193    drop_in_place: box_str_drop,
194    default_in_place: None,
195    clone_into: None,
196    is_truthy: None,
197};
198
199unsafe impl<'a> Facet<'a> for Box<str> {
200    const SHAPE: &'static crate::Shape = &const {
201        fn type_name_box_str(
202            _shape: &'static crate::Shape,
203            f: &mut core::fmt::Formatter<'_>,
204            opts: TypeNameOpts,
205        ) -> core::fmt::Result {
206            write!(f, "Box")?;
207            if let Some(opts) = opts.for_children() {
208                write!(f, "<")?;
209                str::SHAPE.write_type_name(f, opts)?;
210                write!(f, ">")?;
211            } else {
212                write!(f, "<…>")?;
213            }
214            Ok(())
215        }
216
217        ShapeBuilder::for_sized::<Self>("Box")
218            .type_name(type_name_box_str)
219            .vtable_indirect(&VTableIndirect::EMPTY)
220            .type_ops_indirect(&BOX_STR_TYPE_OPS)
221            .ty(Type::User(UserType::Opaque))
222            .def(Def::Pointer(PointerDef {
223                vtable: &const {
224                    PointerVTable {
225                        borrow_fn: Some(box_str_borrow),
226                        ..PointerVTable::new()
227                    }
228                },
229                pointee: Some(str::SHAPE),
230                weak: None,
231                strong: None,
232                flags: PointerFlags::EMPTY,
233                known: Some(KnownPointer::Box),
234            }))
235            .type_params(&[crate::TypeParam {
236                name: "T",
237                shape: str::SHAPE,
238            }])
239            .build()
240    };
241}
242
243unsafe impl<'a, U: Facet<'a>> Facet<'a> for Box<[U]> {
244    const SHAPE: &'static crate::Shape = &const {
245        fn type_name_box_slice<'a, U: Facet<'a>>(
246            _shape: &'static crate::Shape,
247            f: &mut core::fmt::Formatter<'_>,
248            opts: TypeNameOpts,
249        ) -> core::fmt::Result {
250            write!(f, "Box")?;
251            if let Some(opts) = opts.for_children() {
252                write!(f, "<")?;
253                <[U]>::SHAPE.write_type_name(f, opts)?;
254                write!(f, ">")?;
255            } else {
256                write!(f, "<…>")?;
257            }
258            Ok(())
259        }
260
261        ShapeBuilder::for_sized::<Self>("Box")
262            .type_name(type_name_box_slice::<U>)
263            .vtable_indirect(&VTableIndirect::EMPTY)
264            .type_ops_indirect(
265                &const {
266                    TypeOpsIndirect {
267                        drop_in_place: box_slice_drop::<U>,
268                        default_in_place: None,
269                        clone_into: None,
270                        is_truthy: None,
271                    }
272                },
273            )
274            .ty(Type::User(UserType::Opaque))
275            .def(Def::Pointer(PointerDef {
276                vtable: &const {
277                    PointerVTable {
278                        borrow_fn: Some(box_slice_borrow::<U>),
279                        slice_builder_vtable: Some(
280                            &const {
281                                SliceBuilderVTable::new(
282                                    box_slice_builder_new::<U>,
283                                    box_slice_builder_push::<U>,
284                                    box_slice_builder_convert::<U>,
285                                    box_slice_builder_free::<U>,
286                                )
287                            },
288                        ),
289                        ..PointerVTable::new()
290                    }
291                },
292                pointee: Some(<[U]>::SHAPE),
293                weak: None,
294                strong: None,
295                flags: PointerFlags::EMPTY,
296                known: Some(KnownPointer::Box),
297            }))
298            .type_params(&[crate::TypeParam {
299                name: "T",
300                shape: <[U]>::SHAPE,
301            }])
302            .build()
303    };
304}
305
306#[cfg(test)]
307mod tests {
308    use core::mem::ManuallyDrop;
309
310    use alloc::boxed::Box;
311    use alloc::string::String;
312
313    use super::*;
314
315    #[test]
316    fn test_box_type_params() {
317        let [type_param_1] = <Box<i32>>::SHAPE.type_params else {
318            panic!("Box<T> should only have 1 type param")
319        };
320        assert_eq!(type_param_1.shape(), i32::SHAPE);
321    }
322
323    #[test]
324    fn test_box_vtable_1_new_borrow_drop() {
325        facet_testhelpers::setup();
326
327        let box_shape = <Box<String>>::SHAPE;
328        let box_def = box_shape
329            .def
330            .into_pointer()
331            .expect("Box<T> should have a smart pointer definition");
332
333        // Allocate memory for the Box
334        let box_uninit_ptr = box_shape.allocate().unwrap();
335
336        // Get the function pointer for creating a new Box from a value
337        let new_into_fn = box_def
338            .vtable
339            .new_into_fn
340            .expect("Box<T> should have new_into_fn");
341
342        // Create the value and initialize the Box
343        let mut value = ManuallyDrop::new(String::from("example"));
344        let box_ptr = unsafe {
345            new_into_fn(
346                box_uninit_ptr,
347                PtrMut::new(NonNull::from(&mut value).as_ptr()),
348            )
349        };
350        // The value now belongs to the Box, prevent its drop
351
352        // Get the function pointer for borrowing the inner value
353        let borrow_fn = box_def
354            .vtable
355            .borrow_fn
356            .expect("Box<T> should have borrow_fn");
357
358        // Borrow the inner value and check it
359        let borrowed_ptr = unsafe { borrow_fn(box_ptr.as_const()) };
360        // SAFETY: borrowed_ptr points to a valid String within the Box
361        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
362
363        // Drop the Box in place
364        // SAFETY: box_ptr points to a valid Box<String>
365        unsafe {
366            box_shape
367                .call_drop_in_place(box_ptr)
368                .expect("Box<T> should have drop_in_place");
369        }
370
371        // Deallocate the memory
372        // SAFETY: box_ptr was allocated by box_shape and is now dropped (but memory is still valid)
373        unsafe { box_shape.deallocate_mut(box_ptr).unwrap() };
374    }
375
376    #[test]
377    fn test_box_slice_builder() {
378        facet_testhelpers::setup();
379
380        // Get the shapes we'll be working with
381        let box_slice_shape = <Box<[u8]>>::SHAPE;
382        let box_slice_def = box_slice_shape
383            .def
384            .into_pointer()
385            .expect("Box<[u8]> should have a smart pointer definition");
386
387        // Get the slice builder vtable
388        let slice_builder_vtable = box_slice_def
389            .vtable
390            .slice_builder_vtable
391            .expect("Box<[u8]> should have slice_builder_vtable");
392
393        // 1. Create a new builder
394        let builder_ptr = (slice_builder_vtable.new_fn)();
395
396        // 2. Push some items to the builder
397        let push_fn = slice_builder_vtable.push_fn;
398        let values: [u8; 5] = [1, 2, 3, 4, 5];
399        for &value in &values {
400            let mut value_copy = value;
401            let value_ptr = PtrMut::new(NonNull::from(&mut value_copy).as_ptr());
402            unsafe { push_fn(builder_ptr, value_ptr) };
403        }
404
405        // 3. Convert the builder to Box<[u8]>
406        let convert_fn = slice_builder_vtable.convert_fn;
407        let box_slice_ptr = unsafe { convert_fn(builder_ptr) };
408
409        // 4. Verify the contents by borrowing
410        let borrow_fn = box_slice_def
411            .vtable
412            .borrow_fn
413            .expect("Box<[u8]> should have borrow_fn");
414        let borrowed_ptr = unsafe { borrow_fn(box_slice_ptr) };
415
416        // Convert the wide pointer to a slice reference
417        let slice = unsafe { borrowed_ptr.get::<[u8]>() };
418        assert_eq!(slice, &[1, 2, 3, 4, 5]);
419
420        // 5. Clean up - the Box<[u8]> was boxed by convert_fn, we need to deallocate the Box
421        unsafe {
422            let _ = Box::from_raw(box_slice_ptr.as_ptr::<Box<[u8]>>() as *mut Box<[u8]>);
423        }
424    }
425}