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

facet-rs / facet / 14680246574

26 Apr 2025 10:13AM UTC coverage: 56.736% (+0.03%) from 56.703%
14680246574

push

github

fasterthanlime
Expose a typed VTable API, reduce unsafe code

124 of 288 new or added lines in 12 files covered. (43.06%)

3 existing lines in 3 files now uncovered.

6776 of 11943 relevant lines covered (56.74%)

74.65 hits per line

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

62.5
/facet-core/src/impls_core/array.rs
1
use crate::*;
2
use core::{cmp::Ordering, iter::zip};
3

4
unsafe impl<'a, T, const L: usize> Facet<'a> for [T; L]
5
where
6
    T: Facet<'a>,
7
{
8
    const SHAPE: &'static Shape = &const {
9
        Shape::builder_for_sized::<Self>()
10
            .type_params(&[
11
                TypeParam {
12
                    name: "T",
13
                    shape: || T::SHAPE,
1✔
14
                }
15
            ])
16
            .vtable(
17
                &const {
18
                    let mut builder = ValueVTable::builder::<Self>()
19
                        .marker_traits(T::SHAPE.vtable.marker_traits)
20
                        .type_name(|f, opts| {
10✔
21
                            if let Some(opts) = opts.for_children() {
10✔
22
                                write!(f, "[")?;
10✔
23
                                (T::SHAPE.vtable.type_name)(f, opts)?;
10✔
24
                                write!(f, "; {L}]")
10✔
25
                            } else {
26
                                write!(f, "[⋯; {L}]")
×
27
                            }
28
                        });
10✔
29
                    if T::SHAPE.vtable.display.is_some() {
30
                        builder = builder.display(|value, f| {
10✔
31
                            write!(f, "[")?;
10✔
32

33
                            for (idx, value) in value.iter().enumerate() {
74✔
34
                                (<VTableView<T>>::of().display().unwrap())(
74✔
35
                                    value,
74✔
36
                                    f,
74✔
37
                                )?;
74✔
38
                                if idx != L - 1 {
74✔
39
                                    write!(f, ", ")?;
66✔
40
                                }
8✔
41
                            }
42
                            write!(f, "]")
10✔
43
                        });
10✔
44
                    }
45
                    if T::SHAPE.vtable.debug.is_some() {
46
                        builder = builder.debug(|value, f| {
34✔
47
                            write!(f, "[")?;
34✔
48

49
                            for (idx, value) in value.iter().enumerate() {
226✔
50
                                (<VTableView<T>>::of().debug().unwrap())(
226✔
51
                                    value,
226✔
52
                                    f,
226✔
53
                                )?;
226✔
54
                                if idx != L - 1 {
226✔
55
                                    write!(f, ", ")?;
199✔
56
                                }
27✔
57
                            }
58
                            write!(f, "]")
34✔
59
                        });
34✔
60
                    }
61
                    if T::SHAPE.vtable.eq.is_some() {
62
                        builder = builder.eq(|a, b| {
5✔
63
                            zip(a, b).all(|(a, b)| {
37✔
64
                                (<VTableView<T>>::of().eq().unwrap())(
37✔
65
                                    a,
37✔
66
                                    b,
37✔
67
                                )
37✔
68
                            })
37✔
69
                        });
5✔
70
                    }
71
                    if L == 0 {
72
                        // Zero-length arrays implement `Default` irrespective of the element type
73
                        builder =
74
                            builder.default_in_place(|target| unsafe { target.assume_init() });
1✔
75
                    } else if L <= 32 && T::SHAPE.vtable.default_in_place.is_some() {
76
                        builder = builder.default_in_place(|mut target| unsafe {
3✔
77
                            let t_dip = <VTableView<T>>::of().default_in_place().unwrap();
3✔
78
                            let stride = T::SHAPE.layout.sized_layout().unwrap().pad_to_align().size();
3✔
79
                            for idx in 0..L {
7✔
80
                                t_dip(target.field_uninit_at(idx * stride));
4✔
81
                            }
4✔
82
                            target.assume_init()
3✔
83
                        });
3✔
84
                    } else {
85
                        // arrays do not yet implement `Default` for > 32 elements due to
86
                        // specializing the `0` len case
87
                    }
88
                    if T::SHAPE.vtable.clone_into.is_some() {
NEW
89
                        builder = builder.clone_into(|src, mut dst| unsafe {
×
NEW
90
                            let t_cip = <VTableView<T>>::of().clone_into().unwrap();
×
91
                            let stride = T::SHAPE.layout.sized_layout().unwrap().pad_to_align().size();
×
92
                            for (idx, src) in src.iter().enumerate() {
×
NEW
93
                                (t_cip)(src, dst.field_uninit_at(idx * stride));
×
94
                            }
×
95
                            dst.assume_init()
×
96
                        });
×
97
                    }
98
                    if T::SHAPE.vtable.partial_ord.is_some() {
99
                        builder = builder.partial_ord(|a, b| {
5✔
100
                            zip(a, b)
5✔
101
                                .find_map(|(a, b)|  {
37✔
102
                                    match (<VTableView<T>>::of().partial_ord().unwrap())(
37✔
103
                                        a,
37✔
104
                                        b,
37✔
105
                                    ) {
106
                                        Some(Ordering::Equal) => None,
34✔
107
                                        c => Some(c),
3✔
108
                                    }
109
                                })
37✔
110
                                .unwrap_or(Some(Ordering::Equal))
5✔
111
                        });
5✔
112
                    }
113
                    if T::SHAPE.vtable.ord.is_some() {
114
                        builder = builder.ord(|a, b| {
×
115
                            zip(a, b)
×
NEW
116
                                .find_map(|(a, b)| {
×
NEW
117
                                    match (<VTableView<T>>::of().ord().unwrap())(
×
NEW
118
                                        a,
×
NEW
119
                                        b,
×
120
                                    ) {
×
121
                                        Ordering::Equal => None,
×
122
                                        c => Some(c),
×
123
                                    }
124
                                })
×
125
                                .unwrap_or(Ordering::Equal)
×
126
                        });
×
127
                    }
128
                    if T::SHAPE.vtable.hash.is_some() {
129
                        builder = builder.hash(|value, state, hasher| {
×
130
                            for value in value {
×
NEW
131
                                (<VTableView<T>>::of().hash().unwrap())(
×
NEW
132
                                    value,
×
NEW
133
                                    state,
×
NEW
134
                                    hasher,
×
NEW
135
                                )
×
136
                            }
137
                        });
×
138
                    }
139
                    builder.build()
140
                },
141
            )
142
            .def(Def::Array(
143
                ArrayDef::builder()
144
                    .vtable(
145
                        &const {
146
                            ArrayVTable::builder()
147
                                .get_item_ptr(|ptr, index| unsafe {
×
148
                                    if index >= L {
×
149
                                        panic!(
×
150
                                            "Index out of bounds: the len is {L} but the index is {index}"
×
151
                                        );
152
                                    }
×
153
                                    PtrConst::new(ptr.as_ptr::<[T; L]>())
×
154
                                })
×
155
                                .build()
156
                        },
157
                    )
158
                    .n(L)
159
                    .t(|| T::SHAPE)
1✔
160
                    .build(),
161
            ))
162
            .build()
163
    };
164
}
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