• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 17206018795

25 Aug 2025 10:18AM UTC coverage: 54.242% (-0.01%) from 54.253%
17206018795

push

github

fasterthanlime
Relax S: Facet constraint for HashMap impl

This allows the impl to cover third-party hashers like ahash.
On the flip side, we lose type information for the hasher itself.

Closes #867.

4776 of 8805 relevant lines covered (54.24%)

34.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.44
/facet-core/src/impls_std/hashmap.rs
1
use core::hash::BuildHasher;
2
use std::collections::HashMap;
3
use std::hash::RandomState;
4

5
use crate::ptr::{PtrConst, PtrMut};
6

7
use crate::{
8
    Def, Facet, IterVTable, MapDef, MapVTable, MarkerTraits, Shape, Type, TypeParam, UserType,
9
    ValueVTable, value_vtable,
10
};
11

12
type HashMapIterator<'mem, K, V> = std::collections::hash_map::Iter<'mem, K, V>;
13

14
unsafe impl<'a, K, V, S> Facet<'a> for HashMap<K, V, S>
15
where
16
    K: Facet<'a> + core::cmp::Eq + core::hash::Hash,
17
    V: Facet<'a>,
18
    S: 'a + Default + BuildHasher,
19
{
20
    const VTABLE: &'static ValueVTable = &const {
21
        ValueVTable::builder::<Self>()
22
            .marker_traits(|| {
×
23
                let arg_dependent_traits = MarkerTraits::SEND
×
24
                    .union(MarkerTraits::SYNC)
×
25
                    .union(MarkerTraits::EQ)
×
26
                    .union(MarkerTraits::UNPIN)
×
27
                    .union(MarkerTraits::UNWIND_SAFE)
×
28
                    .union(MarkerTraits::REF_UNWIND_SAFE);
×
29
                arg_dependent_traits
×
30
                    .intersection(V::SHAPE.vtable.marker_traits())
×
31
                    .intersection(K::SHAPE.vtable.marker_traits())
×
32
            })
×
33
            .type_name(|f, opts| {
1✔
34
                if let Some(opts) = opts.for_children() {
1✔
35
                    write!(f, "{}<", Self::SHAPE.type_identifier)?;
1✔
36
                    K::SHAPE.vtable.type_name()(f, opts)?;
1✔
37
                    write!(f, ", ")?;
1✔
38
                    V::SHAPE.vtable.type_name()(f, opts)?;
1✔
39
                    write!(f, ">")
1✔
40
                } else {
41
                    write!(f, "{}<⋯>", Self::SHAPE.type_identifier)
×
42
                }
43
            })
1✔
44
            .default_in_place(|| Some(|target| unsafe { target.put(Self::default()) }))
×
45
            .build()
46
    };
47

48
    const SHAPE: &'static Shape = &const {
49
        Shape::builder_for_sized::<Self>()
50
            .type_identifier("HashMap")
51
            .type_params(&[
52
                TypeParam {
53
                    name: "K",
54
                    shape: || K::SHAPE,
55
                },
56
                TypeParam {
57
                    name: "V",
58
                    shape: || V::SHAPE,
59
                },
60
            ])
61
            .ty(Type::User(UserType::Opaque))
62
            .def(Def::Map(
63
                MapDef::builder()
64
                    .k(|| K::SHAPE)
65
                    .v(|| V::SHAPE)
66
                    .vtable(
67
                        &const {
68
                            MapVTable::builder()
69
                                .init_in_place_with_capacity(|uninit, capacity| unsafe {
70
                                    uninit
12✔
71
                                        .put(Self::with_capacity_and_hasher(capacity, S::default()))
12✔
72
                                })
12✔
73
                                .insert(|ptr, key, value| unsafe {
74
                                    let map = ptr.as_mut::<HashMap<K, V>>();
8✔
75
                                    let key = key.read::<K>();
8✔
76
                                    let value = value.read::<V>();
8✔
77
                                    map.insert(key, value);
8✔
78
                                })
8✔
79
                                .len(|ptr| unsafe {
80
                                    let map = ptr.get::<HashMap<K, V>>();
5✔
81
                                    map.len()
5✔
82
                                })
5✔
83
                                .contains_key(|ptr, key| unsafe {
84
                                    let map = ptr.get::<HashMap<K, V>>();
8✔
85
                                    map.contains_key(key.get())
8✔
86
                                })
8✔
87
                                .get_value_ptr(|ptr, key| unsafe {
88
                                    let map = ptr.get::<HashMap<K, V>>();
8✔
89
                                    map.get(key.get()).map(|v| PtrConst::new(v))
8✔
90
                                })
8✔
91
                                .iter_vtable(
92
                                    IterVTable::builder()
93
                                        .init_with_value(|ptr| unsafe {
94
                                            let map = ptr.get::<HashMap<K, V>>();
1✔
95
                                            let iter: HashMapIterator<'_, K, V> = map.iter();
1✔
96
                                            let iter_state = Box::new(iter);
1✔
97
                                            PtrMut::new(Box::into_raw(iter_state) as *mut u8)
1✔
98
                                        })
1✔
99
                                        .next(|iter_ptr| unsafe {
100
                                            let state =
3✔
101
                                                iter_ptr.as_mut::<HashMapIterator<'_, K, V>>();
3✔
102
                                            state.next().map(|(key, value)| {
3✔
103
                                                (
2✔
104
                                                    PtrConst::new(key as *const K),
2✔
105
                                                    PtrConst::new(value as *const V),
2✔
106
                                                )
2✔
107
                                            })
2✔
108
                                        })
3✔
109
                                        .dealloc(|iter_ptr| unsafe {
110
                                            drop(Box::from_raw(
1✔
111
                                                iter_ptr.as_ptr::<HashMapIterator<'_, K, V>>()
1✔
112
                                                    as *mut HashMapIterator<'_, K, V>,
1✔
113
                                            ));
114
                                        })
1✔
115
                                        .build(),
116
                                )
117
                                .build()
118
                        },
119
                    )
120
                    .build(),
121
            ))
122
            .build()
123
    };
124
}
125

126
unsafe impl Facet<'_> for RandomState {
127
    const VTABLE: &'static ValueVTable =
128
        &const { value_vtable!((), |f, _opts| write!(f, "{}", Self::SHAPE.type_identifier)) };
×
129

130
    const SHAPE: &'static Shape = &const {
131
        Shape::builder_for_sized::<Self>()
132
            .type_identifier("RandomState")
133
            .ty(Type::User(UserType::Opaque))
134
            .def(Def::Scalar)
135
            .build()
136
    };
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc