facet_core/types/def/
map.rs1use crate::ptr::{PtrConst, PtrMut, PtrUninit};
2
3use super::{IterVTable, Shape};
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(C)]
8#[non_exhaustive]
9pub struct MapDef<'shape> {
10 pub vtable: &'shape MapVTable,
12 pub k: fn() -> &'shape Shape<'shape>,
14 pub v: fn() -> &'shape Shape<'shape>,
16}
17
18impl<'shape> MapDef<'shape> {
19 pub const fn builder() -> MapDefBuilder<'shape> {
21 MapDefBuilder::new()
22 }
23
24 pub fn k(&self) -> &'shape Shape<'shape> {
26 (self.k)()
27 }
28
29 pub fn v(&self) -> &'shape Shape<'shape> {
31 (self.v)()
32 }
33}
34
35pub struct MapDefBuilder<'shape> {
37 vtable: Option<&'shape MapVTable>,
38 k: Option<fn() -> &'shape Shape<'shape>>,
39 v: Option<fn() -> &'shape Shape<'shape>>,
40}
41
42impl<'shape> MapDefBuilder<'shape> {
43 #[allow(clippy::new_without_default)]
45 pub const fn new() -> Self {
46 Self {
47 vtable: None,
48 k: None,
49 v: None,
50 }
51 }
52
53 pub const fn vtable(mut self, vtable: &'shape MapVTable) -> Self {
55 self.vtable = Some(vtable);
56 self
57 }
58
59 pub const fn k(mut self, k: fn() -> &'shape Shape<'shape>) -> Self {
61 self.k = Some(k);
62 self
63 }
64
65 pub const fn v(mut self, v: fn() -> &'shape Shape<'shape>) -> Self {
67 self.v = Some(v);
68 self
69 }
70
71 pub const fn build(self) -> MapDef<'shape> {
73 MapDef {
74 vtable: self.vtable.unwrap(),
75 k: self.k.unwrap(),
76 v: self.v.unwrap(),
77 }
78 }
79}
80
81pub type MapInitInPlaceWithCapacityFn =
88 for<'mem> unsafe fn(map: PtrUninit<'mem>, capacity: usize) -> PtrMut<'mem>;
89
90pub type MapInsertFn =
98 for<'map, 'key, 'value> unsafe fn(map: PtrMut<'map>, key: PtrMut<'key>, value: PtrMut<'value>);
99
100pub type MapLenFn = for<'map> unsafe fn(map: PtrConst<'map>) -> usize;
106
107pub type MapContainsKeyFn =
113 for<'map, 'key> unsafe fn(map: PtrConst<'map>, key: PtrConst<'key>) -> bool;
114
115pub type MapGetValuePtrFn =
121 for<'map, 'key> unsafe fn(map: PtrConst<'map>, key: PtrConst<'key>) -> Option<PtrConst<'map>>;
122
123#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
125#[repr(C)]
126pub struct MapVTable {
127 pub init_in_place_with_capacity_fn: MapInitInPlaceWithCapacityFn,
129
130 pub insert_fn: MapInsertFn,
132
133 pub len_fn: MapLenFn,
135
136 pub contains_key_fn: MapContainsKeyFn,
138
139 pub get_value_ptr_fn: MapGetValuePtrFn,
141
142 pub iter_vtable: IterVTable<(PtrConst<'static>, PtrConst<'static>)>,
144}
145
146impl MapVTable {
147 pub const fn builder() -> MapVTableBuilder {
149 MapVTableBuilder::new()
150 }
151}
152
153pub struct MapVTableBuilder {
155 init_in_place_with_capacity_fn: Option<MapInitInPlaceWithCapacityFn>,
156 insert_fn: Option<MapInsertFn>,
157 len_fn: Option<MapLenFn>,
158 contains_key_fn: Option<MapContainsKeyFn>,
159 get_value_ptr_fn: Option<MapGetValuePtrFn>,
160 iter_vtable: Option<IterVTable<(PtrConst<'static>, PtrConst<'static>)>>,
161}
162
163impl MapVTableBuilder {
164 #[allow(clippy::new_without_default)]
166 pub const fn new() -> Self {
167 Self {
168 init_in_place_with_capacity_fn: None,
169 insert_fn: None,
170 len_fn: None,
171 contains_key_fn: None,
172 get_value_ptr_fn: None,
173 iter_vtable: None,
174 }
175 }
176
177 pub const fn init_in_place_with_capacity(mut self, f: MapInitInPlaceWithCapacityFn) -> Self {
179 self.init_in_place_with_capacity_fn = Some(f);
180 self
181 }
182
183 pub const fn insert(mut self, f: MapInsertFn) -> Self {
185 self.insert_fn = Some(f);
186 self
187 }
188
189 pub const fn len(mut self, f: MapLenFn) -> Self {
191 self.len_fn = Some(f);
192 self
193 }
194
195 pub const fn contains_key(mut self, f: MapContainsKeyFn) -> Self {
197 self.contains_key_fn = Some(f);
198 self
199 }
200
201 pub const fn get_value_ptr(mut self, f: MapGetValuePtrFn) -> Self {
203 self.get_value_ptr_fn = Some(f);
204 self
205 }
206
207 pub const fn iter_vtable(
209 mut self,
210 vtable: IterVTable<(PtrConst<'static>, PtrConst<'static>)>,
211 ) -> Self {
212 self.iter_vtable = Some(vtable);
213 self
214 }
215
216 pub const fn build(self) -> MapVTable {
222 MapVTable {
223 init_in_place_with_capacity_fn: self.init_in_place_with_capacity_fn.unwrap(),
224 insert_fn: self.insert_fn.unwrap(),
225 len_fn: self.len_fn.unwrap(),
226 contains_key_fn: self.contains_key_fn.unwrap(),
227 get_value_ptr_fn: self.get_value_ptr_fn.unwrap(),
228 iter_vtable: self.iter_vtable.unwrap(),
229 }
230 }
231}