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

facet-rs / facet / 19992174439

06 Dec 2025 05:56PM UTC coverage: 58.742% (-0.005%) from 58.747%
19992174439

Pull #1118

github

web-flow
Merge d1d251ac8 into 45a8cb1c3
Pull Request #1118: Reduce/cordon bloat in facet, introduce bloatbench

1138 of 3103 new or added lines in 61 files covered. (36.67%)

540 existing lines in 30 files now uncovered.

24225 of 41240 relevant lines covered (58.74%)

502.5 hits per line

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

92.93
/facet-core/src/impls_std/hashset.rs
1
use core::hash::BuildHasher;
2
use core::ptr::NonNull;
3
use std::collections::HashSet;
4

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

7
use crate::{
8
    Def, Facet, IterVTable, SetDef, SetVTable, Shape, Type, TypeParam, UserType, ValueVTable,
9
};
10

11
type HashSetIterator<'mem, T> = std::collections::hash_set::Iter<'mem, T>;
12

13
unsafe impl<'a, T, S> Facet<'a> for HashSet<T, S>
14
where
15
    T: Facet<'a> + core::cmp::Eq + core::hash::Hash,
16
    S: Facet<'a> + Default + BuildHasher,
17
{
18
    const SHAPE: &'static Shape = &const {
19
        Shape {
20
            id: Shape::id_of::<Self>(),
21
            layout: Shape::layout_of::<Self>(),
22
            vtable: ValueVTable {
23
                type_name: |f, opts| {
6✔
24
                    write!(f, "{}<", Self::SHAPE.type_identifier)?;
6✔
25
                    if let Some(opts) = opts.for_children() {
6✔
26
                        (T::SHAPE.vtable.type_name())(f, opts)?;
6✔
27
                    } else {
NEW
28
                        write!(f, "…")?;
×
29
                    }
30
                    write!(f, ">")
6✔
31
                },
6✔
32
                drop_in_place: ValueVTable::drop_in_place_for::<Self>(),
NEW
33
                default_in_place: Some(|target| unsafe { target.put(Self::default()) }),
×
NEW
34
                ..ValueVTable::new(|_, _| Ok(()))
×
35
            },
36
            ty: Type::User(UserType::Opaque),
37
            def: Def::Set(SetDef::new(
38
                &const {
39
                    SetVTable::new(
40
                        |uninit, capacity| unsafe {
41
                            uninit.put(Self::with_capacity_and_hasher(capacity, S::default()))
12✔
42
                        },
12✔
43
                        |ptr, item| unsafe {
44
                            let set = ptr.as_mut::<HashSet<T>>();
37✔
45
                            let item = item.read::<T>();
37✔
46
                            set.insert(item)
37✔
47
                        },
37✔
48
                        |ptr| unsafe {
49
                            let set = ptr.get::<HashSet<T>>();
13✔
50
                            set.len()
13✔
51
                        },
13✔
52
                        |ptr, item| unsafe {
NEW
53
                            let set = ptr.get::<HashSet<T>>();
×
NEW
54
                            set.contains(item.get())
×
NEW
55
                        },
×
56
                        IterVTable {
57
                            init_with_value: Some(|ptr| unsafe {
58
                                let set = ptr.get::<HashSet<T>>();
6✔
59
                                let iter: HashSetIterator<'_, T> = set.iter();
6✔
60
                                let iter_state = Box::new(iter);
6✔
61
                                PtrMut::new(NonNull::new_unchecked(
6✔
62
                                    Box::into_raw(iter_state) as *mut u8
6✔
63
                                ))
64
                            }),
6✔
65
                            next: |iter_ptr| unsafe {
66
                                let state = iter_ptr.as_mut::<HashSetIterator<'_, T>>();
19✔
67
                                state
19✔
68
                                    .next()
19✔
69
                                    .map(|value| PtrConst::new(NonNull::from(value)))
19✔
70
                            },
19✔
71
                            next_back: None,
72
                            size_hint: None,
73
                            dealloc: |iter_ptr| unsafe {
74
                                drop(Box::from_raw(iter_ptr.as_ptr::<HashSetIterator<'_, T>>()
6✔
75
                                    as *mut HashSetIterator<'_, T>));
6✔
76
                            },
6✔
77
                        },
78
                    )
79
                },
80
                T::SHAPE,
81
            )),
82
            type_identifier: "HashSet",
83
            type_params: &[
84
                TypeParam {
85
                    name: "T",
86
                    shape: T::SHAPE,
87
                },
88
                TypeParam {
89
                    name: "S",
90
                    shape: S::SHAPE,
91
                },
92
            ],
93
            doc: &[],
94
            attributes: &[],
95
            type_tag: None,
96
            inner: None,
97
        }
98
    };
99
}
100

101
#[cfg(test)]
102
mod tests {
103
    use alloc::string::String;
104
    use core::ptr::NonNull;
105
    use std::collections::HashSet;
106
    use std::hash::RandomState;
107

108
    use super::*;
109

110
    #[test]
111
    fn test_hashset_type_params() {
1✔
112
        // HashSet should have a type param for both its value type
113
        // and its hasher state
114
        let [type_param_1, type_param_2] = <HashSet<i32>>::SHAPE.type_params else {
1✔
115
            panic!("HashSet<T> should have 2 type params")
×
116
        };
117
        assert_eq!(type_param_1.shape(), i32::SHAPE);
1✔
118
        assert_eq!(type_param_2.shape(), RandomState::SHAPE);
1✔
119
    }
1✔
120

121
    #[test]
122
    fn test_hashset_vtable_1_new_insert_iter_drop() {
1✔
123
        facet_testhelpers::setup();
1✔
124

125
        let hashset_shape = <HashSet<String>>::SHAPE;
1✔
126
        let hashset_def = hashset_shape
1✔
127
            .def
1✔
128
            .into_set()
1✔
129
            .expect("HashSet<T> should have a set definition");
1✔
130

131
        // Allocate memory for the HashSet
132
        let hashset_uninit_ptr = hashset_shape.allocate().unwrap();
1✔
133

134
        // Create the HashSet with a capacity of 3
135
        let hashset_ptr =
1✔
136
            unsafe { (hashset_def.vtable.init_in_place_with_capacity_fn)(hashset_uninit_ptr, 3) };
1✔
137

138
        // The HashSet is empty, so ensure its length is 0
139
        let hashset_actual_length = unsafe { (hashset_def.vtable.len_fn)(hashset_ptr.as_const()) };
1✔
140
        assert_eq!(hashset_actual_length, 0);
1✔
141

142
        // 5 sample values to insert
143
        let strings = ["foo", "bar", "bazz", "fizzbuzz", "fifth thing"];
1✔
144

145
        // Insert the 5 values into the HashSet
146
        let mut hashset_length = 0;
1✔
147
        for string in strings {
5✔
148
            // Create the value
149
            let mut new_value = core::mem::ManuallyDrop::new(string.to_string());
5✔
150

151
            // Insert the value
152
            let did_insert = unsafe {
5✔
153
                (hashset_def.vtable.insert_fn)(
5✔
154
                    hashset_ptr,
5✔
155
                    PtrMut::new(NonNull::from(&mut new_value)),
5✔
156
                )
5✔
157
            };
158

159
            assert!(did_insert, "expected value to be inserted in the HashSet");
5✔
160

161
            // Ensure the HashSet's length increased by 1
162
            hashset_length += 1;
5✔
163
            let hashset_actual_length =
5✔
164
                unsafe { (hashset_def.vtable.len_fn)(hashset_ptr.as_const()) };
5✔
165
            assert_eq!(hashset_actual_length, hashset_length);
5✔
166
        }
167

168
        // Insert the same 5 values again, ensuring they are deduplicated
169
        for string in strings {
5✔
170
            // Create the value
171
            let mut new_value = core::mem::ManuallyDrop::new(string.to_string());
5✔
172

173
            // Try to insert the value
174
            let did_insert = unsafe {
5✔
175
                (hashset_def.vtable.insert_fn)(
5✔
176
                    hashset_ptr,
5✔
177
                    PtrMut::new(NonNull::from(&mut new_value)),
5✔
178
                )
5✔
179
            };
180

181
            assert!(
5✔
182
                !did_insert,
5✔
183
                "expected value to not be inserted in the HashSet"
184
            );
185

186
            // Ensure the HashSet's length did not increase
187
            let hashset_actual_length =
5✔
188
                unsafe { (hashset_def.vtable.len_fn)(hashset_ptr.as_const()) };
5✔
189
            assert_eq!(hashset_actual_length, hashset_length);
5✔
190
        }
191

192
        // Create a new iterator over the HashSet
193
        let iter_init_with_value_fn = hashset_def.vtable.iter_vtable.init_with_value.unwrap();
1✔
194
        let hashset_iter_ptr = unsafe { iter_init_with_value_fn(hashset_ptr.as_const()) };
1✔
195

196
        // Collect all the items from the HashSet's iterator
197
        let mut iter_items = HashSet::<&str>::new();
1✔
198
        loop {
199
            // Get the next item from the iterator
200
            let item_ptr = unsafe { (hashset_def.vtable.iter_vtable.next)(hashset_iter_ptr) };
6✔
201
            let Some(item_ptr) = item_ptr else {
6✔
202
                break;
1✔
203
            };
204

205
            let item = unsafe { item_ptr.get::<String>() };
5✔
206

207
            // Insert the item into the set of items returned from the iterator
208
            let did_insert = iter_items.insert(&**item);
5✔
209

210
            assert!(did_insert, "HashSet iterator returned duplicate item");
5✔
211
        }
212

213
        // Deallocate the iterator
214
        unsafe {
1✔
215
            (hashset_def.vtable.iter_vtable.dealloc)(hashset_iter_ptr);
1✔
216
        }
1✔
217

218
        // Ensure the iterator returned all of the strings
219
        assert_eq!(iter_items, strings.iter().copied().collect::<HashSet<_>>());
1✔
220

221
        // Get the function pointer for dropping the HashSet
222
        let drop_fn = hashset_shape
1✔
223
            .vtable
1✔
224
            .drop_in_place
1✔
225
            .expect("HashSet<T> should have drop_in_place");
1✔
226

227
        // Drop the HashSet in place
228
        unsafe { drop_fn(hashset_ptr) };
1✔
229

230
        // Deallocate the memory
231
        unsafe { hashset_shape.deallocate_mut(hashset_ptr).unwrap() };
1✔
232
    }
1✔
233
}
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