1use 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 MapDef {
10 pub vtable: &'static MapVTable,
12 pub k: &'static Shape,
14 pub v: &'static Shape,
16}
17
18impl MapDef {
19 pub const fn builder() -> MapDefBuilder {
21 MapDefBuilder::new()
22 }
23}
24
25pub struct MapDefBuilder {
27 vtable: Option<&'static MapVTable>,
28 k: Option<&'static Shape>,
29 v: Option<&'static Shape>,
30}
31
32impl MapDefBuilder {
33 #[allow(clippy::new_without_default)]
35 pub const fn new() -> Self {
36 Self {
37 vtable: None,
38 k: None,
39 v: None,
40 }
41 }
42
43 pub const fn vtable(mut self, vtable: &'static MapVTable) -> Self {
45 self.vtable = Some(vtable);
46 self
47 }
48
49 pub const fn k(mut self, k: &'static Shape) -> Self {
51 self.k = Some(k);
52 self
53 }
54
55 pub const fn v(mut self, v: &'static Shape) -> Self {
57 self.v = Some(v);
58 self
59 }
60
61 pub const fn build(self) -> MapDef {
63 MapDef {
64 vtable: self.vtable.unwrap(),
65 k: self.k.unwrap(),
66 v: self.v.unwrap(),
67 }
68 }
69}
70
71pub type MapInitInPlaceWithCapacityFn =
78 for<'mem> unsafe fn(map: PtrUninit<'mem>, capacity: usize) -> PtrMut<'mem>;
79
80pub type MapInsertFn =
88 for<'map, 'key, 'value> unsafe fn(map: PtrMut<'map>, key: PtrMut<'key>, value: PtrMut<'value>);
89
90pub type MapLenFn = for<'map> unsafe fn(map: PtrConst<'map>) -> usize;
96
97pub type MapContainsKeyFn =
103 for<'map, 'key> unsafe fn(map: PtrConst<'map>, key: PtrConst<'key>) -> bool;
104
105pub type MapGetValuePtrFn =
111 for<'map, 'key> unsafe fn(map: PtrConst<'map>, key: PtrConst<'key>) -> Option<PtrConst<'map>>;
112
113pub type MapIterFn = for<'map> unsafe fn(map: PtrConst<'map>) -> PtrMut<'map>;
119
120pub type MapIterNextFn =
126 for<'iter> unsafe fn(iter: PtrMut<'iter>) -> Option<(PtrConst<'iter>, PtrConst<'iter>)>;
127
128pub type MapIterDeallocFn = for<'iter> unsafe fn(iter: PtrMut<'iter>);
134
135#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
137#[repr(C)]
138#[non_exhaustive]
139pub struct MapIterVTable {
140 pub next: MapIterNextFn,
142
143 pub dealloc: MapIterDeallocFn,
145}
146
147impl MapIterVTable {
148 pub const fn builder() -> MapIterVTableBuilder {
150 MapIterVTableBuilder::new()
151 }
152}
153
154pub struct MapIterVTableBuilder {
156 next: Option<MapIterNextFn>,
157 dealloc: Option<MapIterDeallocFn>,
158}
159
160impl MapIterVTableBuilder {
161 #[allow(clippy::new_without_default)]
163 pub const fn new() -> Self {
164 Self {
165 next: None,
166 dealloc: None,
167 }
168 }
169
170 pub const fn next(mut self, f: MapIterNextFn) -> Self {
172 self.next = Some(f);
173 self
174 }
175
176 pub const fn dealloc(mut self, f: MapIterDeallocFn) -> Self {
178 self.dealloc = Some(f);
179 self
180 }
181
182 pub const fn build(self) -> MapIterVTable {
188 MapIterVTable {
189 next: self.next.unwrap(),
190 dealloc: self.dealloc.unwrap(),
191 }
192 }
193}
194
195#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
197#[repr(C)]
198pub struct MapVTable {
199 pub init_in_place_with_capacity_fn: MapInitInPlaceWithCapacityFn,
201
202 pub insert_fn: MapInsertFn,
204
205 pub len_fn: MapLenFn,
207
208 pub contains_key_fn: MapContainsKeyFn,
210
211 pub get_value_ptr_fn: MapGetValuePtrFn,
213
214 pub iter_fn: MapIterFn,
216
217 pub iter_vtable: MapIterVTable,
219}
220
221impl MapVTable {
222 pub const fn builder() -> MapVTableBuilder {
224 MapVTableBuilder::new()
225 }
226}
227
228pub struct MapVTableBuilder {
230 init_in_place_with_capacity_fn: Option<MapInitInPlaceWithCapacityFn>,
231 insert_fn: Option<MapInsertFn>,
232 len_fn: Option<MapLenFn>,
233 contains_key_fn: Option<MapContainsKeyFn>,
234 get_value_ptr_fn: Option<MapGetValuePtrFn>,
235 iter_fn: Option<MapIterFn>,
236 iter_vtable: Option<MapIterVTable>,
237}
238
239impl MapVTableBuilder {
240 #[allow(clippy::new_without_default)]
242 pub const fn new() -> Self {
243 Self {
244 init_in_place_with_capacity_fn: None,
245 insert_fn: None,
246 len_fn: None,
247 contains_key_fn: None,
248 get_value_ptr_fn: None,
249 iter_fn: None,
250 iter_vtable: None,
251 }
252 }
253
254 pub const fn init_in_place_with_capacity(mut self, f: MapInitInPlaceWithCapacityFn) -> Self {
256 self.init_in_place_with_capacity_fn = Some(f);
257 self
258 }
259
260 pub const fn insert(mut self, f: MapInsertFn) -> Self {
262 self.insert_fn = Some(f);
263 self
264 }
265
266 pub const fn len(mut self, f: MapLenFn) -> Self {
268 self.len_fn = Some(f);
269 self
270 }
271
272 pub const fn contains_key(mut self, f: MapContainsKeyFn) -> Self {
274 self.contains_key_fn = Some(f);
275 self
276 }
277
278 pub const fn get_value_ptr(mut self, f: MapGetValuePtrFn) -> Self {
280 self.get_value_ptr_fn = Some(f);
281 self
282 }
283
284 pub const fn iter(mut self, f: MapIterFn) -> Self {
286 self.iter_fn = Some(f);
287 self
288 }
289
290 pub const fn iter_vtable(mut self, vtable: MapIterVTable) -> Self {
292 self.iter_vtable = Some(vtable);
293 self
294 }
295
296 pub const fn build(self) -> MapVTable {
302 MapVTable {
303 init_in_place_with_capacity_fn: self.init_in_place_with_capacity_fn.unwrap(),
304 insert_fn: self.insert_fn.unwrap(),
305 len_fn: self.len_fn.unwrap(),
306 contains_key_fn: self.contains_key_fn.unwrap(),
307 get_value_ptr_fn: self.get_value_ptr_fn.unwrap(),
308 iter_fn: self.iter_fn.unwrap(),
309 iter_vtable: self.iter_vtable.unwrap(),
310 }
311 }
312}