1use crate::opaque::{Opaque, OpaqueConst, OpaqueUninit};
2
3pub type MapInitInPlaceWithCapacityFn =
10 unsafe fn(map: OpaqueUninit, capacity: usize) -> Result<Opaque, ()>;
11
12pub type MapInsertFn =
20 for<'map, 'key, 'value> unsafe fn(map: Opaque<'map>, key: Opaque<'key>, value: Opaque<'value>);
21
22pub type MapLenFn = for<'map> unsafe fn(map: OpaqueConst<'map>) -> usize;
28
29pub type MapContainsKeyFn =
35 for<'map, 'key> unsafe fn(map: OpaqueConst<'map>, key: OpaqueConst<'key>) -> bool;
36
37pub type MapGetValuePtrFn = for<'map, 'key> unsafe fn(
43 map: OpaqueConst<'map>,
44 key: OpaqueConst<'key>,
45) -> Option<OpaqueConst<'map>>;
46
47pub type MapIterFn = for<'map> unsafe fn(map: OpaqueConst<'map>) -> Opaque<'map>;
53
54pub type MapIterNextFn =
60 for<'iter> unsafe fn(iter: Opaque<'iter>) -> Option<(OpaqueConst<'iter>, OpaqueConst<'iter>)>;
61
62pub type MapIterDeallocFn = for<'iter> unsafe fn(iter: Opaque<'iter>);
68
69#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
71#[repr(C)]
72#[non_exhaustive]
73pub struct MapIterVTable {
74 pub next: MapIterNextFn,
76
77 pub dealloc: MapIterDeallocFn,
79}
80
81impl MapIterVTable {
82 pub const fn builder() -> MapIterVTableBuilder {
84 MapIterVTableBuilder::new()
85 }
86}
87
88pub struct MapIterVTableBuilder {
90 next: Option<MapIterNextFn>,
91 dealloc: Option<MapIterDeallocFn>,
92}
93
94impl MapIterVTableBuilder {
95 #[allow(clippy::new_without_default)]
97 pub const fn new() -> Self {
98 Self {
99 next: None,
100 dealloc: None,
101 }
102 }
103
104 pub const fn next(mut self, f: MapIterNextFn) -> Self {
106 self.next = Some(f);
107 self
108 }
109
110 pub const fn dealloc(mut self, f: MapIterDeallocFn) -> Self {
112 self.dealloc = Some(f);
113 self
114 }
115
116 pub const fn build(self) -> MapIterVTable {
122 MapIterVTable {
123 next: self.next.unwrap(),
124 dealloc: self.dealloc.unwrap(),
125 }
126 }
127}
128
129#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
131#[repr(C)]
132pub struct MapVTable {
133 pub init_in_place_with_capacity_fn: MapInitInPlaceWithCapacityFn,
135
136 pub insert_fn: MapInsertFn,
138
139 pub len_fn: MapLenFn,
141
142 pub contains_key_fn: MapContainsKeyFn,
144
145 pub get_value_ptr_fn: MapGetValuePtrFn,
147
148 pub iter_fn: MapIterFn,
150
151 pub iter_vtable: MapIterVTable,
153}
154
155impl MapVTable {
156 pub const fn builder() -> MapVTableBuilder {
158 MapVTableBuilder::new()
159 }
160}
161
162pub struct MapVTableBuilder {
164 init_in_place_with_capacity_fn: Option<MapInitInPlaceWithCapacityFn>,
165 insert_fn: Option<MapInsertFn>,
166 len_fn: Option<MapLenFn>,
167 contains_key_fn: Option<MapContainsKeyFn>,
168 get_value_ptr_fn: Option<MapGetValuePtrFn>,
169 iter_fn: Option<MapIterFn>,
170 iter_vtable: Option<MapIterVTable>,
171}
172
173impl MapVTableBuilder {
174 #[allow(clippy::new_without_default)]
176 pub const fn new() -> Self {
177 Self {
178 init_in_place_with_capacity_fn: None,
179 insert_fn: None,
180 len_fn: None,
181 contains_key_fn: None,
182 get_value_ptr_fn: None,
183 iter_fn: None,
184 iter_vtable: None,
185 }
186 }
187
188 pub const fn init_in_place_with_capacity(mut self, f: MapInitInPlaceWithCapacityFn) -> Self {
190 self.init_in_place_with_capacity_fn = Some(f);
191 self
192 }
193
194 pub const fn insert(mut self, f: MapInsertFn) -> Self {
196 self.insert_fn = Some(f);
197 self
198 }
199
200 pub const fn len(mut self, f: MapLenFn) -> Self {
202 self.len_fn = Some(f);
203 self
204 }
205
206 pub const fn contains_key(mut self, f: MapContainsKeyFn) -> Self {
208 self.contains_key_fn = Some(f);
209 self
210 }
211
212 pub const fn get_value_ptr(mut self, f: MapGetValuePtrFn) -> Self {
214 self.get_value_ptr_fn = Some(f);
215 self
216 }
217
218 pub const fn iter(mut self, f: MapIterFn) -> Self {
220 self.iter_fn = Some(f);
221 self
222 }
223
224 pub const fn iter_vtable(mut self, vtable: MapIterVTable) -> Self {
226 self.iter_vtable = Some(vtable);
227 self
228 }
229
230 pub const fn build(self) -> MapVTable {
236 MapVTable {
237 init_in_place_with_capacity_fn: self.init_in_place_with_capacity_fn.unwrap(),
238 insert_fn: self.insert_fn.unwrap(),
239 len_fn: self.len_fn.unwrap(),
240 contains_key_fn: self.contains_key_fn.unwrap(),
241 get_value_ptr_fn: self.get_value_ptr_fn.unwrap(),
242 iter_fn: self.iter_fn.unwrap(),
243 iter_vtable: self.iter_vtable.unwrap(),
244 }
245 }
246}