facet_core/types/def/
array.rs

1use crate::{PtrMut, ptr::PtrConst};
2
3use super::Shape;
4
5/// Fields for array types
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(C)]
8#[non_exhaustive]
9pub struct ArrayDef<'shape> {
10    /// vtable for interacting with the array
11    pub vtable: &'shape ArrayVTable,
12
13    /// shape of the items in the array
14    pub t: &'shape Shape<'shape>,
15
16    /// The length of the array
17    pub n: usize,
18}
19
20impl<'shape> ArrayDef<'shape> {
21    /// Returns a builder for ArrayDef
22    pub const fn builder() -> ArrayDefBuilder<'shape> {
23        ArrayDefBuilder::new()
24    }
25
26    /// Returns the shape of the items in the array
27    pub fn t(&self) -> &'shape Shape<'shape> {
28        self.t
29    }
30}
31
32/// Builder for ArrayDef
33pub struct ArrayDefBuilder<'shape> {
34    vtable: Option<&'shape ArrayVTable>,
35    t: Option<&'shape Shape<'shape>>,
36    n: Option<usize>,
37}
38
39impl<'shape> ArrayDefBuilder<'shape> {
40    /// Creates a new ArrayDefBuilder
41    #[allow(clippy::new_without_default)]
42    pub const fn new() -> Self {
43        Self {
44            vtable: None,
45            t: None,
46            n: None,
47        }
48    }
49
50    /// Sets the vtable for the ArrayDef
51    pub const fn vtable(mut self, vtable: &'shape ArrayVTable) -> Self {
52        self.vtable = Some(vtable);
53        self
54    }
55
56    /// Sets the item shape for the ArrayDef
57    pub const fn t(mut self, t: &'shape Shape<'shape>) -> Self {
58        self.t = Some(t);
59        self
60    }
61
62    /// Sets the length for the ArrayDef (added method)
63    pub const fn n(mut self, n: usize) -> Self {
64        self.n = Some(n);
65        self
66    }
67
68    /// Builds the ArrayDef
69    pub const fn build(self) -> ArrayDef<'shape> {
70        ArrayDef {
71            vtable: self.vtable.unwrap(),
72            t: self.t.unwrap(),
73            n: self.n.unwrap(),
74        }
75    }
76}
77
78/// Get pointer to the data buffer of the array.
79///
80/// # Safety
81///
82/// The `array` parameter must point to aligned, initialized memory of the correct type.
83pub type ArrayAsPtrFn = unsafe fn(array: PtrConst) -> PtrConst;
84
85/// Get mutable pointer to the data buffer of the array.
86///
87/// # Safety
88///
89/// The `array` parameter must point to aligned, initialized memory of the correct type.
90pub type ArrayAsMutPtrFn = unsafe fn(array: PtrMut) -> PtrMut;
91
92/// Virtual table for an array
93#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
94#[repr(C)]
95#[non_exhaustive]
96pub struct ArrayVTable {
97    /// cf. [`ArrayAsPtrFn`]
98    pub as_ptr: ArrayAsPtrFn,
99
100    /// cf. [`ArrayAsMutPtrFn`]
101    pub as_mut_ptr: ArrayAsMutPtrFn,
102}
103
104impl ArrayVTable {
105    /// Returns a builder for ListVTable
106    pub const fn builder() -> ArrayVTableBuilder {
107        ArrayVTableBuilder::new()
108    }
109}
110
111/// Builds a [`ArrayVTable`]
112pub struct ArrayVTableBuilder {
113    as_ptr_fn: Option<ArrayAsPtrFn>,
114    as_mut_ptr_fn: Option<ArrayAsMutPtrFn>,
115}
116
117impl ArrayVTableBuilder {
118    /// Creates a new [`ArrayVTableBuilder`] with all fields set to `None`.
119    #[allow(clippy::new_without_default)]
120    pub const fn new() -> Self {
121        Self {
122            as_ptr_fn: None,
123            as_mut_ptr_fn: None,
124        }
125    }
126
127    /// Sets the as_ptr field
128    pub const fn as_ptr(mut self, f: ArrayAsPtrFn) -> Self {
129        self.as_ptr_fn = Some(f);
130        self
131    }
132
133    /// Sets the as_mut_ptr field
134    pub const fn as_mut_ptr(mut self, f: ArrayAsMutPtrFn) -> Self {
135        self.as_mut_ptr_fn = Some(f);
136        self
137    }
138
139    /// Builds the [`ArrayVTable`] from the current state of the builder.
140    ///
141    /// # Panics
142    ///
143    /// This method will panic if any of the required fields are `None`.
144    pub const fn build(self) -> ArrayVTable {
145        ArrayVTable {
146            as_ptr: self.as_ptr_fn.unwrap(),
147            as_mut_ptr: self.as_mut_ptr_fn.unwrap(),
148        }
149    }
150}