facet_core/impls_std/
hashmap.rs

1use alloc::collections::VecDeque;
2use core::{alloc::Layout, hash::Hash};
3use std::collections::HashMap;
4use std::hash::RandomState;
5
6use crate::ptr::{PtrConst, PtrMut};
7
8use crate::{
9    ConstTypeId, Def, Facet, MapDef, MapIterVTable, MapVTable, MarkerTraits, ScalarAffinity,
10    ScalarDef, Shape, ValueVTable, value_vtable,
11};
12
13struct HashMapIterator<'mem, K> {
14    map: PtrConst<'mem>,
15    keys: VecDeque<&'mem K>,
16}
17
18unsafe impl<K, V, S> Facet for HashMap<K, V, S>
19where
20    K: Facet + core::cmp::Eq + core::hash::Hash + 'static,
21    V: Facet + 'static,
22    S: Facet + Default,
23{
24    const SHAPE: &'static Shape = &const {
25        Shape::builder()
26            .id(ConstTypeId::of::<HashMap<K, V, S>>())
27            .layout(Layout::new::<HashMap<K, V>>())
28            .vtable(
29                &const {
30                    let mut builder = ValueVTable::builder()
31                        .marker_traits({
32                            let arg_dependent_traits = MarkerTraits::SEND
33                                .union(MarkerTraits::SYNC)
34                                .union(MarkerTraits::EQ)
35                                .union(MarkerTraits::UNPIN);
36                            arg_dependent_traits
37                                .intersection(V::SHAPE.vtable.marker_traits)
38                                .intersection(K::SHAPE.vtable.marker_traits)
39                        })
40                        .type_name(|f, opts| {
41                            if let Some(opts) = opts.for_children() {
42                                write!(f, "HashMap<")?;
43                                (K::SHAPE.vtable.type_name)(f, opts)?;
44                                write!(f, ", ")?;
45                                (V::SHAPE.vtable.type_name)(f, opts)?;
46                                write!(f, ">")
47                            } else {
48                                write!(f, "HashMap<⋯>")
49                            }
50                        })
51                        .drop_in_place(|value| unsafe { value.drop_in_place::<HashMap<K, V>>() });
52
53                    if K::SHAPE.vtable.debug.is_some() && V::SHAPE.vtable.debug.is_some() {
54                        builder = builder.debug(|value, f| unsafe {
55                            let value = value.get::<HashMap<K, V>>();
56                            let k_debug = K::SHAPE.vtable.debug.unwrap_unchecked();
57                            let v_debug = V::SHAPE.vtable.debug.unwrap_unchecked();
58                            write!(f, "{{")?;
59                            for (i, (key, val)) in value.iter().enumerate() {
60                                if i > 0 {
61                                    write!(f, ", ")?;
62                                }
63                                (k_debug)(PtrConst::new(key as *const _), f)?;
64                                write!(f, ": ")?;
65                                (v_debug)(PtrConst::new(val as *const _), f)?;
66                            }
67                            write!(f, "}}")
68                        });
69                    }
70
71                    builder =
72                        builder.default_in_place(|target| unsafe { target.put(Self::default()) });
73
74                    builder = builder
75                        .clone_into(|src, dst| unsafe { dst.put(src.get::<HashMap<K, V>>()) });
76
77                    if V::SHAPE.vtable.eq.is_some() {
78                        builder = builder.eq(|a, b| unsafe {
79                            let a = a.get::<HashMap<K, V>>();
80                            let b = b.get::<HashMap<K, V>>();
81                            let v_eq = V::SHAPE.vtable.eq.unwrap_unchecked();
82                            a.len() == b.len()
83                                && a.iter().all(|(key_a, val_a)| {
84                                    b.get(key_a).is_some_and(|val_b| {
85                                        (v_eq)(
86                                            PtrConst::new(val_a as *const _),
87                                            PtrConst::new(val_b as *const _),
88                                        )
89                                    })
90                                })
91                        });
92                    }
93
94                    if V::SHAPE.vtable.hash.is_some() {
95                        builder = builder.hash(|value, hasher_this, hasher_write_fn| unsafe {
96                            use crate::HasherProxy;
97                            let map = value.get::<HashMap<K, V>>();
98                            let v_hash = V::SHAPE.vtable.hash.unwrap_unchecked();
99                            let mut hasher = HasherProxy::new(hasher_this, hasher_write_fn);
100                            map.len().hash(&mut hasher);
101                            for (k, v) in map {
102                                k.hash(&mut hasher);
103                                (v_hash)(
104                                    PtrConst::new(v as *const _),
105                                    hasher_this,
106                                    hasher_write_fn,
107                                );
108                            }
109                        });
110                    }
111
112                    builder.build()
113                },
114            )
115            .def(Def::Map(
116                MapDef::builder()
117                    .k(K::SHAPE)
118                    .v(V::SHAPE)
119                    .vtable(
120                        &const {
121                            MapVTable::builder()
122                                .init_in_place_with_capacity(|uninit, capacity| unsafe {
123                                    uninit
124                                        .put(Self::with_capacity_and_hasher(capacity, S::default()))
125                                })
126                                .insert(|ptr, key, value| unsafe {
127                                    let map = ptr.as_mut::<HashMap<K, V>>();
128                                    let key = key.read::<K>();
129                                    let value = value.read::<V>();
130                                    map.insert(key, value);
131                                })
132                                .len(|ptr| unsafe {
133                                    let map = ptr.get::<HashMap<K, V>>();
134                                    map.len()
135                                })
136                                .contains_key(|ptr, key| unsafe {
137                                    let map = ptr.get::<HashMap<K, V>>();
138                                    map.contains_key(key.get())
139                                })
140                                .get_value_ptr(|ptr, key| unsafe {
141                                    let map = ptr.get::<HashMap<K, V>>();
142                                    map.get(key.get()).map(|v| PtrConst::new(v as *const _))
143                                })
144                                .iter(|ptr| unsafe {
145                                    let map = ptr.get::<HashMap<K, V>>();
146                                    let keys: VecDeque<&K> = map.keys().collect();
147                                    let iter_state = Box::new(HashMapIterator { map: ptr, keys });
148                                    PtrMut::new(Box::into_raw(iter_state) as *mut u8)
149                                })
150                                .iter_vtable(
151                                    MapIterVTable::builder()
152                                        .next(|iter_ptr| unsafe {
153                                            let state = iter_ptr.as_mut::<HashMapIterator<'_, K>>();
154                                            let map = state.map.get::<HashMap<K, V>>();
155                                            while let Some(key) = state.keys.pop_front() {
156                                                if let Some(value) = map.get(key) {
157                                                    return Some((
158                                                        PtrConst::new(key as *const K),
159                                                        PtrConst::new(value as *const V),
160                                                    ));
161                                                }
162                                            }
163
164                                            None
165                                        })
166                                        .dealloc(|iter_ptr| unsafe {
167                                            drop(Box::from_raw(
168                                                iter_ptr.as_ptr::<HashMapIterator<'_, K>>()
169                                                    as *mut HashMapIterator<'_, K>,
170                                            ));
171                                        })
172                                        .build(),
173                                )
174                                .build()
175                        },
176                    )
177                    .build(),
178            ))
179            .build()
180    };
181}
182
183unsafe impl Facet for RandomState {
184    const SHAPE: &'static Shape = &const {
185        Shape::builder()
186            .id(ConstTypeId::of::<RandomState>())
187            .layout(Layout::new::<Self>())
188            .def(Def::Scalar(
189                ScalarDef::builder()
190                    .affinity(ScalarAffinity::opaque().build())
191                    .build(),
192            ))
193            .vtable(value_vtable!((), |f, _opts| write!(f, "RandomState")))
194            .build()
195    };
196}