Skip to main content

facet_core/impls/std/
hashmap.rs

1use core::hash::BuildHasher;
2use core::ptr::NonNull;
3use std::collections::HashMap;
4use std::hash::RandomState;
5
6use crate::{PtrConst, PtrMut, PtrUninit};
7
8use crate::{
9    Def, Facet, IterVTable, MapDef, MapVTable, Shape, ShapeBuilder, Type, TypeNameFn, TypeNameOpts,
10    TypeOpsIndirect, TypeParam, UserType, VTableDirect, VTableIndirect, Variance, VarianceDep,
11    VarianceDesc,
12};
13
14type HashMapIterator<'mem, K, V> = std::collections::hash_map::Iter<'mem, K, V>;
15
16unsafe fn hashmap_init_in_place_with_capacity<K, V, S: Default + BuildHasher>(
17    uninit: PtrUninit,
18    capacity: usize,
19) -> PtrMut {
20    unsafe {
21        uninit.put(HashMap::<K, V, S>::with_capacity_and_hasher(
22            capacity,
23            S::default(),
24        ))
25    }
26}
27
28unsafe fn hashmap_insert<K: Eq + core::hash::Hash, V>(ptr: PtrMut, key: PtrMut, value: PtrMut) {
29    let map = unsafe { ptr.as_mut::<HashMap<K, V>>() };
30    let key = unsafe { key.read::<K>() };
31    let value = unsafe { value.read::<V>() };
32    map.insert(key, value);
33}
34
35unsafe fn hashmap_len<K, V>(ptr: PtrConst) -> usize {
36    unsafe { ptr.get::<HashMap<K, V>>().len() }
37}
38
39unsafe fn hashmap_contains_key<K: Eq + core::hash::Hash, V>(ptr: PtrConst, key: PtrConst) -> bool {
40    unsafe { ptr.get::<HashMap<K, V>>().contains_key(key.get()) }
41}
42
43unsafe fn hashmap_get_value_ptr<K: Eq + core::hash::Hash, V>(
44    ptr: PtrConst,
45    key: PtrConst,
46) -> Option<PtrConst> {
47    unsafe {
48        ptr.get::<HashMap<K, V>>()
49            .get(key.get())
50            .map(|v| PtrConst::new(NonNull::from(v).as_ptr()))
51    }
52}
53
54/// Build a HashMap from a contiguous slice of (K, V) pairs.
55///
56/// This uses `from_iter` with known capacity to avoid rehashing.
57unsafe fn hashmap_from_pair_slice<K: Eq + core::hash::Hash, V, S: Default + BuildHasher>(
58    uninit: PtrUninit,
59    pairs_ptr: *mut u8,
60    count: usize,
61) -> PtrMut {
62    // Create an iterator that reads and moves (K, V) pairs from the buffer
63    let pairs = pairs_ptr as *mut (K, V);
64    let iter = (0..count).map(|i| unsafe {
65        let pair_ptr = pairs.add(i);
66        core::ptr::read(pair_ptr)
67    });
68
69    // Build HashMap with from_iter (which uses reserve internally)
70    let map: HashMap<K, V, S> = iter.collect();
71    unsafe { uninit.put(map) }
72}
73
74unsafe fn hashmap_iter_init<K, V>(ptr: PtrConst) -> PtrMut {
75    unsafe {
76        let map = ptr.get::<HashMap<K, V>>();
77        let iter: HashMapIterator<'_, K, V> = map.iter();
78        let iter_state = Box::new(iter);
79        PtrMut::new(Box::into_raw(iter_state) as *mut u8)
80    }
81}
82
83unsafe fn hashmap_iter_next<K, V>(iter_ptr: PtrMut) -> Option<(PtrConst, PtrConst)> {
84    unsafe {
85        // SAFETY: We're extending the lifetime from '_ to 'static through a raw pointer cast.
86        // This is sound because:
87        // 1. The iterator was allocated in hashmap_iter_init and lives until hashmap_iter_dealloc
88        // 2. We only return pointers (PtrConst), not references with the extended lifetime
89        // 3. The actual lifetime of the data is managed by the HashMap, not this iterator
90        let ptr = iter_ptr.as_mut_ptr::<HashMapIterator<'_, K, V>>();
91        let state = &mut *ptr;
92        state.next().map(|(key, value)| {
93            (
94                PtrConst::new(NonNull::from(key).as_ptr()),
95                PtrConst::new(NonNull::from(value).as_ptr()),
96            )
97        })
98    }
99}
100
101unsafe fn hashmap_iter_dealloc<K, V>(iter_ptr: PtrMut) {
102    unsafe {
103        drop(Box::from_raw(
104            iter_ptr.as_ptr::<HashMapIterator<'_, K, V>>() as *mut HashMapIterator<'_, K, V>,
105        ));
106    }
107}
108
109unsafe fn hashmap_drop<K, V, S>(ox: crate::OxPtrMut) {
110    unsafe {
111        core::ptr::drop_in_place(ox.as_mut::<HashMap<K, V, S>>());
112    }
113}
114
115unsafe fn hashmap_default<K, V, S: Default + BuildHasher>(ox: crate::OxPtrMut) {
116    unsafe { ox.ptr().as_uninit().put(HashMap::<K, V, S>::default()) };
117}
118
119unsafe fn hashmap_is_truthy<K, V>(ptr: PtrConst) -> bool {
120    !unsafe { ptr.get::<HashMap<K, V>>().is_empty() }
121}
122
123// TODO: Debug, PartialEq, Eq for HashMap, HashSet
124unsafe impl<'a, K, V, S> Facet<'a> for HashMap<K, V, S>
125where
126    K: Facet<'a> + core::cmp::Eq + core::hash::Hash,
127    V: Facet<'a>,
128    S: 'a + Default + BuildHasher,
129{
130    const SHAPE: &'static Shape = &const {
131        const fn build_map_vtable<K: Eq + core::hash::Hash, V, S: Default + BuildHasher>()
132        -> MapVTable {
133            MapVTable::builder()
134                .init_in_place_with_capacity(hashmap_init_in_place_with_capacity::<K, V, S>)
135                .insert(hashmap_insert::<K, V>)
136                .len(hashmap_len::<K, V>)
137                .contains_key(hashmap_contains_key::<K, V>)
138                .get_value_ptr(hashmap_get_value_ptr::<K, V>)
139                .iter_vtable(IterVTable {
140                    init_with_value: Some(hashmap_iter_init::<K, V>),
141                    next: hashmap_iter_next::<K, V>,
142                    next_back: None,
143                    size_hint: None,
144                    dealloc: hashmap_iter_dealloc::<K, V>,
145                })
146                .from_pair_slice(Some(hashmap_from_pair_slice::<K, V, S>))
147                .pair_stride(core::mem::size_of::<(K, V)>())
148                .value_offset_in_pair(core::mem::offset_of!((K, V), 1))
149                .build()
150        }
151
152        const fn build_type_name<'a, K: Facet<'a>, V: Facet<'a>>() -> TypeNameFn {
153            fn type_name_impl<'a, K: Facet<'a>, V: Facet<'a>>(
154                _shape: &'static Shape,
155                f: &mut core::fmt::Formatter<'_>,
156                opts: TypeNameOpts,
157            ) -> core::fmt::Result {
158                write!(f, "HashMap")?;
159                if let Some(opts) = opts.for_children() {
160                    write!(f, "<")?;
161                    K::SHAPE.write_type_name(f, opts)?;
162                    write!(f, ", ")?;
163                    V::SHAPE.write_type_name(f, opts)?;
164                    write!(f, ">")?;
165                } else {
166                    write!(f, "<…>")?;
167                }
168                Ok(())
169            }
170            type_name_impl::<K, V>
171        }
172
173        ShapeBuilder::for_sized::<Self>("HashMap")
174            .module_path("std::collections::hash_map")
175            .type_name(build_type_name::<K, V>())
176            .ty(Type::User(UserType::Opaque))
177            .def(Def::Map(MapDef {
178                vtable: &const { build_map_vtable::<K, V, S>() },
179                k: K::SHAPE,
180                v: V::SHAPE,
181            }))
182            .type_params(&[
183                TypeParam {
184                    name: "K",
185                    shape: K::SHAPE,
186                },
187                TypeParam {
188                    name: "V",
189                    shape: V::SHAPE,
190                },
191            ])
192            // HashMap<K, V> combines K and V variances
193            .variance(VarianceDesc {
194                base: Variance::Bivariant,
195                deps: &const {
196                    [
197                        VarianceDep::covariant(K::SHAPE),
198                        VarianceDep::covariant(V::SHAPE),
199                    ]
200                },
201            })
202            .vtable_indirect(&VTableIndirect::EMPTY)
203            .type_ops_indirect(
204                &const {
205                    TypeOpsIndirect {
206                        drop_in_place: hashmap_drop::<K, V, S>,
207                        default_in_place: Some(hashmap_default::<K, V, S>),
208                        clone_into: None,
209                        is_truthy: Some(hashmap_is_truthy::<K, V>),
210                    }
211                },
212            )
213            .build()
214    };
215}
216
217unsafe impl Facet<'_> for RandomState {
218    const SHAPE: &'static Shape = &const {
219        const VTABLE: VTableDirect = VTableDirect::empty();
220
221        ShapeBuilder::for_sized::<Self>("RandomState")
222            .module_path("std::hash")
223            .ty(Type::User(UserType::Opaque))
224            .def(Def::Scalar)
225            .vtable_direct(&VTABLE)
226            .build()
227    };
228}