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