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

facet-rs / facet / 14934658416

09 May 2025 05:46PM UTC coverage: 55.35% (+0.2%) from 55.19%
14934658416

Pull #532

github

web-flow
Merge c45ad39e7 into aeef46ab4
Pull Request #532: wip: references are not pointers

39 of 116 new or added lines in 2 files covered. (33.62%)

4 existing lines in 2 files now uncovered.

6782 of 12253 relevant lines covered (55.35%)

73.65 hits per line

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

27.84
/facet-core/src/impls_core/pointer.rs
1
use core::{fmt, hash::Hash};
2

3
use crate::{
4
    Facet, HasherProxy, MarkerTraits, PointerType, Shape, Type, TypeParam, ValuePointerType,
5
    ValueVTable,
6
};
7

8
macro_rules! impl_facet_for_pointer {
9
    ($variant:ident: $type:ty => $shape:expr => $vtable_builder:expr => $ptr_type:ident, $mutable:expr) => {
10
        unsafe impl<'a, T: Facet<'a> + ?Sized> Facet<'a> for $type {
11
            const VTABLE: &'static ValueVTable = &const {
12
                $vtable_builder
13
                    .type_name(|f, opts| {
36✔
14
                        if let Some(opts) = opts.for_children() {
36✔
15
                            if stringify!($ptr_type) == "Raw" {
36✔
16
                                if $mutable {
NEW
17
                                    write!(f, "*mut ")?;
×
18
                                } else {
NEW
19
                                    write!(f, "*const ")?;
×
20
                                }
21
                            } else {
22
                                write!(f, "&")?;
36✔
23
                                if $mutable {
NEW
24
                                    write!(f, "mut ")?;
×
25
                                }
36✔
26
                            }
27
                            (T::VTABLE.type_name)(f, opts)
36✔
28
                        } else {
NEW
29
                            if stringify!($ptr_type) == "Raw" {
×
30
                                if $mutable {
NEW
31
                                    write!(f, "*mut ⋯")
×
32
                                } else {
NEW
33
                                    write!(f, "*const ⋯")
×
34
                                }
35
                            } else {
NEW
36
                                write!(f, "&")?;
×
37
                                if $mutable {
NEW
38
                                    write!(f, "mut ⋯")
×
39
                                } else {
NEW
40
                                    write!(f, "⋯")
×
41
                                }
42
                            }
43
                        }
44
                    })
36✔
45
                    .build()
46
            };
47

48
            const SHAPE: &'static Shape = &const {
49
                $shape
50
                    .type_params(&[TypeParam {
51
                        name: "T",
52
                        shape: || T::SHAPE,
1✔
53
                    }])
54
                    .ty({
55
                        let is_wide =
56
                            ::core::mem::size_of::<$type>() != ::core::mem::size_of::<*const ()>();
57
                        let vpt = ValuePointerType {
58
                            mutable: $mutable,
59
                            wide: is_wide,
60
                            target: || T::SHAPE,
4✔
61
                        };
62

63
                        Type::Pointer(PointerType::$ptr_type(vpt))
64
                    })
65
                    .build()
66
            };
67
        }
68
    };
69
}
70

71
// *const pointers
72
impl_facet_for_pointer!(
73
    Raw: *const T
74
        => Shape::builder_for_sized::<Self>()
NEW
75
            .inner(|| T::SHAPE)
×
76
        => ValueVTable::builder::<Self>()
77
            .marker_traits(
78
                MarkerTraits::EQ
79
                    .union(MarkerTraits::COPY)
80
                    .union(MarkerTraits::UNPIN),
81
            )
82
            .debug(fmt::Debug::fmt)
NEW
83
            .clone_into(|src, dst| unsafe { dst.put(*src) })
×
NEW
84
            .eq(|left, right| left.cast::<()>().eq(&right.cast::<()>()))
×
NEW
85
            .partial_ord(|&left, &right| {
×
NEW
86
                left.cast::<()>().partial_cmp(&right.cast::<()>())
×
NEW
87
            })
×
NEW
88
            .ord(|&left, &right| left.cast::<()>().cmp(&right.cast::<()>()))
×
NEW
89
            .hash(|value, hasher_this, hasher_write_fn| {
×
NEW
90
                value.hash(&mut unsafe {
×
NEW
91
                    HasherProxy::new(hasher_this, hasher_write_fn)
×
NEW
92
                })
×
NEW
93
            })
×
94
        => Raw, false
95
);
96

97
// *mut pointers
98
impl_facet_for_pointer!(
99
    Raw: *mut T
100
        => Shape::builder_for_sized::<Self>()
NEW
101
            .inner(|| T::SHAPE)
×
102
        => ValueVTable::builder::<Self>()
103
            .marker_traits(
104
                MarkerTraits::EQ
105
                    .union(MarkerTraits::COPY)
106
                    .union(MarkerTraits::UNPIN),
107
            )
108
            .debug(fmt::Debug::fmt)
NEW
109
            .clone_into(|src, dst| unsafe { dst.put(*src) })
×
NEW
110
            .eq(|left, right| left.cast::<()>().eq(&right.cast::<()>()))
×
NEW
111
            .partial_ord(|&left, &right| {
×
NEW
112
                left.cast::<()>().partial_cmp(&right.cast::<()>())
×
NEW
113
            })
×
NEW
114
            .ord(|&left, &right| left.cast::<()>().cmp(&right.cast::<()>()))
×
NEW
115
            .hash(|value, hasher_this, hasher_write_fn| {
×
NEW
116
                value.hash(&mut unsafe {
×
NEW
117
                    HasherProxy::new(hasher_this, hasher_write_fn)
×
NEW
118
                })
×
NEW
119
            })
×
120
        => Raw, true
121
);
122

123
// &T references
124
impl_facet_for_pointer!(
125
    Reference: &'a T
126
        => Shape::builder_for_sized::<Self>()
127
        => {
128
            let mut builder = ValueVTable::builder::<Self>()
129
                .marker_traits(
130
                    MarkerTraits::UNPIN
131
                        .union(MarkerTraits::COPY)
132
                )
NEW
133
                .clone_into(|src, dst| unsafe { dst.put(core::ptr::read(src)) });
×
134

135
            // Forward trait methods to the underlying type if it implements them
136
            if T::VTABLE.debug.is_some() {
137
                builder = builder.debug(|value, f| {
36✔
138
                    let target_ptr = crate::PtrConst::new(value);
36✔
139
                    unsafe { (T::VTABLE.debug.unwrap())(target_ptr, f) }
36✔
140
                });
36✔
141
            }
142

143
            if T::VTABLE.display.is_some() {
144
                builder = builder.display(|value, f| {
4✔
145
                    let target_ptr = crate::PtrConst::new(value);
4✔
146
                    unsafe { (T::VTABLE.display.unwrap())(target_ptr, f) }
4✔
147
                });
4✔
148
            }
149

150
            if T::VTABLE.eq.is_some() {
151
                builder = builder.eq(|a, b| {
5✔
152
                    let a_ptr = crate::PtrConst::new(a);
5✔
153
                    let b_ptr = crate::PtrConst::new(b);
5✔
154
                    unsafe { (T::VTABLE.eq.unwrap())(a_ptr, b_ptr) }
5✔
155
                });
5✔
156
            }
157

158
            if T::VTABLE.partial_ord.is_some() {
159
                builder = builder.partial_ord(|a, b| {
5✔
160
                    let a_ptr = crate::PtrConst::new(a);
5✔
161
                    let b_ptr = crate::PtrConst::new(b);
5✔
162
                    unsafe { (T::VTABLE.partial_ord.unwrap())(a_ptr, b_ptr) }
5✔
163
                });
5✔
164
            }
165

166
            if T::VTABLE.ord.is_some() {
NEW
167
                builder = builder.ord(|a, b| {
×
NEW
168
                    let a_ptr = crate::PtrConst::new(a);
×
NEW
169
                    let b_ptr = crate::PtrConst::new(b);
×
NEW
170
                    unsafe { (T::VTABLE.ord.unwrap())(a_ptr, b_ptr) }
×
NEW
171
                });
×
172
            }
173

174
            if T::VTABLE.hash.is_some() {
NEW
175
                builder = builder.hash(|value, hasher_this, hasher_write_fn| {
×
NEW
176
                    let target_ptr = crate::PtrConst::new(value);
×
NEW
177
                    unsafe { (T::VTABLE.hash.unwrap())(target_ptr, hasher_this, hasher_write_fn) }
×
NEW
178
                });
×
179
            }
180

181
            builder
182
        }
183
        => Reference, false
184
);
185

186
// &mut T references
187
impl_facet_for_pointer!(
188
    Reference: &'a mut T
189
        => Shape::builder_for_sized::<Self>()
190
        => {
191
            let mut builder = ValueVTable::builder::<Self>()
192
                .marker_traits(
193
                    MarkerTraits::UNPIN
194
                );
195

196
            // Forward trait methods to the underlying type if it implements them
197
            if T::VTABLE.debug.is_some() {
NEW
198
                builder = builder.debug(|value, f| {
×
NEW
199
                    let target_ptr = crate::PtrConst::new(value);
×
NEW
200
                    unsafe { (T::VTABLE.debug.unwrap())(target_ptr, f) }
×
NEW
201
                });
×
202
            }
203

204
            if T::VTABLE.display.is_some() {
NEW
205
                builder = builder.display(|value, f| {
×
NEW
206
                    let target_ptr = crate::PtrConst::new(value);
×
NEW
207
                    unsafe { (T::VTABLE.display.unwrap())(target_ptr, f) }
×
NEW
208
                });
×
209
            }
210

211
            if T::VTABLE.eq.is_some() {
NEW
212
                builder = builder.eq(|a, b| {
×
NEW
213
                    let a_ptr = crate::PtrConst::new(a);
×
NEW
214
                    let b_ptr = crate::PtrConst::new(b);
×
NEW
215
                    unsafe { (T::VTABLE.eq.unwrap())(a_ptr, b_ptr) }
×
NEW
216
                });
×
217
            }
218

219
            if T::VTABLE.partial_ord.is_some() {
NEW
220
                builder = builder.partial_ord(|a, b| {
×
NEW
221
                    let a_ptr = crate::PtrConst::new(a);
×
NEW
222
                    let b_ptr = crate::PtrConst::new(b);
×
NEW
223
                    unsafe { (T::VTABLE.partial_ord.unwrap())(a_ptr, b_ptr) }
×
NEW
224
                });
×
225
            }
226

227
            if T::VTABLE.ord.is_some() {
NEW
228
                builder = builder.ord(|a, b| {
×
NEW
229
                    let a_ptr = crate::PtrConst::new(a);
×
NEW
230
                    let b_ptr = crate::PtrConst::new(b);
×
NEW
231
                    unsafe { (T::VTABLE.ord.unwrap())(a_ptr, b_ptr) }
×
NEW
232
                });
×
233
            }
234

235
            if T::VTABLE.hash.is_some() {
NEW
236
                builder = builder.hash(|value, hasher_this, hasher_write_fn| {
×
NEW
237
                    let target_ptr = crate::PtrConst::new(value);
×
NEW
238
                    unsafe { (T::VTABLE.hash.unwrap())(target_ptr, hasher_this, hasher_write_fn) }
×
NEW
239
                });
×
240
            }
241

242
            builder
243
        }
244
        => Reference, true
245
);
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