facet_reflect/poke/
map.rs1use crate::PokeValueUninit;
2use facet_core::{MapDef, MapVTable, Opaque, OpaqueConst, OpaqueUninit, Shape};
3
4pub struct PokeMapUninit<'mem> {
6 data: OpaqueUninit<'mem>,
7 shape: &'static Shape,
8 def: MapDef,
9}
10
11impl<'mem> PokeMapUninit<'mem> {
12 #[inline(always)]
13 pub fn into_value(self) -> PokeValueUninit<'mem> {
15 unsafe { PokeValueUninit::new(self.data, self.shape) }
16 }
17
18 #[inline(always)]
19 pub fn shape(&self) -> &'static Shape {
21 self.shape
22 }
23 pub(crate) unsafe fn new(data: OpaqueUninit<'mem>, shape: &'static Shape, def: MapDef) -> Self {
29 Self { data, shape, def }
30 }
31
32 pub fn init(self, size_hint: Option<usize>) -> Result<PokeMap<'mem>, OpaqueUninit<'mem>> {
34 let res = if let Some(capacity) = size_hint {
35 let init_in_place_with_capacity = self.def.vtable.init_in_place_with_capacity_fn;
36 unsafe { init_in_place_with_capacity(self.data, capacity) }
37 } else {
38 let pv = unsafe { PokeValueUninit::new(self.data, self.shape) };
39 pv.default_in_place().map_err(|_| ())
40 };
41 let data = res.map_err(|_| self.data)?;
42 Ok(unsafe { PokeMap::new(data, self.shape, self.def) })
43 }
44}
45
46pub struct PokeMap<'mem> {
48 data: Opaque<'mem>,
49 #[allow(dead_code)]
50 shape: &'static Shape,
51 def: MapDef,
52}
53
54impl<'mem> PokeMap<'mem> {
55 #[inline]
61 pub(crate) unsafe fn new(data: Opaque<'mem>, shape: &'static Shape, def: MapDef) -> Self {
62 Self { data, shape, def }
63 }
64
65 #[inline(always)]
67 pub fn shape(&self) -> &'static Shape {
68 self.shape
69 }
70
71 #[inline(always)]
73 pub fn map_vtable(&self) -> &'static MapVTable {
74 self.def.vtable
75 }
76
77 #[inline]
84 pub unsafe fn insert(&mut self, key: Opaque<'_>, value: Opaque<'_>) {
85 unsafe { (self.map_vtable().insert_fn)(self.data, key, value) }
86 }
87
88 #[inline]
90 pub fn len(&self) -> usize {
91 unsafe { (self.map_vtable().len_fn)(self.data.as_const()) }
92 }
93
94 #[inline]
96 pub fn is_empty(&self) -> bool {
97 self.len() == 0
98 }
99
100 #[inline]
102 pub fn contains_key(&self, key: OpaqueConst<'_>) -> bool {
103 unsafe { (self.map_vtable().contains_key_fn)(self.data.as_const(), key) }
104 }
105
106 #[inline]
110 pub fn get_value_ptr<'key>(&self, key: OpaqueConst<'key>) -> Option<OpaqueConst<'mem>> {
111 unsafe { (self.map_vtable().get_value_ptr_fn)(self.data.as_const(), key) }
112 }
113
114 pub fn build_in_place(self) -> Opaque<'mem> {
116 self.data
117 }
118
119 #[inline]
121 pub fn def(&self) -> &MapDef {
122 &self.def
123 }
124}