• 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

88.37
/facet-core/src/impls_core/array.rs
1
use core::ptr::NonNull;
2

3
use crate::*;
4

5
unsafe impl<'a, T, const L: usize> Facet<'a> for [T; L]
6
where
7
    T: Facet<'a>,
8
{
9
    const SHAPE: &'static Shape = &const {
10
        Shape {
11
            id: Shape::id_of::<Self>(),
12
            layout: Shape::layout_of::<Self>(),
13
            vtable: ValueVTable {
14
                type_name: |f, opts| {
32✔
15
                    if let Some(opts) = opts.for_children() {
32✔
16
                        write!(f, "[")?;
32✔
17
                        (T::SHAPE.vtable.type_name())(f, opts)?;
32✔
18
                        write!(f, "; {L}]")
32✔
19
                    } else {
UNCOV
20
                        write!(f, "[…; {L}]")
×
21
                    }
22
                },
32✔
23
                drop_in_place: ValueVTable::drop_in_place_for::<Self>(),
24
                default_in_place: {
25
                    if L == 0 {
26
                        // Zero-length arrays implement `Default` irrespective of the element type
27
                        Some(|target| unsafe { target.assume_init() })
1✔
28
                    } else if L <= 32 && T::SHAPE.vtable.has_default_in_place() {
29
                        Some(|target| unsafe {
30
                            let t_dip = T::SHAPE.vtable.default_in_place.unwrap();
1✔
31
                            let stride = T::SHAPE
1✔
32
                                .layout
1✔
33
                                .sized_layout()
1✔
34
                                .unwrap()
1✔
35
                                .pad_to_align()
1✔
36
                                .size();
1✔
37
                            for idx in 0..L {
1✔
38
                                t_dip(target.field_uninit_at(idx * stride));
1✔
39
                            }
1✔
40
                            target.assume_init()
1✔
41
                        })
1✔
42
                    } else {
43
                        // arrays do not yet implement `Default` for > 32 elements due
44
                        // to specializing the `0` len case
45
                        None
46
                    }
47
                },
48
                clone_into: {
49
                    if T::SHAPE.vtable.has_clone_into() {
50
                        Some(|src, dst| unsafe {
51
                            let src = src.get::<Self>();
1✔
52
                            let t_cip = T::SHAPE.vtable.clone_into.unwrap();
1✔
53
                            let stride = T::SHAPE
1✔
54
                                .layout
1✔
55
                                .sized_layout()
1✔
56
                                .unwrap()
1✔
57
                                .pad_to_align()
1✔
58
                                .size();
1✔
59
                            for (idx, src) in src.iter().enumerate() {
1✔
60
                                (t_cip)(
1✔
61
                                    PtrConst::new(NonNull::from(src)),
1✔
62
                                    dst.field_uninit_at(idx * stride),
1✔
63
                                );
1✔
64
                            }
1✔
65
                            dst.assume_init()
1✔
66
                        })
1✔
67
                    } else {
68
                        None
69
                    }
70
                },
UNCOV
71
                ..ValueVTable::new(|_, _| Ok(()))
×
72
            },
73
            ty: Type::Sequence(SequenceType::Array(ArrayType { t: T::SHAPE, n: L })),
74
            def: Def::Array(ArrayDef::new(
75
                &const {
76
                    ArrayVTable::new(
77
                        |ptr| unsafe {
78
                            let array = ptr.get::<[T; L]>();
34✔
79
                            PtrConst::new(NonNull::from(array))
34✔
80
                        },
34✔
81
                        |ptr| unsafe {
82
                            let array = ptr.as_mut::<[T; L]>();
×
83
                            PtrMut::new(NonNull::from(array))
×
UNCOV
84
                        },
×
85
                    )
86
                },
87
                T::SHAPE,
88
                L,
89
            )),
90
            type_identifier: "&[_; _]",
91
            type_params: &[TypeParam {
92
                name: "T",
93
                shape: T::SHAPE,
94
            }],
95
            doc: &[],
96
            attributes: &[],
97
            type_tag: None,
98
            // Array uses Def::Array, not inner, for element access
99
            inner: None,
100
            proxy: None,
101
            // [T; N] is covariant in T, computed from the element type
102
            variance: Shape::computed_variance,
103
        }
104
    };
105
}
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