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

facet-rs / facet / 14459841398

15 Apr 2025 02:20AM UTC coverage: 30.835% (+0.7%) from 30.122%
14459841398

push

github

web-flow
Rename opaque to ptr, closes #221 (#222)

47 of 344 new or added lines in 20 files covered. (13.66%)

1 existing line in 1 file now uncovered.

2032 of 6590 relevant lines covered (30.83%)

25.82 hits per line

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

47.3
/facet-core/src/impls_core/slice.rs
1
use crate::*;
2
use core::alloc::Layout;
3

4
unsafe impl<T> Facet for &[T]
5
where
6
    T: Facet,
7
{
8
    const SHAPE: &'static Shape = &const {
9
        Shape::builder()
10
            .id(ConstTypeId::of::<&[T]>())
11
            .layout(Layout::new::<&[T]>())
12
            .def(Def::Slice(
13
                SliceDef::builder()
14
                    .vtable(
15
                        &const {
16
                            SliceVTable::builder()
17
                                .len(|ptr| unsafe {
×
18
                                    let slice = ptr.get::<&[T]>();
×
19
                                    slice.len()
×
20
                                })
21
                                .get_item_ptr(|ptr, index| unsafe {
×
22
                                    let slice = ptr.get::<&[T]>();
×
23
                                    let len = slice.len();
×
24
                                    if index >= len {
×
25
                                        panic!(
×
26
                                            "Index out of bounds: the len is {len} but the index is {index}"
×
27
                                        );
28
                                    }
NEW
29
                                    PtrConst::new(slice.as_ptr().add(index))
×
30
                                })
31
                                .build()
32
                        },
33
                    )
34
                    .t(|| T::SHAPE)
×
35
                    .build(),
36
            ))
37
            .vtable(
38
                &const {
39
                    let mut builder = ValueVTable::builder()
40
                        .type_name(|f, opts| {
4✔
41
                            if let Some(opts) = opts.for_children() {
8✔
42
                                write!(f, "&[")?;
×
43
                                (T::SHAPE.vtable.type_name)(f, opts)?;
4✔
44
                                write!(f, "]")
4✔
45
                            } else {
46
                                write!(f, "&[⋯]")
×
47
                            }
48
                        })
49
                        .marker_traits(T::SHAPE.vtable.marker_traits)
50
                        .default_in_place(|ptr| unsafe { ptr.put(&[] as &[T]) })
2✔
51
                        .clone_into(|src, dst| unsafe {
×
52
                            // This works because we're cloning a shared reference (&[T]), not the actual slice data.
53
                            // We're just copying the fat pointer (ptr + length) that makes up the slice reference.
54
                            dst.put(src.get::<&[T]>())
×
55
                        });
56

57
                    if T::SHAPE.vtable.debug.is_some() {
58
                        builder = builder.debug(|value, f| {
14✔
59
                            let value = unsafe { value.get::<&[T]>() };
14✔
60
                            write!(f, "[")?;
14✔
61
                            for (i, item) in value.iter().enumerate() {
44✔
62
                                if i > 0 {
30✔
63
                                    write!(f, ", ")?;
18✔
64
                                }
65
                                unsafe {
66
                                    (T::SHAPE.vtable.debug.unwrap_unchecked())(
30✔
67
                                        PtrConst::new(item as *const _),
30✔
68
                                        f,
30✔
69
                                    )?;
70
                                }
71
                            }
72
                            write!(f, "]")
14✔
73
                        });
74
                    }
75

76
                    if T::SHAPE.vtable.eq.is_some() {
77
                        builder = builder.eq(|a, b| {
2✔
78
                            let a = unsafe { a.get::<&[T]>() };
2✔
79
                            let b = unsafe { b.get::<&[T]>() };
2✔
80
                            if a.len() != b.len() {
2✔
81
                                return false;
×
82
                            }
83
                            for (x, y) in a.iter().zip(b.iter()) {
2✔
84
                                if !unsafe {
2✔
85
                                    (T::SHAPE.vtable.eq.unwrap_unchecked())(
2✔
86
                                        PtrConst::new(x as *const _),
2✔
87
                                        PtrConst::new(y as *const _),
2✔
88
                                    )
89
                                } {
90
                                    return false;
2✔
91
                                }
92
                            }
93
                            true
×
94
                        });
95
                    }
96

97
                    if T::SHAPE.vtable.ord.is_some() {
98
                        builder = builder.ord(|a, b| {
×
99
                            let a = unsafe { a.get::<&[T]>() };
×
100
                            let b = unsafe { b.get::<&[T]>() };
×
101
                            for (x, y) in a.iter().zip(b.iter()) {
×
102
                                let ord = unsafe {
×
103
                                    (T::SHAPE.vtable.ord.unwrap_unchecked())(
×
NEW
104
                                        PtrConst::new(x as *const _),
×
NEW
105
                                        PtrConst::new(y as *const _),
×
106
                                    )
107
                                };
108
                                if ord != core::cmp::Ordering::Equal {
×
109
                                    return ord;
×
110
                                }
111
                            }
112
                            a.len().cmp(&b.len())
×
113
                        });
114
                    }
115

116
                    if T::SHAPE.vtable.partial_ord.is_some() {
117
                        builder = builder.partial_ord(|a, b| {
2✔
118
                            let a = unsafe { a.get::<&[T]>() };
2✔
119
                            let b = unsafe { b.get::<&[T]>() };
2✔
120
                            for (x, y) in a.iter().zip(b.iter()) {
4✔
121
                                let ord = unsafe {
2✔
122
                                    (T::SHAPE.vtable.partial_ord.unwrap_unchecked())(
2✔
123
                                        PtrConst::new(x as *const _),
2✔
124
                                        PtrConst::new(y as *const _),
2✔
125
                                    )
126
                                };
127
                                match ord {
2✔
128
                                    Some(core::cmp::Ordering::Equal) => continue,
×
129
                                    Some(order) => return Some(order),
2✔
130
                                    None => return None,
×
131
                                }
132
                            }
133
                            a.len().partial_cmp(&b.len())
×
134
                        });
135
                    }
136

137
                    if T::SHAPE.vtable.hash.is_some() {
138
                        builder = builder.hash(|value, state, hasher| {
×
139
                            let value = unsafe { value.get::<&[T]>() };
×
140
                            for item in value.iter() {
×
141
                                unsafe {
×
142
                                    (T::SHAPE.vtable.hash.unwrap_unchecked())(
×
NEW
143
                                        PtrConst::new(item as *const _),
×
144
                                        state,
×
145
                                        hasher,
×
146
                                    )
147
                                };
148
                            }
149
                        });
150
                    }
151

152
                    builder.build()
153
                },
154
            )
155
            .build()
156
    };
157
}
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