facet_core/types/def/
array.rs1use crate::{PtrMut, ptr::PtrConst};
2
3use super::Shape;
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(C)]
8#[non_exhaustive]
9pub struct ArrayDef<'shape> {
10 pub vtable: &'shape ArrayVTable,
12
13 pub t: &'shape Shape<'shape>,
15
16 pub n: usize,
18}
19
20impl<'shape> ArrayDef<'shape> {
21 pub const fn builder() -> ArrayDefBuilder<'shape> {
23 ArrayDefBuilder::new()
24 }
25
26 pub fn t(&self) -> &'shape Shape<'shape> {
28 self.t
29 }
30}
31
32pub 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 #[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 pub const fn vtable(mut self, vtable: &'shape ArrayVTable) -> Self {
52 self.vtable = Some(vtable);
53 self
54 }
55
56 pub const fn t(mut self, t: &'shape Shape<'shape>) -> Self {
58 self.t = Some(t);
59 self
60 }
61
62 pub const fn n(mut self, n: usize) -> Self {
64 self.n = Some(n);
65 self
66 }
67
68 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
78pub type ArrayAsPtrFn = unsafe fn(array: PtrConst) -> PtrConst;
84
85pub type ArrayAsMutPtrFn = unsafe fn(array: PtrMut) -> PtrMut;
91
92#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
94#[repr(C)]
95#[non_exhaustive]
96pub struct ArrayVTable {
97 pub as_ptr: ArrayAsPtrFn,
99
100 pub as_mut_ptr: ArrayAsMutPtrFn,
102}
103
104impl ArrayVTable {
105 pub const fn builder() -> ArrayVTableBuilder {
107 ArrayVTableBuilder::new()
108 }
109}
110
111pub struct ArrayVTableBuilder {
113 as_ptr_fn: Option<ArrayAsPtrFn>,
114 as_mut_ptr_fn: Option<ArrayAsMutPtrFn>,
115}
116
117impl ArrayVTableBuilder {
118 #[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 pub const fn as_ptr(mut self, f: ArrayAsPtrFn) -> Self {
129 self.as_ptr_fn = Some(f);
130 self
131 }
132
133 pub const fn as_mut_ptr(mut self, f: ArrayAsMutPtrFn) -> Self {
135 self.as_mut_ptr_fn = Some(f);
136 self
137 }
138
139 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}