facet_core/types/def/
set.rs1use crate::ptr::{PtrConst, PtrMut, PtrUninit};
2
3use super::Shape;
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(C)]
8#[non_exhaustive]
9pub struct SetDef {
10 pub vtable: &'static SetVTable,
12 pub t: fn() -> &'static Shape,
14}
15
16impl SetDef {
17 pub const fn builder() -> SetDefBuilder {
19 SetDefBuilder::new()
20 }
21
22 pub fn t(&self) -> &'static Shape {
24 (self.t)()
25 }
26}
27
28pub struct SetDefBuilder {
30 vtable: Option<&'static SetVTable>,
31 t: Option<fn() -> &'static Shape>,
32}
33
34impl SetDefBuilder {
35 #[allow(clippy::new_without_default)]
37 pub const fn new() -> Self {
38 Self {
39 vtable: None,
40 t: None,
41 }
42 }
43
44 pub const fn vtable(mut self, vtable: &'static SetVTable) -> Self {
46 self.vtable = Some(vtable);
47 self
48 }
49
50 pub const fn t(mut self, t: fn() -> &'static Shape) -> Self {
52 self.t = Some(t);
53 self
54 }
55
56 pub const fn build(self) -> SetDef {
58 SetDef {
59 vtable: self.vtable.unwrap(),
60 t: self.t.unwrap(),
61 }
62 }
63}
64
65pub type SetInitInPlaceWithCapacityFn =
72 for<'mem> unsafe fn(set: PtrUninit<'mem>, capacity: usize) -> PtrMut<'mem>;
73
74pub type SetInsertFn =
83 for<'set, 'value> unsafe fn(set: PtrMut<'set>, value: PtrMut<'value>) -> bool;
84
85pub type SetLenFn = for<'set> unsafe fn(set: PtrConst<'set>) -> usize;
91
92pub type SetContainsFn =
98 for<'set, 'value> unsafe fn(set: PtrConst<'set>, value: PtrConst<'value>) -> bool;
99
100pub type SetIterFn = for<'set> unsafe fn(set: PtrConst<'set>) -> PtrMut<'set>;
106
107pub type SetIterNextFn = for<'iter> unsafe fn(iter: PtrMut<'iter>) -> Option<PtrConst<'iter>>;
113
114pub type SetIterDeallocFn = for<'iter> unsafe fn(iter: PtrMut<'iter>);
120
121#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
123#[repr(C)]
124#[non_exhaustive]
125pub struct SetIterVTable {
126 pub next: SetIterNextFn,
128
129 pub dealloc: SetIterDeallocFn,
131}
132
133impl SetIterVTable {
134 pub const fn builder() -> SetIterVTableBuilder {
136 SetIterVTableBuilder::new()
137 }
138}
139
140pub struct SetIterVTableBuilder {
142 next: Option<SetIterNextFn>,
143 dealloc: Option<SetIterDeallocFn>,
144}
145
146impl SetIterVTableBuilder {
147 #[allow(clippy::new_without_default)]
149 pub const fn new() -> Self {
150 Self {
151 next: None,
152 dealloc: None,
153 }
154 }
155
156 pub const fn next(mut self, f: SetIterNextFn) -> Self {
158 self.next = Some(f);
159 self
160 }
161
162 pub const fn dealloc(mut self, f: SetIterDeallocFn) -> Self {
164 self.dealloc = Some(f);
165 self
166 }
167
168 pub const fn build(self) -> SetIterVTable {
174 SetIterVTable {
175 next: self.next.unwrap(),
176 dealloc: self.dealloc.unwrap(),
177 }
178 }
179}
180
181#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
183#[repr(C)]
184pub struct SetVTable {
185 pub init_in_place_with_capacity_fn: SetInitInPlaceWithCapacityFn,
187
188 pub insert_fn: SetInsertFn,
190
191 pub len_fn: SetLenFn,
193
194 pub contains_fn: SetContainsFn,
196
197 pub iter_fn: SetIterFn,
199
200 pub iter_vtable: SetIterVTable,
202}
203
204impl SetVTable {
205 pub const fn builder() -> SetVTableBuilder {
207 SetVTableBuilder::new()
208 }
209}
210
211pub struct SetVTableBuilder {
213 init_in_place_with_capacity_fn: Option<SetInitInPlaceWithCapacityFn>,
214 insert_fn: Option<SetInsertFn>,
215 len_fn: Option<SetLenFn>,
216 contains_fn: Option<SetContainsFn>,
217 iter_fn: Option<SetIterFn>,
218 iter_vtable: Option<SetIterVTable>,
219}
220
221impl SetVTableBuilder {
222 #[allow(clippy::new_without_default)]
224 pub const fn new() -> Self {
225 Self {
226 init_in_place_with_capacity_fn: None,
227 insert_fn: None,
228 len_fn: None,
229 contains_fn: None,
230 iter_fn: None,
231 iter_vtable: None,
232 }
233 }
234
235 pub const fn init_in_place_with_capacity(mut self, f: SetInitInPlaceWithCapacityFn) -> Self {
237 self.init_in_place_with_capacity_fn = Some(f);
238 self
239 }
240
241 pub const fn insert(mut self, f: SetInsertFn) -> Self {
243 self.insert_fn = Some(f);
244 self
245 }
246
247 pub const fn len(mut self, f: SetLenFn) -> Self {
249 self.len_fn = Some(f);
250 self
251 }
252
253 pub const fn contains(mut self, f: SetContainsFn) -> Self {
255 self.contains_fn = Some(f);
256 self
257 }
258
259 pub const fn iter(mut self, f: SetIterFn) -> Self {
261 self.iter_fn = Some(f);
262 self
263 }
264
265 pub const fn iter_vtable(mut self, vtable: SetIterVTable) -> Self {
267 self.iter_vtable = Some(vtable);
268 self
269 }
270
271 pub const fn build(self) -> SetVTable {
277 SetVTable {
278 init_in_place_with_capacity_fn: self.init_in_place_with_capacity_fn.unwrap(),
279 insert_fn: self.insert_fn.unwrap(),
280 len_fn: self.len_fn.unwrap(),
281 contains_fn: self.contains_fn.unwrap(),
282 iter_fn: self.iter_fn.unwrap(),
283 iter_vtable: self.iter_vtable.unwrap(),
284 }
285 }
286}