• 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

76.56
/facet-core/src/impls_alloc/vec.rs
1
use crate::*;
2
use core::hash::Hash as _;
3

4
use alloc::vec::Vec;
5

6
unsafe impl<'a, T> Facet<'a> for Vec<T>
7
where
8
    T: Facet<'a>,
9
{
10
    const SHAPE: &'static Shape = &const {
11
        Shape::builder_for_sized::<Self>()
12
            .type_params(&[
13
                TypeParam {
14
                    name: "T",
15
                    shape: || T::SHAPE,
1✔
16
                }
17
            ])
18
            .vtable(
19
                &const {
20
                    let mut builder = ValueVTable::builder::<Self>()
21
                        .type_name(|f, opts| {
28✔
22
                            if let Some(opts) = opts.for_children() {
28✔
23
                                write!(f, "Vec<")?;
28✔
24
                                (T::SHAPE.vtable.type_name)(f, opts)?;
28✔
25
                                write!(f, ">")
28✔
26
                            } else {
27
                                write!(f, "Vec<⋯>")
×
28
                            }
29
                        })
28✔
30
                        .default_in_place(|target| unsafe { target.put(Self::default()) })
58✔
31
                    // FIXME: THIS IS VERY WRONG
NEW
32
                        .clone_into(|src, dst| unsafe { dst.put(core::ptr::read(src)) });
×
33

34
                    if T::SHAPE.vtable.debug.is_some() {
35
                        builder = builder.debug(|value, f| {
20✔
36
                            write!(f, "[")?;
20✔
37
                            for (i, item) in value.iter().enumerate() {
40✔
38
                                if i > 0 {
40✔
39
                                    write!(f, ", ")?;
24✔
40
                                }
16✔
41
                                (<VTableView<T>>::of().debug().unwrap())(
40✔
42
                                    item,
40✔
43
                                    f,
40✔
44
                                )?;
40✔
45
                            }
46
                            write!(f, "]")
20✔
47
                        });
20✔
48
                    }
49

50
                    if T::SHAPE.vtable.eq.is_some() {
51
                        builder = builder.eq(|a, b|  {
4✔
52
                            if a.len() != b.len() {
4✔
53
                                return false;
×
54
                            }
4✔
55
                            for (item_a, item_b) in a.iter().zip(b.iter()) {
7✔
56
                                if !(<VTableView<T>>::of().eq().unwrap())(
7✔
57
                                    item_a,
7✔
58
                                    item_b,
7✔
59
                                ) {
7✔
60
                                    return false;
2✔
61
                                }
5✔
62
                            }
63
                            true
2✔
64
                        });
4✔
65
                    }
66

67
                    if T::SHAPE.vtable.hash.is_some() {
68
                        builder = builder.hash(|vec, hasher_this, hasher_write_fn| unsafe {
69
                            use crate::HasherProxy;
NEW
70
                            let t_hash = <VTableView<T>>::of().hash().unwrap_unchecked();
×
71
                            let mut hasher = HasherProxy::new(hasher_this, hasher_write_fn);
×
72
                            vec.len().hash(&mut hasher);
×
73
                            for item in vec {
×
NEW
74
                                (t_hash)(item, hasher_this, hasher_write_fn);
×
75
                            }
×
76
                        });
×
77
                    }
78

79
                    let traits = MarkerTraits::SEND
80
                        .union(MarkerTraits::SYNC)
81
                        .union(MarkerTraits::EQ)
82
                        .union(MarkerTraits::UNPIN)
83
                        .intersection(T::SHAPE.vtable.marker_traits);
84
                    builder = builder.marker_traits(traits);
85

86
                    builder.build()
87
                },
88
            )
89
            .def(Def::List(
90
                ListDef::builder()
91
                    .vtable(
92
                        &const {
93
                            ListVTable::builder()
94
                                .init_in_place_with_capacity(|data, capacity| unsafe {
×
95
                                    data.put(Self::with_capacity(capacity))
×
96
                                })
×
97
                                .push(|ptr, item| unsafe {
74✔
98
                                    let vec = ptr.as_mut::<Self>();
74✔
99
                                    let item = item.read::<T>();
74✔
100
                                    (*vec).push(item);
74✔
101
                                })
74✔
102
                                .len(|ptr| unsafe {
13✔
103
                                    let vec = ptr.get::<Self>();
13✔
104
                                    vec.len()
13✔
105
                                })
13✔
106
                                .get_item_ptr(|ptr, index| unsafe {
8✔
107
                                    let vec = ptr.get::<Self>();
8✔
108
                                    let len = vec.len();
8✔
109
                                    if index >= len {
8✔
110
                                        panic!(
×
111
                                            "Index out of bounds: the len is {len} but the index is {index}"
×
112
                                        );
113
                                    }
8✔
114
                                    PtrConst::new(vec.as_ptr().add(index))
8✔
115
                                })
8✔
116
                                .build()
117
                        },
118
                    )
119
                    .t(|| T::SHAPE)
95✔
120
                    .build(),
121
            ))
122
            .build()
123
    };
124
}
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