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

facet-rs / facet / 14892196246

07 May 2025 08:01PM UTC coverage: 58.557% (+1.2%) from 57.323%
14892196246

push

github

fasterthanlime
Clean up facet-deserialize

Rename it to facet-deserialize, add missing metadata, etc.

2 of 3 new or added lines in 1 file covered. (66.67%)

73 existing lines in 7 files now uncovered.

8051 of 13749 relevant lines covered (58.56%)

122.71 hits per line

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

79.63
/facet-core/src/impls_alloc/arc.rs
1
use crate::{
2
    Def, Facet, KnownSmartPointer, PtrConst, PtrMut, PtrUninit, Shape, SmartPointerDef,
3
    SmartPointerFlags, SmartPointerVTable, TryBorrowInnerError, TryFromError, TryIntoInnerError,
4
    value_vtable,
5
};
6

7
unsafe impl<'a, T: Facet<'a>> Facet<'a> for alloc::sync::Arc<T> {
8
    const SHAPE: &'static crate::Shape = &const {
9
        // Define the functions for transparent conversion between Arc<T> and T
10
        unsafe fn try_from<'a, 'src, 'dst, T: Facet<'a>>(
1✔
11
            src_ptr: PtrConst<'src>,
1✔
12
            src_shape: &'static Shape,
1✔
13
            dst: PtrUninit<'dst>,
1✔
14
        ) -> Result<PtrMut<'dst>, TryFromError> {
1✔
15
            if src_shape.id != T::SHAPE.id {
1✔
16
                return Err(TryFromError::UnsupportedSourceShape {
×
17
                    src_shape,
×
18
                    expected: &[T::SHAPE],
×
19
                });
×
20
            }
1✔
21
            let t = unsafe { src_ptr.read::<T>() };
1✔
22
            let arc = alloc::sync::Arc::new(t);
1✔
23
            Ok(unsafe { dst.put(arc) })
1✔
24
        }
1✔
25

26
        unsafe fn try_into_inner<'a, 'src, 'dst, T: Facet<'a>>(
×
27
            src_ptr: PtrConst<'src>,
×
28
            dst: PtrUninit<'dst>,
×
29
        ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
×
30
            let arc = unsafe { src_ptr.get::<alloc::sync::Arc<T>>() };
×
31
            match alloc::sync::Arc::try_unwrap(arc.clone()) {
×
32
                Ok(t) => Ok(unsafe { dst.put(t) }),
×
33
                Err(_) => Err(TryIntoInnerError::Unavailable),
×
34
            }
35
        }
×
36

37
        unsafe fn try_borrow_inner<'a, 'src, T: Facet<'a>>(
×
38
            src_ptr: PtrConst<'src>,
×
39
        ) -> Result<PtrConst<'src>, TryBorrowInnerError> {
×
40
            let arc = unsafe { src_ptr.get::<alloc::sync::Arc<T>>() };
×
41
            Ok(PtrConst::new(&**arc))
×
42
        }
×
43

44
        // Function to return inner type's shape
45
        fn inner_shape<'a, T: Facet<'a>>() -> &'static Shape {
2✔
46
            T::SHAPE
2✔
47
        }
2✔
48

49
        crate::Shape::builder_for_sized::<Self>()
50
            .type_params(&[crate::TypeParam {
51
                name: "T",
52
                shape: || T::SHAPE,
2✔
53
            }])
54
            .def(Def::SmartPointer(
55
                SmartPointerDef::builder()
56
                    .pointee(T::SHAPE)
57
                    .flags(SmartPointerFlags::ATOMIC)
58
                    .known(KnownSmartPointer::Arc)
59
                    .weak(|| <alloc::sync::Weak<T> as Facet>::SHAPE)
×
60
                    .vtable(
61
                        &const {
62
                            SmartPointerVTable::builder()
63
                                .borrow_fn(|this| {
2✔
64
                                    let ptr = Self::as_ptr(unsafe { this.get() });
2✔
65
                                    PtrConst::new(ptr)
2✔
66
                                })
2✔
67
                                .new_into_fn(|this, ptr| {
3✔
68
                                    let t = unsafe { ptr.read::<T>() };
3✔
69
                                    let arc = alloc::sync::Arc::new(t);
3✔
70
                                    unsafe { this.put(arc) }
3✔
71
                                })
3✔
72
                                .downgrade_into_fn(|strong, weak| unsafe {
2✔
73
                                    weak.put(alloc::sync::Arc::downgrade(strong.get::<Self>()))
2✔
74
                                })
2✔
75
                                .build()
76
                        },
77
                    )
78
                    .build(),
79
            ))
80
            .vtable(
81
                &const {
UNCOV
82
                    let mut vtable = value_vtable!(alloc::sync::Arc<T>, |f, opts| {
×
UNCOV
83
                        write!(f, "Arc")?;
×
UNCOV
84
                        if let Some(opts) = opts.for_children() {
×
UNCOV
85
                            write!(f, "<")?;
×
UNCOV
86
                            (T::SHAPE.vtable.type_name)(f, opts)?;
×
UNCOV
87
                            write!(f, ">")?;
×
88
                        } else {
89
                            write!(f, "<…>")?;
×
90
                        }
UNCOV
91
                        Ok(())
×
UNCOV
92
                    });
×
93
                    vtable.try_from = Some(try_from::<T>);
94
                    vtable.try_into_inner = Some(try_into_inner::<T>);
95
                    vtable.try_borrow_inner = Some(try_borrow_inner::<T>);
96
                    vtable
97
                },
98
            )
99
            .inner(inner_shape::<T>)
100
            .build()
101
    };
102
}
103

104
unsafe impl<'a, T: Facet<'a>> Facet<'a> for alloc::sync::Weak<T> {
105
    const SHAPE: &'static crate::Shape = &const {
106
        // Function to return inner type's shape
107
        fn inner_shape<'a, T: Facet<'a>>() -> &'static Shape {
×
108
            T::SHAPE
×
109
        }
×
110

111
        crate::Shape::builder_for_sized::<Self>()
112
            .type_params(&[crate::TypeParam {
113
                name: "T",
114
                shape: || T::SHAPE,
1✔
115
            }])
116
            .def(Def::SmartPointer(
117
                SmartPointerDef::builder()
118
                    .pointee(T::SHAPE)
119
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
120
                    .known(KnownSmartPointer::ArcWeak)
121
                    .strong(|| <alloc::sync::Arc<T> as Facet>::SHAPE)
×
122
                    .vtable(
123
                        &const {
124
                            SmartPointerVTable::builder()
125
                                .upgrade_into_fn(|weak, strong| unsafe {
2✔
126
                                    Some(strong.put(weak.get::<Self>().upgrade()?))
2✔
127
                                })
2✔
128
                                .build()
129
                        },
130
                    )
131
                    .build(),
132
            ))
133
            .vtable(
134
                &const {
135
                    value_vtable!(alloc::sync::Weak<T>, |f, opts| {
×
136
                        write!(f, "Weak")?;
×
137
                        if let Some(opts) = opts.for_children() {
×
138
                            write!(f, "<")?;
×
139
                            (T::SHAPE.vtable.type_name)(f, opts)?;
×
140
                            write!(f, ">")?;
×
141
                        } else {
142
                            write!(f, "<…>")?;
×
143
                        }
144
                        Ok(())
×
145
                    })
×
146
                },
147
            )
148
            .inner(inner_shape::<T>)
149
            .build()
150
    };
151
}
152

153
#[cfg(test)]
154
mod tests {
155
    use alloc::string::String;
156
    use alloc::sync::{Arc, Weak as ArcWeak};
157

158
    use super::*;
159

160
    #[test]
161
    fn test_arc_type_params() {
1✔
162
        let [type_param_1] = <Arc<i32>>::SHAPE.type_params else {
1✔
163
            panic!("Arc<T> should only have 1 type param")
×
164
        };
165
        assert_eq!(type_param_1.shape(), i32::SHAPE);
1✔
166
    }
1✔
167

168
    #[test]
169
    fn test_arc_vtable_1_new_borrow_drop() -> eyre::Result<()> {
1✔
170
        facet_testhelpers::setup();
1✔
171

1✔
172
        let arc_shape = <Arc<String>>::SHAPE;
1✔
173
        let arc_def = arc_shape
1✔
174
            .def
1✔
175
            .into_smart_pointer()
1✔
176
            .expect("Arc<T> should have a smart pointer definition");
1✔
177

178
        // Allocate memory for the Arc
179
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
180

181
        // Get the function pointer for creating a new Arc from a value
182
        let new_into_fn = arc_def
1✔
183
            .vtable
1✔
184
            .new_into_fn
1✔
185
            .expect("Arc<T> should have new_into_fn");
1✔
186

1✔
187
        // Create the value and initialize the Arc
1✔
188
        let value = String::from("example");
1✔
189
        let arc_ptr = unsafe { new_into_fn(arc_uninit_ptr, PtrConst::new(&raw const value)) };
1✔
190
        // The value now belongs to the Arc, prevent its drop
1✔
191
        core::mem::forget(value);
1✔
192

1✔
193
        // Get the function pointer for borrowing the inner value
1✔
194
        let borrow_fn = arc_def
1✔
195
            .vtable
1✔
196
            .borrow_fn
1✔
197
            .expect("Arc<T> should have borrow_fn");
1✔
198

1✔
199
        // Borrow the inner value and check it
1✔
200
        let borrowed_ptr = unsafe { borrow_fn(arc_ptr.as_const()) };
1✔
201
        // SAFETY: borrowed_ptr points to a valid String within the Arc
1✔
202
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
1✔
203

204
        // Get the function pointer for dropping the Arc
205
        let drop_fn = arc_shape
1✔
206
            .vtable
1✔
207
            .drop_in_place
1✔
208
            .expect("Arc<T> should have drop_in_place");
1✔
209

1✔
210
        // Drop the Arc in place
1✔
211
        // SAFETY: arc_ptr points to a valid Arc<String>
1✔
212
        unsafe { drop_fn(arc_ptr) };
1✔
213

1✔
214
        // Deallocate the memory
1✔
215
        // SAFETY: arc_ptr was allocated by arc_shape and is now dropped (but memory is still valid)
1✔
216
        unsafe { arc_shape.deallocate_mut(arc_ptr)? };
1✔
217

218
        Ok(())
1✔
219
    }
1✔
220

221
    #[test]
222
    fn test_arc_vtable_2_downgrade_upgrade_drop() -> eyre::Result<()> {
1✔
223
        facet_testhelpers::setup();
1✔
224

1✔
225
        let arc_shape = <Arc<String>>::SHAPE;
1✔
226
        let arc_def = arc_shape
1✔
227
            .def
1✔
228
            .into_smart_pointer()
1✔
229
            .expect("Arc<T> should have a smart pointer definition");
1✔
230

1✔
231
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
232
        let weak_def = weak_shape
1✔
233
            .def
1✔
234
            .into_smart_pointer()
1✔
235
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
236

237
        // 1. Create the first Arc (arc1)
238
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
239
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
240
        let value = String::from("example");
1✔
241
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrConst::new(&raw const value)) };
1✔
242
        core::mem::forget(value); // Value now owned by arc1
1✔
243

244
        // 2. Downgrade arc1 to create a weak pointer (weak1)
245
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
246
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
247
        // SAFETY: arc1_ptr points to a valid Arc, weak1_uninit_ptr is allocated for a Weak
1✔
248
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
249

250
        // 3. Upgrade weak1 to create a second Arc (arc2)
251
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
252
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
253
        // SAFETY: weak1_ptr points to a valid Weak, arc2_uninit_ptr is allocated for an Arc.
1✔
254
        // Upgrade should succeed as arc1 still exists.
1✔
255
        let arc2_ptr = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) }
1✔
256
            .expect("Upgrade should succeed while original Arc exists");
1✔
257

1✔
258
        // Check the content of the upgraded Arc
1✔
259
        let borrow_fn = arc_def.vtable.borrow_fn.unwrap();
1✔
260
        // SAFETY: arc2_ptr points to a valid Arc<String>
1✔
261
        let borrowed_ptr = unsafe { borrow_fn(arc2_ptr.as_const()) };
1✔
262
        // SAFETY: borrowed_ptr points to a valid String
1✔
263
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
1✔
264

265
        // 4. Drop everything and free memory
266
        let arc_drop_fn = arc_shape.vtable.drop_in_place.unwrap();
1✔
267
        let weak_drop_fn = weak_shape.vtable.drop_in_place.unwrap();
1✔
268

1✔
269
        unsafe {
1✔
270
            // Drop Arcs
1✔
271
            arc_drop_fn(arc1_ptr);
1✔
272
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
273
            arc_drop_fn(arc2_ptr);
1✔
274
            arc_shape.deallocate_mut(arc2_ptr)?;
1✔
275

276
            // Drop Weak
277
            weak_drop_fn(weak1_ptr);
1✔
278
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
279
        }
280

281
        Ok(())
1✔
282
    }
1✔
283

284
    #[test]
285
    fn test_arc_vtable_3_downgrade_drop_try_upgrade() -> eyre::Result<()> {
1✔
286
        facet_testhelpers::setup();
1✔
287

1✔
288
        let arc_shape = <Arc<String>>::SHAPE;
1✔
289
        let arc_def = arc_shape
1✔
290
            .def
1✔
291
            .into_smart_pointer()
1✔
292
            .expect("Arc<T> should have a smart pointer definition");
1✔
293

1✔
294
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
295
        let weak_def = weak_shape
1✔
296
            .def
1✔
297
            .into_smart_pointer()
1✔
298
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
299

300
        // 1. Create the strong Arc (arc1)
301
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
302
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
303
        let value = String::from("example");
1✔
304
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrConst::new(&raw const value)) };
1✔
305
        core::mem::forget(value);
1✔
306

307
        // 2. Downgrade arc1 to create a weak pointer (weak1)
308
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
309
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
310
        // SAFETY: arc1_ptr is valid, weak1_uninit_ptr is allocated for Weak
1✔
311
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
312

1✔
313
        // 3. Drop and free the strong pointer (arc1)
1✔
314
        let arc_drop_fn = arc_shape.vtable.drop_in_place.unwrap();
1✔
315
        unsafe {
1✔
316
            arc_drop_fn(arc1_ptr);
1✔
317
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
318
        }
319

320
        // 4. Attempt to upgrade the weak pointer (weak1)
321
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
322
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
323
        // SAFETY: weak1_ptr is valid (though points to dropped data), arc2_uninit_ptr is allocated for Arc
324
        let upgrade_result = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) };
1✔
325

1✔
326
        // Assert that the upgrade failed
1✔
327
        assert!(
1✔
328
            upgrade_result.is_none(),
1✔
329
            "Upgrade should fail after the strong Arc is dropped"
×
330
        );
331

332
        // 5. Clean up: Deallocate the memory intended for the failed upgrade and drop/deallocate the weak pointer
333
        let weak_drop_fn = weak_shape.vtable.drop_in_place.unwrap();
1✔
334
        unsafe {
1✔
335
            // Deallocate the *uninitialized* memory allocated for the failed upgrade attempt
1✔
336
            arc_shape.deallocate_uninit(arc2_uninit_ptr)?;
1✔
337

338
            // Drop and deallocate the weak pointer
339
            weak_drop_fn(weak1_ptr);
1✔
340
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
341
        }
342

343
        Ok(())
1✔
344
    }
1✔
345
}
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