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