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

facet-rs / facet / 20024290994

08 Dec 2025 10:08AM UTC coverage: 58.613% (+0.01%) from 58.6%
20024290994

push

github

fasterthanlime
feat: compute variance automatically from field types

This implements automatic variance computation for derived types:

- Change `Shape.variance` from `fn() -> Variance` to `fn(&'static Shape) -> Variance`
  to allow variance functions to walk type structure without capturing generics

- Add `Shape::computed_variance()` that walks struct fields, enum variants,
  and inner shapes (for Box, Vec, Option, etc.) to compute variance

- Use thread-local cycle detection to prevent stack overflow with recursive types.
  When a cycle is detected, returns `Covariant` (identity for `combine`)

- Update derive macros to use `.variance(Shape::computed_variance)`
  instead of generating per-type variance computation code

- Update `Box<T>` to use computed variance (depends on T's variance)
  instead of hardcoded `Invariant`

- Add variance tests for recursive types

Closes #1171

44 of 60 new or added lines in 3 files covered. (73.33%)

487 existing lines in 23 files now uncovered.

24730 of 42192 relevant lines covered (58.61%)

544.95 hits per line

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

93.48
/facet-core/src/impls_std/hashmap.rs
1
use crate::Variance;
2
use core::hash::BuildHasher;
3
use core::ptr::NonNull;
4
use std::collections::HashMap;
5
use std::hash::RandomState;
6

7
use crate::ptr::{PtrConst, PtrMut};
8

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

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

16
// TODO: Debug, PartialEq, Eq for HashMap, HashSet
17
unsafe impl<'a, K, V, S> Facet<'a> for HashMap<K, V, S>
18
where
19
    K: Facet<'a> + core::cmp::Eq + core::hash::Hash,
20
    V: Facet<'a>,
21
    S: 'a + Default + BuildHasher,
22
{
23
    const SHAPE: &'static Shape = &const {
24
        Shape {
25
            id: Shape::id_of::<Self>(),
26
            layout: Shape::layout_of::<Self>(),
27
            vtable: ValueVTable {
28
                type_name: |f, opts| {
56✔
29
                    write!(f, "{}<", Self::SHAPE.type_identifier)?;
56✔
30
                    if let Some(opts) = opts.for_children() {
56✔
31
                        K::SHAPE.vtable.type_name()(f, opts)?;
56✔
32
                        write!(f, ", ")?;
56✔
33
                        V::SHAPE.vtable.type_name()(f, opts)?;
56✔
34
                    } else {
UNCOV
35
                        write!(f, "…")?;
×
36
                    }
37
                    write!(f, ">")
56✔
38
                },
56✔
39
                drop_in_place: ValueVTable::drop_in_place_for::<Self>(),
40
                default_in_place: Some(|target| unsafe { target.put(Self::default()) }),
×
UNCOV
41
                ..ValueVTable::new(|_, _| Ok(()))
×
42
            },
43
            ty: Type::User(UserType::Opaque),
44
            def: Def::Map(MapDef {
45
                vtable: &const {
46
                    MapVTable {
47
                        init_in_place_with_capacity_fn: |uninit, capacity| unsafe {
48
                            uninit.put(Self::with_capacity_and_hasher(capacity, S::default()))
68✔
49
                        },
68✔
50
                        insert_fn: |ptr, key, value| unsafe {
51
                            let map = ptr.as_mut::<HashMap<K, V>>();
89✔
52
                            let key = key.read::<K>();
89✔
53
                            let value = value.read::<V>();
89✔
54
                            map.insert(key, value);
89✔
55
                        },
89✔
56
                        len_fn: |ptr| unsafe {
57
                            let map = ptr.get::<HashMap<K, V>>();
5✔
58
                            map.len()
5✔
59
                        },
5✔
60
                        contains_key_fn: |ptr, key| unsafe {
61
                            let map = ptr.get::<HashMap<K, V>>();
12✔
62
                            map.contains_key(key.get())
12✔
63
                        },
12✔
64
                        get_value_ptr_fn: |ptr, key| unsafe {
65
                            let map = ptr.get::<HashMap<K, V>>();
12✔
66
                            map.get(key.get()).map(|v| PtrConst::new(NonNull::from(v)))
12✔
67
                        },
12✔
68
                        iter_vtable: IterVTable {
69
                            init_with_value: Some(|ptr| unsafe {
70
                                let map = ptr.get::<HashMap<K, V>>();
30✔
71
                                let iter: HashMapIterator<'_, K, V> = map.iter();
30✔
72
                                let iter_state = Box::new(iter);
30✔
73
                                PtrMut::new(NonNull::new_unchecked(
30✔
74
                                    Box::into_raw(iter_state) as *mut u8
30✔
75
                                ))
76
                            }),
30✔
77
                            next: |iter_ptr| unsafe {
78
                                let state = iter_ptr.as_mut::<HashMapIterator<'_, K, V>>();
75✔
79
                                state.next().map(|(key, value)| {
75✔
80
                                    (
46✔
81
                                        PtrConst::new(NonNull::from(key)),
46✔
82
                                        PtrConst::new(NonNull::from(value)),
46✔
83
                                    )
46✔
84
                                })
46✔
85
                            },
75✔
86
                            next_back: None,
87
                            size_hint: None,
88
                            dealloc: |iter_ptr| unsafe {
89
                                drop(Box::from_raw(
30✔
90
                                    iter_ptr.as_ptr::<HashMapIterator<'_, K, V>>()
30✔
91
                                        as *mut HashMapIterator<'_, K, V>,
30✔
92
                                ));
93
                            },
30✔
94
                        },
95
                    }
96
                },
97
                k: K::SHAPE,
98
                v: V::SHAPE,
99
            }),
100
            type_identifier: "HashMap",
101
            type_params: &[
102
                TypeParam {
103
                    name: "K",
104
                    shape: K::SHAPE,
105
                },
106
                TypeParam {
107
                    name: "V",
108
                    shape: V::SHAPE,
109
                },
110
            ],
111
            doc: &[],
112
            attributes: &[],
113
            type_tag: None,
114
            inner: None,
115
            proxy: None,
116
            // HashMap<K, V> is covariant in both K and V, but we use INVARIANT
117
            // as a safe conservative default since computed_variance doesn't
118
            // yet support multiple type parameters
119
            variance: Variance::INVARIANT,
120
        }
121
    };
122
}
123

124
unsafe impl Facet<'_> for RandomState {
125
    const SHAPE: &'static Shape = &const {
126
        Shape {
127
            id: Shape::id_of::<Self>(),
128
            layout: Shape::layout_of::<Self>(),
129
            vtable: value_vtable!((), |f, _opts| write!(f, "{}", Self::SHAPE.type_identifier)),
2✔
130
            ty: Type::User(UserType::Opaque),
131
            def: Def::Scalar,
132
            type_identifier: "RandomState",
133
            type_params: &[],
134
            doc: &[],
135
            attributes: &[],
136
            type_tag: None,
137
            inner: None,
138
            proxy: None,
139
            // RandomState has no lifetime parameters, so it's covariant
140
            variance: Variance::COVARIANT,
141
        }
142
    };
143
}
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