• 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

53.4
/facet-core/src/impls_bytes.rs
1
use core::ptr::NonNull;
2

3
use alloc::boxed::Box;
4

5
use bytes::{BufMut as _, Bytes, BytesMut};
6

7
use crate::{
8
    Def, Facet, IterVTable, ListDef, ListVTable, PtrConst, PtrMut, PtrUninit, Shape, Type,
9
    UserType, Variance, value_vtable,
10
};
11

12
type BytesIterator<'mem> = core::slice::Iter<'mem, u8>;
13

14
unsafe impl Facet<'_> for Bytes {
15
    const SHAPE: &'static Shape = &const {
16
        Shape {
17
            id: Shape::id_of::<Self>(),
18
            layout: Shape::layout_of::<Self>(),
19
            vtable: {
20
                let mut vtable = value_vtable!(Bytes, |f, _opts| write!(
2✔
21
                    f,
2✔
22
                    "{}",
23
                    Self::SHAPE.type_identifier
2✔
24
                ));
25
                {
26
                    vtable.try_from = {
27
                        Some(
28
                            |source: PtrConst, source_shape: &Shape, target: PtrUninit| {
1✔
29
                                if source_shape.is_type::<BytesMut>() {
1✔
30
                                    let source = unsafe { source.read::<BytesMut>() };
1✔
31
                                    let bytes = source.freeze();
1✔
32
                                    Ok(unsafe { target.put(bytes) })
1✔
33
                                } else {
34
                                    Err(crate::TryFromError::UnsupportedSourceShape {
×
35
                                        src_shape: source_shape,
×
36
                                        expected: &[Bytes::SHAPE],
×
37
                                    })
×
38
                                }
39
                            },
1✔
40
                        )
41
                    };
42
                }
43

44
                vtable
45
            },
46
            ty: Type::User(UserType::Opaque),
47
            def: Def::List(ListDef::new(
48
                &const {
49
                    ListVTable {
50
                        init_in_place_with_capacity: None,
51
                        push: None,
52
                        len: |ptr| unsafe {
53
                            let bytes = ptr.get::<Self>();
1✔
54
                            bytes.len()
1✔
55
                        },
1✔
56
                        get: |ptr, index| unsafe {
57
                            let bytes = ptr.get::<Self>();
×
58
                            let item = bytes.get(index)?;
×
59
                            Some(PtrConst::new(item.into()))
×
60
                        },
×
61
                        get_mut: None,
62
                        as_ptr: Some(|ptr| unsafe {
63
                            let bytes: &Self = ptr.get::<Self>();
2✔
64
                            PtrConst::new(core::ptr::NonNull::new_unchecked(
2✔
65
                                bytes.as_ptr() as *mut u8
2✔
66
                            ))
67
                        }),
2✔
68
                        as_mut_ptr: None,
69
                        iter_vtable: IterVTable {
70
                            init_with_value: Some(|ptr| unsafe {
71
                                let bytes = ptr.get::<Self>();
×
72
                                let iter: BytesIterator = bytes.iter();
×
73
                                let iter_state = Box::new(iter);
×
74
                                PtrMut::new(NonNull::new_unchecked(
×
75
                                    Box::into_raw(iter_state) as *mut u8
×
76
                                ))
77
                            }),
×
78
                            next: |iter_ptr| unsafe {
79
                                let state = iter_ptr.as_mut::<BytesIterator<'_>>();
×
80
                                state.next().map(|value| PtrConst::new(value.into()))
×
81
                            },
×
82
                            next_back: Some(|iter_ptr| unsafe {
83
                                let state = iter_ptr.as_mut::<BytesIterator<'_>>();
×
84
                                state.next_back().map(|value| PtrConst::new(value.into()))
×
85
                            }),
×
86
                            size_hint: None,
87
                            dealloc: |iter_ptr| unsafe {
88
                                drop(Box::from_raw(iter_ptr.as_ptr::<BytesIterator<'_>>()
×
89
                                    as *mut BytesIterator<'_>));
×
90
                            },
×
91
                        },
92
                    }
93
                },
94
                u8::SHAPE,
95
            )),
96
            type_identifier: "Bytes",
97
            type_params: &[],
98
            doc: &[],
99
            attributes: &[],
100
            type_tag: None,
101
            inner: Some(BytesMut::SHAPE),
102
            proxy: None,
103
            // Bytes has no lifetime parameters, so it's covariant
104
            variance: Variance::COVARIANT,
105
        }
106
    };
107
}
108

109
unsafe impl Facet<'_> for BytesMut {
110
    const SHAPE: &'static Shape = &const {
111
        Shape {
112
            id: Shape::id_of::<Self>(),
113
            layout: Shape::layout_of::<Self>(),
114
            vtable: value_vtable!(BytesMut, |f, _opts| write!(
2✔
115
                f,
2✔
116
                "{}",
117
                Self::SHAPE.type_identifier
2✔
118
            )),
119
            ty: Type::User(UserType::Opaque),
120
            def: Def::List(ListDef::new(
121
                &const {
122
                    ListVTable {
123
                        init_in_place_with_capacity: Some(|data, capacity| unsafe {
124
                            data.put(Self::with_capacity(capacity))
2✔
125
                        }),
2✔
126
                        push: Some(|ptr, item| unsafe {
127
                            let bytes = ptr.as_mut::<Self>();
10✔
128
                            let item = item.read::<u8>();
10✔
129
                            (*bytes).put_u8(item);
10✔
130
                        }),
10✔
131
                        len: |ptr| unsafe {
132
                            let bytes = ptr.get::<Self>();
1✔
133
                            bytes.len()
1✔
134
                        },
1✔
135
                        get: |ptr, index| unsafe {
136
                            let bytes = ptr.get::<Self>();
×
137
                            let item = bytes.get(index)?;
×
138
                            Some(PtrConst::new(item.into()))
×
UNCOV
139
                        },
×
140
                        get_mut: Some(|ptr, index| unsafe {
141
                            let bytes = ptr.as_mut::<Self>();
×
142
                            let item = bytes.get_mut(index)?;
×
143
                            Some(PtrMut::new(item.into()))
×
UNCOV
144
                        }),
×
145
                        as_ptr: Some(|ptr| unsafe {
146
                            let bytes = ptr.get::<Self>();
2✔
147
                            PtrConst::new(core::ptr::NonNull::new_unchecked(
2✔
148
                                bytes.as_ptr() as *mut u8
2✔
149
                            ))
150
                        }),
2✔
151
                        as_mut_ptr: Some(|ptr| unsafe {
152
                            let bytes = ptr.as_mut::<Self>();
1✔
153
                            PtrMut::new(core::ptr::NonNull::new_unchecked(bytes.as_mut_ptr()))
1✔
154
                        }),
1✔
155
                        iter_vtable: IterVTable {
156
                            init_with_value: Some(|ptr| unsafe {
157
                                let bytes = ptr.get::<Self>();
×
158
                                let iter: BytesIterator = bytes.iter();
×
159
                                let iter_state = Box::new(iter);
×
160
                                PtrMut::new(NonNull::new_unchecked(
×
UNCOV
161
                                    Box::into_raw(iter_state) as *mut u8
×
162
                                ))
UNCOV
163
                            }),
×
164
                            next: |iter_ptr| unsafe {
165
                                let state = iter_ptr.as_mut::<BytesIterator<'_>>();
×
166
                                state.next().map(|value| PtrConst::new(value.into()))
×
UNCOV
167
                            },
×
168
                            next_back: Some(|iter_ptr| unsafe {
169
                                let state = iter_ptr.as_mut::<BytesIterator<'_>>();
×
170
                                state.next_back().map(|value| PtrConst::new(value.into()))
×
UNCOV
171
                            }),
×
172
                            size_hint: None,
173
                            dealloc: |iter_ptr| unsafe {
174
                                drop(Box::from_raw(iter_ptr.as_ptr::<BytesIterator<'_>>()
×
175
                                    as *mut BytesIterator<'_>));
×
UNCOV
176
                            },
×
177
                        },
178
                    }
179
                },
180
                u8::SHAPE,
181
            )),
182
            type_identifier: "BytesMut",
183
            type_params: &[],
184
            doc: &[],
185
            attributes: &[],
186
            type_tag: None,
187
            inner: None,
188
            proxy: None,
189
            // BytesMut has no lifetime parameters, so it's covariant
190
            variance: Variance::COVARIANT,
191
        }
192
    };
193
}
194

195
#[cfg(test)]
196
mod tests {
197
    use super::*;
198

199
    #[test]
200
    fn test_as_ptr() {
1✔
201
        let bytes = Bytes::from(vec![0, 1, 2, 3, 4]);
1✔
202
        let expected = bytes.as_ptr();
1✔
203
        let Def::List(def) = Bytes::SHAPE.def else {
1✔
UNCOV
204
            panic!()
×
205
        };
206
        let actual =
1✔
207
            unsafe { (def.vtable.as_ptr).unwrap()(PtrConst::new((&bytes).into())) }.as_byte_ptr();
1✔
208
        assert_eq!(expected, actual);
1✔
209
    }
1✔
210

211
    #[test]
212
    fn test_as_ptr_mut() {
1✔
213
        let bytes = Bytes::from(vec![0, 1, 2, 3, 4]);
1✔
214
        let mut bytes = BytesMut::from(bytes);
1✔
215
        let expected = bytes.as_ptr();
1✔
216
        let Def::List(def) = BytesMut::SHAPE.def else {
1✔
UNCOV
217
            panic!()
×
218
        };
219
        let actual =
1✔
220
            unsafe { (def.vtable.as_ptr).unwrap()(PtrConst::new((&bytes).into())) }.as_byte_ptr();
1✔
221
        assert_eq!(expected, actual);
1✔
222

223
        let actual = unsafe { (def.vtable.as_mut_ptr).unwrap()(PtrMut::new((&mut bytes).into())) }
1✔
224
            .as_byte_ptr();
1✔
225
        assert_eq!(expected, actual);
1✔
226
    }
1✔
227
}
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