facet_core/types/def/
list.rs1use crate::ptr::{PtrConst, PtrMut, PtrUninit};
2
3use super::{IterVTable, Shape};
4
5#[derive(Clone, Copy, Debug)]
7#[repr(C)]
8pub struct ListDef {
9 pub vtable: &'static ListVTable,
11 pub t: fn() -> &'static Shape,
13}
14
15impl ListDef {
16 pub const fn builder() -> ListDefBuilder {
18 ListDefBuilder::new()
19 }
20
21 pub fn t(&self) -> &'static Shape {
23 (self.t)()
24 }
25}
26
27pub struct ListDefBuilder {
29 vtable: Option<&'static ListVTable>,
30 t: Option<fn() -> &'static Shape>,
31}
32
33impl ListDefBuilder {
34 #[allow(clippy::new_without_default)]
36 pub const fn new() -> Self {
37 Self {
38 vtable: None,
39 t: None,
40 }
41 }
42
43 pub const fn vtable(mut self, vtable: &'static ListVTable) -> Self {
45 self.vtable = Some(vtable);
46 self
47 }
48
49 pub const fn t(mut self, t: fn() -> &'static Shape) -> Self {
51 self.t = Some(t);
52 self
53 }
54
55 pub const fn build(self) -> ListDef {
57 ListDef {
58 vtable: self.vtable.unwrap(),
59 t: self.t.unwrap(),
60 }
61 }
62}
63
64pub type ListInitInPlaceWithCapacityFn =
71 for<'mem> unsafe fn(list: PtrUninit<'mem>, capacity: usize) -> PtrMut<'mem>;
72
73pub type ListPushFn = unsafe fn(list: PtrMut, item: PtrMut);
81pub type ListLenFn = unsafe fn(list: PtrConst) -> usize;
89
90pub type ListGetFn = unsafe fn(list: PtrConst, index: usize) -> Option<PtrConst>;
97
98pub type ListGetMutFn = unsafe fn(list: PtrMut, index: usize) -> Option<PtrMut>;
105
106pub type ListAsPtrFn = unsafe fn(list: PtrConst) -> PtrConst;
112
113pub type ListAsMutPtrFn = unsafe fn(list: PtrMut) -> PtrMut;
119
120#[derive(Clone, Copy, Debug)]
122#[repr(C)]
123pub struct ListVTable {
124 pub init_in_place_with_capacity: Option<ListInitInPlaceWithCapacityFn>,
127
128 pub push: Option<ListPushFn>,
131
132 pub len: ListLenFn,
134
135 pub get: ListGetFn,
137
138 pub get_mut: Option<ListGetMutFn>,
141
142 pub as_ptr: Option<ListAsPtrFn>,
145
146 pub as_mut_ptr: Option<ListAsMutPtrFn>,
149
150 pub iter_vtable: IterVTable<PtrConst<'static>>,
152}
153
154impl ListVTable {
155 pub const fn builder() -> ListVTableBuilder {
157 ListVTableBuilder::new()
158 }
159}
160
161pub struct ListVTableBuilder {
163 init_in_place_with_capacity: Option<ListInitInPlaceWithCapacityFn>,
164 push: Option<ListPushFn>,
165 len: Option<ListLenFn>,
166 get: Option<ListGetFn>,
167 get_mut: Option<ListGetMutFn>,
168 as_ptr: Option<ListAsPtrFn>,
169 as_mut_ptr: Option<ListAsMutPtrFn>,
170 iter_vtable: Option<IterVTable<PtrConst<'static>>>,
171}
172
173impl ListVTableBuilder {
174 #[allow(clippy::new_without_default)]
176 pub const fn new() -> Self {
177 Self {
178 init_in_place_with_capacity: None,
179 push: None,
180 len: None,
181 get: None,
182 get_mut: None,
183 as_ptr: None,
184 as_mut_ptr: None,
185 iter_vtable: None,
186 }
187 }
188
189 pub const fn init_in_place_with_capacity(mut self, f: ListInitInPlaceWithCapacityFn) -> Self {
191 self.init_in_place_with_capacity = Some(f);
192 self
193 }
194
195 pub const fn push(mut self, f: ListPushFn) -> Self {
197 self.push = Some(f);
198 self
199 }
200
201 pub const fn len(mut self, f: ListLenFn) -> Self {
203 self.len = Some(f);
204 self
205 }
206
207 pub const fn get(mut self, f: ListGetFn) -> Self {
209 self.get = Some(f);
210 self
211 }
212
213 pub const fn get_mut(mut self, f: ListGetMutFn) -> Self {
215 self.get_mut = Some(f);
216 self
217 }
218
219 pub const fn as_ptr(mut self, f: ListAsPtrFn) -> Self {
221 self.as_ptr = Some(f);
222 self
223 }
224
225 pub const fn as_mut_ptr(mut self, f: ListAsMutPtrFn) -> Self {
227 self.as_mut_ptr = Some(f);
228 self
229 }
230
231 pub const fn iter_vtable(mut self, vtable: IterVTable<PtrConst<'static>>) -> Self {
233 self.iter_vtable = Some(vtable);
234 self
235 }
236
237 pub const fn build(self) -> ListVTable {
243 assert!(self.as_ptr.is_some());
244 ListVTable {
245 init_in_place_with_capacity: self.init_in_place_with_capacity,
246 push: self.push,
247 len: self.len.unwrap(),
248 get: self.get.unwrap(),
249 get_mut: self.get_mut,
250 as_ptr: self.as_ptr,
251 as_mut_ptr: self.as_mut_ptr,
252 iter_vtable: self.iter_vtable.unwrap(),
253 }
254 }
255}