• 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

46.0
/facet-core/src/impls_alloc/vec.rs
1
use core::ptr::NonNull;
2

3
use crate::shape_util::vtable_for_list;
4
use crate::*;
5

6
use alloc::boxed::Box;
7
use alloc::vec::Vec;
8

9
type VecIterator<'mem, T> = core::slice::Iter<'mem, T>;
10

11
unsafe impl<'a, T> Facet<'a> for Vec<T>
12
where
13
    T: Facet<'a>,
14
{
15
    const SHAPE: &'static Shape = &const {
16
        Shape {
17
            id: Shape::id_of::<Self>(),
18
            layout: Shape::layout_of::<Self>(),
19
            vtable: ValueVTable {
20
                type_name: |f, opts| {
206✔
21
                    if let Some(opts) = opts.for_children() {
206✔
22
                        write!(f, "{}<", Self::SHAPE.type_identifier)?;
206✔
23
                        T::SHAPE.vtable.type_name()(f, opts)?;
206✔
24
                        write!(f, ">")
206✔
25
                    } else {
UNCOV
26
                        write!(f, "{}<…>", Self::SHAPE.type_identifier)
×
27
                    }
28
                },
206✔
29
                default_in_place: Some(|target| unsafe { target.put(Self::default()) }),
2✔
30
                ..vtable_for_list::<T, Self>()
31
            },
32
            ty: Type::User(UserType::Opaque),
33
            def: Def::List(ListDef::new(
34
                &const {
35
                    ListVTable {
36
                        init_in_place_with_capacity: Some(|data, capacity| unsafe {
37
                            data.put(Self::with_capacity(capacity))
441✔
38
                        }),
441✔
39
                        push: Some(|ptr, item| unsafe {
40
                            let vec = ptr.as_mut::<Self>();
12,827✔
41
                            let item = item.read::<T>();
12,827✔
42
                            (*vec).push(item);
12,827✔
43
                        }),
12,827✔
44
                        len: |ptr| unsafe {
45
                            let vec = ptr.get::<Self>();
1,216✔
46
                            vec.len()
1,216✔
47
                        },
1,216✔
48
                        get: |ptr, index| unsafe {
49
                            let vec = ptr.get::<Self>();
26✔
50
                            let item = vec.get(index)?;
26✔
51
                            Some(PtrConst::new(NonNull::from(item)))
24✔
52
                        },
26✔
53
                        get_mut: Some(|ptr, index| unsafe {
54
                            let vec = ptr.as_mut::<Self>();
×
55
                            let item = vec.get_mut(index)?;
×
56
                            Some(PtrMut::new(NonNull::from(item)))
×
UNCOV
57
                        }),
×
58
                        as_ptr: Some(|ptr| unsafe {
59
                            let vec = ptr.get::<Self>();
746✔
60
                            PtrConst::new(NonNull::new_unchecked(vec.as_ptr() as *mut T))
746✔
61
                        }),
746✔
62
                        as_mut_ptr: Some(|ptr| unsafe {
63
                            let vec = ptr.as_mut::<Self>();
×
64
                            PtrMut::new(NonNull::new_unchecked(vec.as_mut_ptr()))
×
UNCOV
65
                        }),
×
66
                        iter_vtable: {
67
                            IterVTable {
68
                                init_with_value: Some(|ptr| unsafe {
69
                                    let vec = ptr.get::<Self>();
×
70
                                    let iter: VecIterator<T> = vec.iter();
×
71
                                    let iter_state = Box::new(iter);
×
72
                                    PtrMut::new(NonNull::new_unchecked(
×
UNCOV
73
                                        Box::into_raw(iter_state) as *mut u8
×
74
                                    ))
UNCOV
75
                                }),
×
76
                                next: |iter_ptr| unsafe {
77
                                    let state = iter_ptr.as_mut::<VecIterator<'_, T>>();
×
78
                                    state
×
79
                                        .next()
×
80
                                        .map(|value| PtrConst::new(NonNull::from(value)))
×
UNCOV
81
                                },
×
82
                                next_back: Some(|iter_ptr| unsafe {
83
                                    let state = iter_ptr.as_mut::<VecIterator<'_, T>>();
×
84
                                    state
×
85
                                        .next_back()
×
86
                                        .map(|value| PtrConst::new(NonNull::from(value)))
×
UNCOV
87
                                }),
×
88
                                size_hint: None,
89
                                dealloc: |iter_ptr| unsafe {
90
                                    drop(Box::from_raw(iter_ptr.as_ptr::<VecIterator<'_, T>>()
×
91
                                        as *mut VecIterator<'_, T>));
×
UNCOV
92
                                },
×
93
                            }
94
                        },
95
                    }
96
                },
97
                T::SHAPE,
98
            )),
99
            type_identifier: "Vec",
100
            type_params: &[TypeParam {
101
                name: "T",
102
                shape: T::SHAPE,
103
            }],
104
            doc: &[],
105
            attributes: &[],
106
            type_tag: None,
107
            // Vec uses Def::List, not inner, for element access
108
            inner: None,
109
            proxy: None,
110
            // Vec<T> is covariant in T, computed from the element type
111
            variance: Shape::computed_variance,
112
        }
113
    };
114
}
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