facet-core 0.46.0

Core reflection traits and types for the facet ecosystem - provides the Facet trait, Shape metadata, and type-erased pointers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use alloc::alloc::Layout;
use alloc::vec::Vec;
use core::ptr::NonNull;

use alloc::boxed::Box;

use crate::{
    Def, Facet, KnownPointer, OxPtrMut, PointerDef, PointerFlags, PointerVTable, PtrConst, PtrMut,
    PtrUninit, Shape, ShapeBuilder, SliceBuilderVTable, TryFromError, Type, TypeNameFn,
    TypeNameOpts, TypeOpsIndirect, UserType, VTableIndirect, Variance, VarianceDep, VarianceDesc,
};

// Named function for try_from
unsafe fn try_from(
    src_ptr: PtrConst,
    src_shape: &'static Shape,
    dst: PtrUninit,
) -> Result<PtrMut, TryFromError> {
    let layout = src_shape.layout.sized_layout().unwrap();

    unsafe {
        let alloc = crate::alloc_for_layout(layout);

        let src_ptr = src_ptr.as_ptr::<u8>();
        core::ptr::copy_nonoverlapping(src_ptr, alloc.as_mut_byte_ptr(), layout.size());

        // layout of Box<T> == *mut T == *mut u8
        Ok(dst.put(alloc.as_mut_byte_ptr()))
    }
}

// Named function for borrow_fn (sized types)
unsafe extern "C" fn borrow_fn<'a, T: Facet<'a>>(this: PtrConst) -> PtrConst {
    unsafe {
        let concrete = this.get::<Box<T>>();
        let t: &T = concrete.as_ref();
        PtrConst::new(NonNull::from(t).as_ptr() as *const u8)
    }
}

// Named function for new_into_fn (sized types)
unsafe extern "C" fn new_into_fn<'a, 'src, T: Facet<'a>>(this: PtrUninit, ptr: PtrMut) -> PtrMut {
    unsafe { try_from(ptr.as_const(), T::SHAPE, this).unwrap() }
}

// Note: This impl is for sized T only. Box<[U]> and Box<str> have separate impls below.
unsafe impl<'a, T: Facet<'a>> Facet<'a> for Box<T> {
    const SHAPE: &'static crate::Shape = &const {
        const fn build_type_name<'a, T: Facet<'a>>() -> TypeNameFn {
            fn type_name_impl<'a, T: Facet<'a>>(
                _shape: &'static crate::Shape,
                f: &mut core::fmt::Formatter<'_>,
                opts: TypeNameOpts,
            ) -> core::fmt::Result {
                write!(f, "Box")?;
                if let Some(opts) = opts.for_children() {
                    write!(f, "<")?;
                    T::SHAPE.write_type_name(f, opts)?;
                    write!(f, ">")?;
                } else {
                    write!(f, "<…>")?;
                }
                Ok(())
            }
            type_name_impl::<T>
        }

        ShapeBuilder::for_sized::<Self>("Box")
            .module_path("alloc::boxed")
            .type_name(build_type_name::<T>())
            .vtable_indirect(&VTableIndirect::EMPTY)
            .type_ops_indirect(
                &const {
                    unsafe fn drop_in_place<'a, T: Facet<'a>>(ox: OxPtrMut) {
                        unsafe {
                            core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<T>>() as *mut Box<T>)
                        };
                    }
                    TypeOpsIndirect {
                        drop_in_place: drop_in_place::<T>,
                        default_in_place: None,
                        clone_into: None,
                        is_truthy: None,
                    }
                },
            )
            .ty(Type::User(UserType::Opaque))
            .def(Def::Pointer(PointerDef {
                vtable: &const {
                    PointerVTable {
                        borrow_fn: Some(borrow_fn::<T>),
                        new_into_fn: Some(new_into_fn::<T>),
                        ..PointerVTable::new()
                    }
                },
                pointee: Some(T::SHAPE),
                weak: None,
                strong: None,
                flags: PointerFlags::EMPTY,
                known: Some(KnownPointer::Box),
            }))
            .type_params(&[crate::TypeParam {
                name: "T",
                shape: T::SHAPE,
            }])
            .inner(T::SHAPE)
            // Box<T> propagates T's variance
            .variance(VarianceDesc {
                base: Variance::Bivariant,
                deps: &const { [VarianceDep::covariant(T::SHAPE)] },
            })
            .build()
    };
}

// ============================================================================
// Box<[U]> implementation with SliceBuilderVTable
// ============================================================================

// Drop function for Box<[U]>
unsafe fn box_slice_drop<U>(ox: OxPtrMut) {
    unsafe { core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<[U]>>() as *mut Box<[U]>) };
}

// Borrow function for Box<[U]>
unsafe extern "C" fn box_slice_borrow<'a, U: Facet<'a>>(this: PtrConst) -> PtrConst {
    unsafe {
        let concrete = this.get::<Box<[U]>>();
        let slice: &[U] = concrete.as_ref();
        PtrConst::new(NonNull::from(slice).as_ptr())
    }
}

// Slice builder functions for Box<[U]>
fn box_slice_builder_new<'a, U: Facet<'a>>() -> PtrMut {
    let v = Box::new(Vec::<U>::new());
    let raw = Box::into_raw(v);
    PtrMut::new(raw as *mut u8)
}

unsafe fn box_slice_builder_push<'a, U: Facet<'a>>(builder: PtrMut, item: PtrMut) {
    unsafe {
        let vec = builder.as_mut::<Vec<U>>();
        let value = item.read::<U>();
        vec.push(value);
    }
}

unsafe fn box_slice_builder_convert<'a, U: Facet<'a>>(builder: PtrMut) -> PtrConst {
    unsafe {
        let vec_box = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
        let boxed_slice: Box<[U]> = (*vec_box).into_boxed_slice();

        // Allocate memory for the Box<[U]> (which is a fat pointer, 16 bytes on 64-bit)
        let layout = Layout::new::<Box<[U]>>();
        let ptr = alloc::alloc::alloc(layout) as *mut Box<[U]>;
        if ptr.is_null() {
            alloc::alloc::handle_alloc_error(layout);
        }

        // Write the Box<[U]> into the allocation
        ptr.write(boxed_slice);

        PtrConst::new(ptr as *const u8)
    }
}

unsafe fn box_slice_builder_free<'a, U: Facet<'a>>(builder: PtrMut) {
    unsafe {
        let _ = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
    }
}

// ============================================================================
// Box<str> implementation
// ============================================================================

// Drop function for Box<str>
unsafe fn box_str_drop(ox: OxPtrMut) {
    unsafe { core::ptr::drop_in_place(ox.ptr().as_ptr::<Box<str>>() as *mut Box<str>) };
}

// Borrow function for Box<str>
unsafe extern "C" fn box_str_borrow(this: PtrConst) -> PtrConst {
    unsafe {
        let concrete = this.get::<Box<str>>();
        let s: &str = concrete;
        PtrConst::new(NonNull::from(s).as_ptr())
    }
}

// Module-level static for Box<str> type ops
static BOX_STR_TYPE_OPS: TypeOpsIndirect = TypeOpsIndirect {
    drop_in_place: box_str_drop,
    default_in_place: None,
    clone_into: None,
    is_truthy: None,
};

unsafe impl<'a> Facet<'a> for Box<str> {
    const SHAPE: &'static crate::Shape = &const {
        fn type_name_box_str(
            _shape: &'static crate::Shape,
            f: &mut core::fmt::Formatter<'_>,
            opts: TypeNameOpts,
        ) -> core::fmt::Result {
            write!(f, "Box")?;
            if let Some(opts) = opts.for_children() {
                write!(f, "<")?;
                str::SHAPE.write_type_name(f, opts)?;
                write!(f, ">")?;
            } else {
                write!(f, "<…>")?;
            }
            Ok(())
        }

        ShapeBuilder::for_sized::<Self>("Box")
            .module_path("alloc::boxed")
            .type_name(type_name_box_str)
            .vtable_indirect(&VTableIndirect::EMPTY)
            .type_ops_indirect(&BOX_STR_TYPE_OPS)
            .ty(Type::User(UserType::Opaque))
            .def(Def::Pointer(PointerDef {
                vtable: &const {
                    PointerVTable {
                        borrow_fn: Some(box_str_borrow),
                        ..PointerVTable::new()
                    }
                },
                pointee: Some(str::SHAPE),
                weak: None,
                strong: None,
                flags: PointerFlags::EMPTY,
                known: Some(KnownPointer::Box),
            }))
            .type_params(&[crate::TypeParam {
                name: "T",
                shape: str::SHAPE,
            }])
            .build()
    };
}

unsafe impl<'a, U: Facet<'a>> Facet<'a> for Box<[U]> {
    const SHAPE: &'static crate::Shape = &const {
        fn type_name_box_slice<'a, U: Facet<'a>>(
            _shape: &'static crate::Shape,
            f: &mut core::fmt::Formatter<'_>,
            opts: TypeNameOpts,
        ) -> core::fmt::Result {
            write!(f, "Box")?;
            if let Some(opts) = opts.for_children() {
                write!(f, "<")?;
                <[U]>::SHAPE.write_type_name(f, opts)?;
                write!(f, ">")?;
            } else {
                write!(f, "<…>")?;
            }
            Ok(())
        }

        ShapeBuilder::for_sized::<Self>("Box")
            .module_path("alloc::boxed")
            .type_name(type_name_box_slice::<U>)
            .vtable_indirect(&VTableIndirect::EMPTY)
            .type_ops_indirect(
                &const {
                    TypeOpsIndirect {
                        drop_in_place: box_slice_drop::<U>,
                        default_in_place: None,
                        clone_into: None,
                        is_truthy: None,
                    }
                },
            )
            .ty(Type::User(UserType::Opaque))
            .def(Def::Pointer(PointerDef {
                vtable: &const {
                    PointerVTable {
                        borrow_fn: Some(box_slice_borrow::<U>),
                        slice_builder_vtable: Some(
                            &const {
                                SliceBuilderVTable::new(
                                    box_slice_builder_new::<U>,
                                    box_slice_builder_push::<U>,
                                    box_slice_builder_convert::<U>,
                                    box_slice_builder_free::<U>,
                                )
                            },
                        ),
                        ..PointerVTable::new()
                    }
                },
                pointee: Some(<[U]>::SHAPE),
                weak: None,
                strong: None,
                flags: PointerFlags::EMPTY,
                known: Some(KnownPointer::Box),
            }))
            .type_params(&[crate::TypeParam {
                name: "T",
                shape: <[U]>::SHAPE,
            }])
            .build()
    };
}

#[cfg(test)]
mod tests {
    use core::mem::ManuallyDrop;

    use alloc::boxed::Box;
    use alloc::string::String;

    use super::*;

    #[test]
    fn test_box_type_params() {
        let [type_param_1] = <Box<i32>>::SHAPE.type_params else {
            panic!("Box<T> should only have 1 type param")
        };
        assert_eq!(type_param_1.shape(), i32::SHAPE);
    }

    #[test]
    fn test_box_vtable_1_new_borrow_drop() {
        facet_testhelpers::setup();

        let box_shape = <Box<String>>::SHAPE;
        let box_def = box_shape
            .def
            .into_pointer()
            .expect("Box<T> should have a smart pointer definition");

        // Allocate memory for the Box
        let box_uninit_ptr = box_shape.allocate().unwrap();

        // Get the function pointer for creating a new Box from a value
        let new_into_fn = box_def
            .vtable
            .new_into_fn
            .expect("Box<T> should have new_into_fn");

        // Create the value and initialize the Box
        let mut value = ManuallyDrop::new(String::from("example"));
        let box_ptr = unsafe {
            new_into_fn(
                box_uninit_ptr,
                PtrMut::new(NonNull::from(&mut value).as_ptr()),
            )
        };
        // The value now belongs to the Box, prevent its drop

        // Get the function pointer for borrowing the inner value
        let borrow_fn = box_def
            .vtable
            .borrow_fn
            .expect("Box<T> should have borrow_fn");

        // Borrow the inner value and check it
        let borrowed_ptr = unsafe { borrow_fn(box_ptr.as_const()) };
        // SAFETY: borrowed_ptr points to a valid String within the Box
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");

        // Drop the Box in place
        // SAFETY: box_ptr points to a valid Box<String>
        unsafe {
            box_shape
                .call_drop_in_place(box_ptr)
                .expect("Box<T> should have drop_in_place");
        }

        // Deallocate the memory
        // SAFETY: box_ptr was allocated by box_shape and is now dropped (but memory is still valid)
        unsafe { box_shape.deallocate_mut(box_ptr).unwrap() };
    }

    #[test]
    fn test_box_slice_builder() {
        facet_testhelpers::setup();

        // Get the shapes we'll be working with
        let box_slice_shape = <Box<[u8]>>::SHAPE;
        let box_slice_def = box_slice_shape
            .def
            .into_pointer()
            .expect("Box<[u8]> should have a smart pointer definition");

        // Get the slice builder vtable
        let slice_builder_vtable = box_slice_def
            .vtable
            .slice_builder_vtable
            .expect("Box<[u8]> should have slice_builder_vtable");

        // 1. Create a new builder
        let builder_ptr = (slice_builder_vtable.new_fn)();

        // 2. Push some items to the builder
        let push_fn = slice_builder_vtable.push_fn;
        let values: [u8; 5] = [1, 2, 3, 4, 5];
        for &value in &values {
            let mut value_copy = value;
            let value_ptr = PtrMut::new(&mut value_copy);
            unsafe { push_fn(builder_ptr, value_ptr) };
        }

        // 3. Convert the builder to Box<[u8]>
        let convert_fn = slice_builder_vtable.convert_fn;
        let box_slice_ptr = unsafe { convert_fn(builder_ptr) };

        // 4. Verify the contents by borrowing
        let borrow_fn = box_slice_def
            .vtable
            .borrow_fn
            .expect("Box<[u8]> should have borrow_fn");
        let borrowed_ptr = unsafe { borrow_fn(box_slice_ptr) };

        // Convert the wide pointer to a slice reference
        let slice = unsafe { borrowed_ptr.get::<[u8]>() };
        assert_eq!(slice, &[1, 2, 3, 4, 5]);

        // 5. Clean up - the Box<[u8]> was boxed by convert_fn, we need to deallocate the Box
        unsafe {
            let _ = Box::from_raw(box_slice_ptr.as_ptr::<Box<[u8]>>() as *mut Box<[u8]>);
        }
    }
}