Skip to main content

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