facet_core/types/def/array.rs
1use super::{PtrConst, PtrMut, Shape};
2
3/// Fields for array types
4#[derive(Clone, Copy, Debug)]
5#[repr(C)]
6pub struct ArrayDef {
7 /// vtable for interacting with the array
8 pub vtable: &'static ArrayVTable,
9
10 /// shape of the items in the array
11 pub t: &'static Shape,
12
13 /// The length of the array
14 pub n: usize,
15}
16
17impl ArrayDef {
18 /// Construct an `ArrayDef` from its vtable, element shape, and length.
19 pub const fn new(vtable: &'static ArrayVTable, t: &'static Shape, n: usize) -> Self {
20 Self { vtable, t, n }
21 }
22
23 /// Returns the shape of the items in the array
24 pub const fn t(&self) -> &'static Shape {
25 self.t
26 }
27}
28
29/// Get pointer to the data buffer of the array.
30///
31/// # Safety
32///
33/// The `array` parameter must point to aligned, initialized memory of the correct type.
34pub type ArrayAsPtrFn = unsafe extern "C" fn(array: PtrConst) -> PtrConst;
35
36/// Get mutable pointer to the data buffer of the array.
37///
38/// # Safety
39///
40/// The `array` parameter must point to aligned, initialized memory of the correct type.
41pub type ArrayAsMutPtrFn = unsafe extern "C" fn(array: PtrMut) -> PtrMut;
42
43vtable_def! {
44 /// Virtual table for an array
45 #[derive(Clone, Copy, Debug)]
46 #[repr(C)]
47 pub struct ArrayVTable + ArrayVTableBuilder {
48 /// cf. [`ArrayAsPtrFn`]
49 pub as_ptr: ArrayAsPtrFn,
50 /// cf. [`ArrayAsMutPtrFn`]
51 pub as_mut_ptr: ArrayAsMutPtrFn,
52 }
53}