facet_types/map.rs
1use facet_opaque::{Opaque, OpaqueConst, OpaqueUninit};
2
3/// Initialize a map in place with a given capacity
4///
5/// # Safety
6///
7/// The `map` parameter must point to uninitialized memory of sufficient size.
8/// The function must properly initialize the memory.
9pub type MapInitInPlaceWithCapacityFn =
10 unsafe fn(map: OpaqueUninit, capacity: usize) -> Result<Opaque, ()>;
11
12/// Insert a key-value pair into the map
13///
14/// # Safety
15///
16/// The `map` parameter must point to aligned, initialized memory of the correct type.
17/// `key` and `value` are moved out of (with [`std::ptr::read`]) — they should be deallocated
18/// afterwards but NOT dropped.
19pub type MapInsertFn =
20 for<'map, 'key, 'value> unsafe fn(map: Opaque<'map>, key: Opaque<'key>, value: Opaque<'value>);
21
22/// Get the number of entries in the map
23///
24/// # Safety
25///
26/// The `map` parameter must point to aligned, initialized memory of the correct type.
27pub type MapLenFn = for<'map> unsafe fn(map: OpaqueConst<'map>) -> usize;
28
29/// Check if the map contains a key
30///
31/// # Safety
32///
33/// The `map` parameter must point to aligned, initialized memory of the correct type.
34pub type MapContainsKeyFn =
35 for<'map, 'key> unsafe fn(map: OpaqueConst<'map>, key: OpaqueConst<'key>) -> bool;
36
37/// Get pointer to a value for a given key, returns None if not found
38///
39/// # Safety
40///
41/// The `map` parameter must point to aligned, initialized memory of the correct type.
42pub type MapGetValuePtrFn = for<'map, 'key> unsafe fn(
43 map: OpaqueConst<'map>,
44 key: OpaqueConst<'key>,
45) -> Option<OpaqueConst<'map>>;
46
47/// Get an iterator over the map
48///
49/// # Safety
50///
51/// The `map` parameter must point to aligned, initialized memory of the correct type.
52pub type MapIterFn = for<'map> unsafe fn(map: OpaqueConst<'map>) -> OpaqueConst<'map>;
53
54/// Get the next key-value pair from the iterator
55///
56/// # Safety
57///
58/// The `iter` parameter must point to aligned, initialized memory of the correct type.
59pub type MapIterNextFn =
60 for<'iter> unsafe fn(iter: Opaque<'iter>) -> Option<(OpaqueConst<'iter>, OpaqueConst<'iter>)>;
61
62/// Deallocate the iterator
63///
64/// # Safety
65///
66/// The `iter` parameter must point to aligned, initialized memory of the correct type.
67pub type MapIterDeallocFn = for<'iter> unsafe fn(iter: Opaque<'iter>);
68
69/// VTable for an iterator over a map
70#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
71pub struct MapIterVTable {
72 /// cf. [`MapIterNextFn`]
73 pub next: MapIterNextFn,
74
75 /// cf. [`MapIterDeallocFn`]
76 pub dealloc: MapIterDeallocFn,
77}
78
79/// Virtual table for a Map<K, V>
80#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
81pub struct MapVTable {
82 /// cf. [`MapInitInPlaceWithCapacityFn`]
83 pub init_in_place_with_capacity_fn: MapInitInPlaceWithCapacityFn,
84
85 /// cf. [`MapInsertFn`]
86 pub insert_fn: MapInsertFn,
87
88 /// cf. [`MapLenFn`]
89 pub len_fn: MapLenFn,
90
91 /// cf. [`MapContainsKeyFn`]
92 pub contains_key_fn: MapContainsKeyFn,
93
94 /// cf. [`MapGetValuePtrFn`]
95 pub get_value_ptr_fn: MapGetValuePtrFn,
96
97 /// cf. [`MapIterFn`]
98 pub iter_fn: MapIterFn,
99
100 /// Virtual table for map iterator operations
101 pub iter_vtable_fn: MapIterVTable,
102}