facet_core/types/def/map.rs
1use super::{PtrConst, PtrMut, PtrUninit};
2
3use super::{IterVTable, Shape};
4
5/// Fields for map types
6#[derive(Clone, Copy, Debug)]
7#[repr(C)]
8pub struct MapDef {
9 /// vtable for interacting with the map
10 pub vtable: &'static MapVTable,
11 /// shape of the keys in the map
12 pub k: &'static Shape,
13 /// shape of the values in the map
14 pub v: &'static Shape,
15}
16
17impl MapDef {
18 /// Construct a `MapDef` from its vtable and key/value shapes.
19 pub const fn new(vtable: &'static MapVTable, k: &'static Shape, v: &'static Shape) -> Self {
20 Self { vtable, k, v }
21 }
22
23 /// Returns the shape of the keys of the map
24 pub const fn k(&self) -> &'static Shape {
25 self.k
26 }
27
28 /// Returns the shape of the values of the map
29 pub const fn v(&self) -> &'static Shape {
30 self.v
31 }
32}
33
34/// Initialize a map in place with a given capacity
35///
36/// # Safety
37///
38/// The `map` parameter must point to uninitialized memory of sufficient size.
39/// The function must properly initialize the memory.
40pub type MapInitInPlaceWithCapacityFn =
41 unsafe extern "C" fn(map: PtrUninit, capacity: usize) -> PtrMut;
42
43/// Insert a key-value pair into the map
44///
45/// # Safety
46///
47/// The `map` parameter must point to aligned, initialized memory of the correct type.
48/// `key` and `value` are moved out of (with [`core::ptr::read`]) — they should be deallocated
49/// afterwards (e.g. with [`core::mem::forget`]) but NOT dropped.
50pub type MapInsertFn = unsafe extern "C" fn(map: PtrMut, key: PtrMut, value: PtrMut);
51
52/// Get the number of entries in the map
53///
54/// # Safety
55///
56/// The `map` parameter must point to aligned, initialized memory of the correct type.
57pub type MapLenFn = unsafe extern "C" fn(map: PtrConst) -> usize;
58
59/// Check if the map contains a key
60///
61/// # Safety
62///
63/// The `map` parameter must point to aligned, initialized memory of the correct type.
64pub type MapContainsKeyFn = unsafe extern "C" fn(map: PtrConst, key: PtrConst) -> bool;
65
66/// Get pointer to a value for a given key, returns None if not found
67///
68/// # Safety
69///
70/// The `map` parameter must point to aligned, initialized memory of the correct type.
71pub type MapGetValuePtrFn = unsafe extern "C" fn(map: PtrConst, key: PtrConst) -> *const u8;
72
73/// Build a map from a contiguous slice of (K, V) pairs.
74///
75/// This is an optimization for JIT deserialization that avoids per-entry vtable
76/// calls and rehashing by building the map with known capacity in one shot.
77///
78/// # Safety
79///
80/// - `map` must point to uninitialized memory of sufficient size for the map.
81/// - `pairs_ptr` must point to `count` contiguous (K, V) tuples, properly aligned.
82/// - Keys and values are moved out via `ptr::read` - the memory should be deallocated
83/// but not dropped afterwards.
84pub type MapFromPairSliceFn =
85 unsafe extern "C" fn(map: PtrUninit, pairs_ptr: *mut u8, count: usize) -> PtrMut;
86
87vtable_def! {
88 /// Virtual table for a Map<K, V>
89 #[derive(Clone, Copy, Debug)]
90 #[repr(C)]
91 pub struct MapVTable + MapVTableBuilder {
92 /// cf. [`MapInitInPlaceWithCapacityFn`]
93 pub init_in_place_with_capacity: MapInitInPlaceWithCapacityFn,
94
95 /// cf. [`MapInsertFn`]
96 pub insert: MapInsertFn,
97
98 /// cf. [`MapLenFn`]
99 pub len: MapLenFn,
100
101 /// cf. [`MapContainsKeyFn`]
102 pub contains_key: MapContainsKeyFn,
103
104 /// cf. [`MapGetValuePtrFn`]
105 pub get_value_ptr: MapGetValuePtrFn,
106
107 /// Virtual table for map iterator operations
108 pub iter_vtable: IterVTable<(PtrConst, PtrConst)>,
109
110 /// cf. [`MapFromPairSliceFn`] - optional optimization for JIT
111 pub from_pair_slice: Option<MapFromPairSliceFn>,
112
113 /// Size of (K, V) tuple in bytes (for JIT buffer allocation)
114 pub pair_stride: usize,
115
116 /// Offset of V within (K, V) tuple (for JIT value placement)
117 pub value_offset_in_pair: usize,
118 }
119}