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

facet-rs / facet / 15844739052

24 Jun 2025 07:59AM UTC coverage: 62.24% (-0.02%) from 62.259%
15844739052

Pull #813

github

web-flow
Merge 943ca4746 into bfa76766b
Pull Request #813: Attempt to support `Arc<[u8]>`

253 of 425 new or added lines in 5 files covered. (59.53%)

3 existing lines in 1 file now uncovered.

11210 of 18011 relevant lines covered (62.24%)

152.19 hits per line

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

81.02
/facet-core/src/impls_alloc/arc.rs
1
use alloc::boxed::Box;
2
use alloc::sync::{Arc, Weak};
3
use alloc::vec::Vec;
4

5
use crate::{
6
    Def, Facet, KnownSmartPointer, PtrConst, PtrConstWide, PtrMut, PtrUninit, Shape,
7
    SliceBuilderVTable, SmartPointerDef, SmartPointerFlags, SmartPointerVTable,
8
    TryBorrowInnerError, TryFromError, TryIntoInnerError, Type, UserType, ValueVTable,
9
    value_vtable,
10
};
11

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

31
        unsafe fn try_into_inner<'a, 'src, 'dst, T: Facet<'a>>(
1✔
32
            src_ptr: PtrMut<'src>,
1✔
33
            dst: PtrUninit<'dst>,
1✔
34
        ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
1✔
35
            use alloc::sync::Arc;
36

37
            // Read the Arc from the source pointer
38
            let arc = unsafe { src_ptr.read::<Arc<T>>() };
1✔
39

40
            // Try to unwrap the Arc to get exclusive ownership
41
            match Arc::try_unwrap(arc) {
1✔
42
                Ok(inner) => Ok(unsafe { dst.put(inner) }),
1✔
43
                Err(arc) => {
×
44
                    // Arc is shared, so we can't extract the inner value
45
                    core::mem::forget(arc);
×
46
                    Err(TryIntoInnerError::Unavailable)
×
47
                }
48
            }
49
        }
1✔
50

51
        unsafe fn try_borrow_inner<'a, 'src, T: Facet<'a>>(
×
52
            src_ptr: PtrConst<'src>,
×
53
        ) -> Result<PtrConst<'src>, TryBorrowInnerError> {
×
54
            let arc = unsafe { src_ptr.get::<Arc<T>>() };
×
55
            Ok(PtrConst::new(&**arc))
×
56
        }
×
57

58
        let mut vtable = value_vtable!(alloc::sync::Arc<T>, |f, opts| {
19✔
59
            write!(f, "{}", Self::SHAPE.type_identifier)?;
19✔
60
            if let Some(opts) = opts.for_children() {
19✔
61
                write!(f, "<")?;
19✔
62
                (T::SHAPE.vtable.type_name())(f, opts)?;
19✔
63
                write!(f, ">")?;
19✔
64
            } else {
65
                write!(f, "<…>")?;
×
66
            }
67
            Ok(())
19✔
68
        });
19✔
69

70
        {
71
            let vtable = vtable.sized_mut().unwrap();
72
            vtable.try_from = || Some(try_from::<T>);
1✔
73
            vtable.try_into_inner = || Some(try_into_inner::<T>);
1✔
74
            vtable.try_borrow_inner = || Some(try_borrow_inner::<T>);
×
75
        }
76
        vtable
77
    };
78

79
    const SHAPE: &'static crate::Shape<'static> = &const {
80
        // Function to return inner type's shape
81
        fn inner_shape<'a, T: Facet<'a>>() -> &'static Shape<'static> {
×
82
            T::SHAPE
×
83
        }
×
84

85
        crate::Shape::builder_for_sized::<Self>()
86
            .type_identifier("Arc")
87
            .type_params(&[crate::TypeParam {
88
                name: "T",
89
                shape: || T::SHAPE,
90
            }])
91
            .ty(Type::User(UserType::Opaque))
92
            .def(Def::SmartPointer(
93
                SmartPointerDef::builder()
94
                    .pointee(|| T::SHAPE)
95
                    .flags(SmartPointerFlags::ATOMIC)
96
                    .known(KnownSmartPointer::Arc)
97
                    .weak(|| <Weak<T> as Facet>::SHAPE)
98
                    .vtable(
99
                        &const {
100
                            SmartPointerVTable::builder()
101
                                .borrow_fn(|this| {
4✔
102
                                    let arc_ptr = unsafe { this.as_ptr::<Arc<T>>() };
4✔
103
                                    let ptr = unsafe { Arc::as_ptr(&*arc_ptr) };
4✔
104
                                    PtrConst::new(ptr).into()
4✔
105
                                })
4✔
106
                                .new_into_fn(|this, ptr| {
11✔
107
                                    let t = unsafe { ptr.read::<T>() };
11✔
108
                                    let arc = Arc::new(t);
11✔
109
                                    unsafe { this.put(arc) }
11✔
110
                                })
11✔
111
                                .downgrade_into_fn(|strong, weak| unsafe {
112
                                    weak.put(Arc::downgrade(strong.get::<Self>()))
2✔
113
                                })
2✔
114
                                .build()
115
                        },
116
                    )
117
                    .build(),
118
            ))
119
            .inner(inner_shape::<T>)
120
            .build()
121
    };
122
}
123

124
unsafe impl<'a> Facet<'a> for Arc<str> {
125
    const VTABLE: &'static ValueVTable = &const {
126
        value_vtable!(alloc::sync::Arc<str>, |f, opts| {
8✔
127
            write!(f, "{}", Self::SHAPE.type_identifier)?;
8✔
128
            if let Some(opts) = opts.for_children() {
8✔
129
                write!(f, "<")?;
8✔
130
                (str::SHAPE.vtable.type_name())(f, opts)?;
8✔
131
                write!(f, ">")?;
8✔
132
            } else {
133
                write!(f, "<…>")?;
×
134
            }
135
            Ok(())
8✔
136
        })
8✔
137
    };
138

139
    const SHAPE: &'static crate::Shape<'static> = &const {
140
        // Function to return inner type's shape
141
        fn inner_shape() -> &'static Shape<'static> {
2✔
142
            str::SHAPE
2✔
143
        }
2✔
144

145
        crate::Shape::builder_for_sized::<Self>()
146
            .type_identifier("Arc")
147
            .type_params(&[crate::TypeParam {
148
                name: "T",
149
                shape: || str::SHAPE,
150
            }])
151
            .ty(Type::User(UserType::Opaque))
152
            .def(Def::SmartPointer(
153
                SmartPointerDef::builder()
154
                    .pointee(|| str::SHAPE)
155
                    .flags(SmartPointerFlags::ATOMIC)
156
                    .known(KnownSmartPointer::Arc)
157
                    .weak(|| <Weak<str> as Facet>::SHAPE)
158
                    .vtable(
159
                        &const {
160
                            SmartPointerVTable::builder()
161
                                .borrow_fn(|this| unsafe {
162
                                    let concrete = this.get::<Arc<str>>();
1✔
163
                                    let s: &str = concrete;
1✔
164
                                    PtrConstWide::new(&raw const *s).into()
1✔
165
                                })
1✔
166
                                .new_into_fn(|_this, _ptr| todo!())
167
                                .downgrade_into_fn(|_strong, _weak| todo!())
168
                                .build()
169
                        },
170
                    )
171
                    .build(),
172
            ))
173
            .inner(inner_shape)
174
            .build()
175
    };
176
}
177

178
unsafe impl<'a, U: Facet<'a>> Facet<'a> for Arc<[U]> {
179
    const VTABLE: &'static ValueVTable = &const {
NEW
180
        value_vtable!(alloc::sync::Arc<[U]>, |f, opts| {
×
NEW
181
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
NEW
182
            if let Some(opts) = opts.for_children() {
×
NEW
183
                write!(f, "<")?;
×
NEW
184
                (<[U]>::SHAPE.vtable.type_name())(f, opts)?;
×
NEW
185
                write!(f, ">")?;
×
186
            } else {
NEW
187
                write!(f, "<…>")?;
×
188
            }
NEW
189
            Ok(())
×
NEW
190
        })
×
191
    };
192

193
    const SHAPE: &'static crate::Shape<'static> = &const {
194
        // Function to return inner type's shape
NEW
195
        fn inner_shape<'a, U: Facet<'a>>() -> &'static Shape<'static> {
×
NEW
196
            <[U]>::SHAPE
×
NEW
197
        }
×
198

199
        fn slice_builder_new<'a, U: Facet<'a>>() -> PtrMut<'static> {
8✔
200
            let v = Box::new(Vec::<U>::new());
8✔
201
            let raw = Box::into_raw(v);
8✔
202
            PtrMut::new(raw)
8✔
203
        }
8✔
204

205
        fn slice_builder_push<'a, U: Facet<'a>>(builder: PtrMut, item: PtrMut) {
22✔
206
            unsafe {
22✔
207
                let vec = builder.as_mut::<Vec<U>>();
22✔
208
                let value = item.read::<U>();
22✔
209
                vec.push(value);
22✔
210
            }
22✔
211
        }
22✔
212

213
        fn slice_builder_convert<'a, U: Facet<'a>>(builder: PtrMut<'static>) -> PtrConst<'static> {
7✔
214
            unsafe {
215
                let vec_box = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
7✔
216
                let arc: Arc<[U]> = (*vec_box).into();
7✔
217
                let arc_box = Box::new(arc);
7✔
218
                PtrConst::new(Box::into_raw(arc_box) as *const Arc<[U]>)
7✔
219
            }
220
        }
7✔
221

222
        fn slice_builder_free<'a, U: Facet<'a>>(builder: PtrMut<'static>) {
1✔
223
            unsafe {
1✔
224
                let _ = Box::from_raw(builder.as_ptr::<Vec<U>>() as *mut Vec<U>);
1✔
225
            }
1✔
226
        }
1✔
227

228
        crate::Shape::builder_for_sized::<Self>()
229
            .type_identifier("Arc")
230
            .type_params(&[crate::TypeParam {
231
                name: "T",
232
                shape: || <[U]>::SHAPE,
233
            }])
234
            .ty(Type::User(UserType::Opaque))
235
            .def(Def::SmartPointer(
236
                SmartPointerDef::builder()
237
                    .pointee(|| <[U]>::SHAPE)
238
                    .flags(SmartPointerFlags::ATOMIC)
239
                    .known(KnownSmartPointer::Arc)
240
                    .weak(|| <Weak<[U]> as Facet>::SHAPE)
241
                    .vtable(
242
                        &const {
243
                            SmartPointerVTable::builder()
244
                                .borrow_fn(|this| unsafe {
245
                                    let concrete = this.get::<Arc<[U]>>();
1✔
246
                                    let s: &[U] = concrete;
1✔
247
                                    PtrConstWide::new(&raw const *s).into()
1✔
248
                                })
1✔
249
                                .new_into_fn(|_this, _ptr| todo!())
250
                                .downgrade_into_fn(|_strong, _weak| todo!())
251
                                .slice_builder_vtable(
252
                                    &const {
253
                                        SliceBuilderVTable::builder()
254
                                            .new_fn(slice_builder_new::<U>)
255
                                            .push_fn(slice_builder_push::<U>)
256
                                            .convert_fn(slice_builder_convert::<U>)
257
                                            .free_fn(slice_builder_free::<U>)
258
                                            .build()
259
                                    },
260
                                )
261
                                .build()
262
                        },
263
                    )
264
                    .build(),
265
            ))
266
            .inner(inner_shape::<U>)
267
            .build()
268
    };
269
}
270

271
unsafe impl<'a, T: Facet<'a>> Facet<'a> for Weak<T> {
272
    const VTABLE: &'static ValueVTable = &const {
273
        value_vtable!(alloc::sync::Weak<T>, |f, opts| {
×
274
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
275
            if let Some(opts) = opts.for_children() {
×
276
                write!(f, "<")?;
×
277
                (T::SHAPE.vtable.type_name())(f, opts)?;
×
278
                write!(f, ">")?;
×
279
            } else {
280
                write!(f, "<…>")?;
×
281
            }
282
            Ok(())
×
283
        })
×
284
    };
285

286
    const SHAPE: &'static crate::Shape<'static> = &const {
287
        // Function to return inner type's shape
288
        fn inner_shape<'a, T: Facet<'a>>() -> &'static Shape<'static> {
×
289
            T::SHAPE
×
290
        }
×
291

292
        crate::Shape::builder_for_sized::<Self>()
293
            .type_identifier("Weak")
294
            .type_params(&[crate::TypeParam {
295
                name: "T",
296
                shape: || T::SHAPE,
297
            }])
298
            .ty(Type::User(UserType::Opaque))
299
            .def(Def::SmartPointer(
300
                SmartPointerDef::builder()
301
                    .pointee(|| T::SHAPE)
302
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
303
                    .known(KnownSmartPointer::ArcWeak)
304
                    .strong(|| <Arc<T> as Facet>::SHAPE)
305
                    .vtable(
306
                        &const {
307
                            SmartPointerVTable::builder()
308
                                .upgrade_into_fn(|weak, strong| unsafe {
309
                                    Some(strong.put(weak.get::<Self>().upgrade()?))
2✔
310
                                })
2✔
311
                                .build()
312
                        },
313
                    )
314
                    .build(),
315
            ))
316
            .inner(inner_shape::<T>)
317
            .build()
318
    };
319
}
320

321
unsafe impl<'a> Facet<'a> for Weak<str> {
322
    const VTABLE: &'static ValueVTable = &const {
323
        value_vtable!(alloc::sync::Weak<str>, |f, opts| {
×
324
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
325
            if let Some(opts) = opts.for_children() {
×
326
                write!(f, "<")?;
×
327
                (str::SHAPE.vtable.type_name())(f, opts)?;
×
328
                write!(f, ">")?;
×
329
            } else {
330
                write!(f, "<…>")?;
×
331
            }
332
            Ok(())
×
333
        })
×
334
    };
335

336
    const SHAPE: &'static crate::Shape<'static> = &const {
337
        // Function to return inner type's shape
338
        fn inner_shape() -> &'static Shape<'static> {
×
339
            str::SHAPE
×
340
        }
×
341

342
        crate::Shape::builder_for_sized::<Self>()
343
            .type_identifier("Weak")
344
            .type_params(&[crate::TypeParam {
345
                name: "T",
346
                shape: || str::SHAPE,
347
            }])
348
            .ty(Type::User(UserType::Opaque))
349
            .def(Def::SmartPointer(
350
                SmartPointerDef::builder()
351
                    .pointee(|| str::SHAPE)
352
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
353
                    .known(KnownSmartPointer::ArcWeak)
354
                    .strong(|| <Arc<str> as Facet>::SHAPE)
355
                    .vtable(
356
                        &const {
357
                            SmartPointerVTable::builder()
358
                                .upgrade_into_fn(|_weak, _strong| todo!())
359
                                .build()
360
                        },
361
                    )
362
                    .build(),
363
            ))
364
            .inner(inner_shape)
365
            .build()
366
    };
367
}
368

369
unsafe impl<'a, U: Facet<'a>> Facet<'a> for Weak<[U]> {
370
    const VTABLE: &'static ValueVTable = &const {
NEW
371
        value_vtable!(alloc::sync::Weak<[U]>, |f, opts| {
×
NEW
372
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
NEW
373
            if let Some(opts) = opts.for_children() {
×
NEW
374
                write!(f, "<")?;
×
NEW
375
                (<[U]>::SHAPE.vtable.type_name())(f, opts)?;
×
NEW
376
                write!(f, ">")?;
×
377
            } else {
NEW
378
                write!(f, "<…>")?;
×
379
            }
NEW
380
            Ok(())
×
NEW
381
        })
×
382
    };
383

384
    const SHAPE: &'static crate::Shape<'static> = &const {
NEW
385
        fn inner_shape<'a, U: Facet<'a>>() -> &'static Shape<'static> {
×
NEW
386
            <[U]>::SHAPE
×
NEW
387
        }
×
388

389
        crate::Shape::builder_for_sized::<Self>()
390
            .type_identifier("Weak")
391
            .type_params(&[crate::TypeParam {
392
                name: "T",
393
                shape: || <[U]>::SHAPE,
394
            }])
395
            .ty(Type::User(UserType::Opaque))
396
            .def(Def::SmartPointer(
397
                SmartPointerDef::builder()
398
                    .pointee(|| <[U]>::SHAPE)
399
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
400
                    .known(KnownSmartPointer::ArcWeak)
401
                    .strong(|| <Arc<[U]> as Facet>::SHAPE)
402
                    .vtable(
403
                        &const {
404
                            SmartPointerVTable::builder()
405
                                .upgrade_into_fn(|weak, strong| unsafe {
NEW
406
                                    Some(strong.put(weak.get::<Self>().upgrade()?))
×
NEW
407
                                })
×
408
                                .build()
409
                        },
410
                    )
411
                    .build(),
412
            ))
413
            .inner(inner_shape::<U>)
414
            .build()
415
    };
416
}
417

418
#[cfg(test)]
419
mod tests {
420
    use alloc::string::String;
421
    use alloc::sync::{Arc, Weak as ArcWeak};
422

423
    use super::*;
424

425
    #[test]
426
    fn test_arc_type_params() {
1✔
427
        let [type_param_1] = <Arc<i32>>::SHAPE.type_params else {
1✔
428
            panic!("Arc<T> should only have 1 type param")
×
429
        };
430
        assert_eq!(type_param_1.shape(), i32::SHAPE);
1✔
431
    }
1✔
432

433
    #[test]
434
    fn test_arc_vtable_1_new_borrow_drop() -> eyre::Result<()> {
1✔
435
        facet_testhelpers::setup();
1✔
436

437
        let arc_shape = <Arc<String>>::SHAPE;
1✔
438
        let arc_def = arc_shape
1✔
439
            .def
1✔
440
            .into_smart_pointer()
1✔
441
            .expect("Arc<T> should have a smart pointer definition");
1✔
442

443
        // Allocate memory for the Arc
444
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
445

446
        // Get the function pointer for creating a new Arc from a value
447
        let new_into_fn = arc_def
1✔
448
            .vtable
1✔
449
            .new_into_fn
1✔
450
            .expect("Arc<T> should have new_into_fn");
1✔
451

452
        // Create the value and initialize the Arc
453
        let mut value = String::from("example");
1✔
454
        let arc_ptr = unsafe { new_into_fn(arc_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
455
        // The value now belongs to the Arc, prevent its drop
456
        core::mem::forget(value);
1✔
457

458
        // Get the function pointer for borrowing the inner value
459
        let borrow_fn = arc_def
1✔
460
            .vtable
1✔
461
            .borrow_fn
1✔
462
            .expect("Arc<T> should have borrow_fn");
1✔
463

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

469
        // Get the function pointer for dropping the Arc
470
        let drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)()
1✔
471
            .expect("Arc<T> should have drop_in_place");
1✔
472

473
        // Drop the Arc in place
474
        // SAFETY: arc_ptr points to a valid Arc<String>
475
        unsafe { drop_fn(arc_ptr) };
1✔
476

477
        // Deallocate the memory
478
        // SAFETY: arc_ptr was allocated by arc_shape and is now dropped (but memory is still valid)
479
        unsafe { arc_shape.deallocate_mut(arc_ptr)? };
1✔
480

481
        Ok(())
1✔
482
    }
1✔
483

484
    #[test]
485
    fn test_arc_vtable_2_downgrade_upgrade_drop() -> eyre::Result<()> {
1✔
486
        facet_testhelpers::setup();
1✔
487

488
        let arc_shape = <Arc<String>>::SHAPE;
1✔
489
        let arc_def = arc_shape
1✔
490
            .def
1✔
491
            .into_smart_pointer()
1✔
492
            .expect("Arc<T> should have a smart pointer definition");
1✔
493

494
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
495
        let weak_def = weak_shape
1✔
496
            .def
1✔
497
            .into_smart_pointer()
1✔
498
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
499

500
        // 1. Create the first Arc (arc1)
501
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
502
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
503
        let mut value = String::from("example");
1✔
504
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
505
        core::mem::forget(value); // Value now owned by arc1
1✔
506

507
        // 2. Downgrade arc1 to create a weak pointer (weak1)
508
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
509
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
510
        // SAFETY: arc1_ptr points to a valid Arc, weak1_uninit_ptr is allocated for a Weak
511
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
512

513
        // 3. Upgrade weak1 to create a second Arc (arc2)
514
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
515
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
516
        // SAFETY: weak1_ptr points to a valid Weak, arc2_uninit_ptr is allocated for an Arc.
517
        // Upgrade should succeed as arc1 still exists.
518
        let arc2_ptr = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) }
1✔
519
            .expect("Upgrade should succeed while original Arc exists");
1✔
520

521
        // Check the content of the upgraded Arc
522
        let borrow_fn = arc_def.vtable.borrow_fn.unwrap();
1✔
523
        // SAFETY: arc2_ptr points to a valid Arc<String>
524
        let borrowed_ptr = unsafe { borrow_fn(arc2_ptr.as_const()) };
1✔
525
        // SAFETY: borrowed_ptr points to a valid String
526
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
1✔
527

528
        // 4. Drop everything and free memory
529
        let arc_drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
530
        let weak_drop_fn = (weak_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
531

532
        unsafe {
533
            // Drop Arcs
534
            arc_drop_fn(arc1_ptr);
1✔
535
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
536
            arc_drop_fn(arc2_ptr);
1✔
537
            arc_shape.deallocate_mut(arc2_ptr)?;
1✔
538

539
            // Drop Weak
540
            weak_drop_fn(weak1_ptr);
1✔
541
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
542
        }
543

544
        Ok(())
1✔
545
    }
1✔
546

547
    #[test]
548
    fn test_arc_vtable_3_downgrade_drop_try_upgrade() -> eyre::Result<()> {
1✔
549
        facet_testhelpers::setup();
1✔
550

551
        let arc_shape = <Arc<String>>::SHAPE;
1✔
552
        let arc_def = arc_shape
1✔
553
            .def
1✔
554
            .into_smart_pointer()
1✔
555
            .expect("Arc<T> should have a smart pointer definition");
1✔
556

557
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
558
        let weak_def = weak_shape
1✔
559
            .def
1✔
560
            .into_smart_pointer()
1✔
561
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
562

563
        // 1. Create the strong Arc (arc1)
564
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
565
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
566
        let mut value = String::from("example");
1✔
567
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
568
        core::mem::forget(value);
1✔
569

570
        // 2. Downgrade arc1 to create a weak pointer (weak1)
571
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
572
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
573
        // SAFETY: arc1_ptr is valid, weak1_uninit_ptr is allocated for Weak
574
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
575

576
        // 3. Drop and free the strong pointer (arc1)
577
        let arc_drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
578
        unsafe {
579
            arc_drop_fn(arc1_ptr);
1✔
580
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
581
        }
582

583
        // 4. Attempt to upgrade the weak pointer (weak1)
584
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
585
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
586
        // SAFETY: weak1_ptr is valid (though points to dropped data), arc2_uninit_ptr is allocated for Arc
587
        let upgrade_result = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) };
1✔
588

589
        // Assert that the upgrade failed
590
        assert!(
1✔
591
            upgrade_result.is_none(),
1✔
592
            "Upgrade should fail after the strong Arc is dropped"
×
593
        );
594

595
        // 5. Clean up: Deallocate the memory intended for the failed upgrade and drop/deallocate the weak pointer
596
        let weak_drop_fn = (weak_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
597
        unsafe {
598
            // Deallocate the *uninitialized* memory allocated for the failed upgrade attempt
599
            arc_shape.deallocate_uninit(arc2_uninit_ptr)?;
1✔
600

601
            // Drop and deallocate the weak pointer
602
            weak_drop_fn(weak1_ptr);
1✔
603
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
604
        }
605

606
        Ok(())
1✔
607
    }
1✔
608

609
    #[test]
610
    fn test_arc_vtable_4_try_from() -> eyre::Result<()> {
1✔
611
        facet_testhelpers::setup();
1✔
612

613
        // Get the shapes we'll be working with
614
        let string_shape = <String>::SHAPE;
1✔
615
        let arc_shape = <Arc<String>>::SHAPE;
1✔
616
        let arc_def = arc_shape
1✔
617
            .def
1✔
618
            .into_smart_pointer()
1✔
619
            .expect("Arc<T> should have a smart pointer definition");
1✔
620

621
        // 1. Create a String value
622
        let value = String::from("try_from test");
1✔
623
        let value_ptr = PtrConst::new(&value as *const String as *const u8);
1✔
624

625
        // 2. Allocate memory for the Arc<String>
626
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
627

628
        // 3. Get the try_from function from the Arc<String> shape's ValueVTable
629
        let try_from_fn =
1✔
630
            (arc_shape.vtable.sized().unwrap().try_from)().expect("Arc<T> should have try_from");
1✔
631

632
        // 4. Try to convert String to Arc<String>
633
        let arc_ptr = unsafe { try_from_fn(value_ptr, string_shape, arc_uninit_ptr) }
1✔
634
            .expect("try_from should succeed");
1✔
635
        core::mem::forget(value);
1✔
636

637
        // 5. Borrow the inner value and verify it's correct
638
        let borrow_fn = arc_def
1✔
639
            .vtable
1✔
640
            .borrow_fn
1✔
641
            .expect("Arc<T> should have borrow_fn");
1✔
642
        let borrowed_ptr = unsafe { borrow_fn(arc_ptr.as_const()) };
1✔
643

644
        // SAFETY: borrowed_ptr points to a valid String within the Arc
645
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "try_from test");
1✔
646

647
        // 6. Clean up
648
        let drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)()
1✔
649
            .expect("Arc<T> should have drop_in_place");
1✔
650

651
        unsafe {
652
            drop_fn(arc_ptr);
1✔
653
            arc_shape.deallocate_mut(arc_ptr)?;
1✔
654
        }
655

656
        Ok(())
1✔
657
    }
1✔
658

659
    #[test]
660
    fn test_arc_vtable_5_try_into_inner() -> eyre::Result<()> {
1✔
661
        facet_testhelpers::setup();
1✔
662

663
        // Get the shapes we'll be working with
664
        let string_shape = <String>::SHAPE;
1✔
665
        let arc_shape = <Arc<String>>::SHAPE;
1✔
666
        let arc_def = arc_shape
1✔
667
            .def
1✔
668
            .into_smart_pointer()
1✔
669
            .expect("Arc<T> should have a smart pointer definition");
1✔
670

671
        // 1. Create an Arc<String>
672
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
673
        let new_into_fn = arc_def
1✔
674
            .vtable
1✔
675
            .new_into_fn
1✔
676
            .expect("Arc<T> should have new_into_fn");
1✔
677

678
        let mut value = String::from("try_into_inner test");
1✔
679
        let arc_ptr = unsafe { new_into_fn(arc_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
680
        core::mem::forget(value); // Value now owned by arc
1✔
681

682
        // 2. Allocate memory for the extracted String
683
        let string_uninit_ptr = string_shape.allocate()?;
1✔
684

685
        // 3. Get the try_into_inner function from the Arc<String>'s ValueVTable
686
        let try_into_inner_fn = (arc_shape.vtable.sized().unwrap().try_into_inner)()
1✔
687
            .expect("Arc<T> Shape should have try_into_inner");
1✔
688

689
        // 4. Try to extract the String from the Arc<String>
690
        // This should succeed because we have exclusive access to the Arc (strong count = 1)
691
        let string_ptr = unsafe { try_into_inner_fn(arc_ptr, string_uninit_ptr) }
1✔
692
            .expect("try_into_inner should succeed with exclusive access");
1✔
693

694
        // 5. Verify the extracted String
695
        assert_eq!(
1✔
696
            unsafe { string_ptr.as_const().get::<String>() },
1✔
697
            "try_into_inner test"
698
        );
699

700
        // 6. Clean up
701
        let string_drop_fn = (string_shape.vtable.sized().unwrap().drop_in_place)()
1✔
702
            .expect("String should have drop_in_place");
1✔
703

704
        unsafe {
705
            // The Arc should already be dropped by try_into_inner
706
            // But we still need to deallocate its memory
707
            arc_shape.deallocate_mut(arc_ptr)?;
1✔
708

709
            // Drop and deallocate the extracted String
710
            string_drop_fn(string_ptr);
1✔
711
            string_shape.deallocate_mut(string_ptr)?;
1✔
712
        }
713

714
        Ok(())
1✔
715
    }
1✔
716

717
    #[test]
718
    fn test_arc_vtable_6_slice_builder() -> eyre::Result<()> {
1✔
719
        facet_testhelpers::setup();
1✔
720

721
        // Get the shapes we'll be working with
722
        let arc_slice_shape = <Arc<[i32]>>::SHAPE;
1✔
723
        let arc_slice_def = arc_slice_shape
1✔
724
            .def
1✔
725
            .into_smart_pointer()
1✔
726
            .expect("Arc<[i32]> should have a smart pointer definition");
1✔
727

728
        // Get the slice builder vtable
729
        let slice_builder_vtable = arc_slice_def
1✔
730
            .vtable
1✔
731
            .slice_builder_vtable
1✔
732
            .expect("Arc<[i32]> should have slice_builder_vtable");
1✔
733

734
        // 1. Create a new builder
735
        let builder_ptr = (slice_builder_vtable.new_fn)();
1✔
736

737
        // 2. Push some items to the builder
738
        let push_fn = slice_builder_vtable.push_fn;
1✔
739
        let values = [1i32, 2, 3, 4, 5];
1✔
740
        for &value in &values {
6✔
741
            let mut value_copy = value;
5✔
742
            let value_ptr = PtrMut::new(&raw mut value_copy);
5✔
743
            unsafe { push_fn(builder_ptr, value_ptr) };
5✔
744
            let _ = value_copy; // Value now owned by the builder
5✔
745
        }
5✔
746

747
        // 3. Convert the builder to Arc<[i32]>
748
        let convert_fn = slice_builder_vtable.convert_fn;
1✔
749
        let arc_slice_ptr = unsafe { convert_fn(builder_ptr) };
1✔
750

751
        // 4. Verify the contents by borrowing
752
        let borrow_fn = arc_slice_def
1✔
753
            .vtable
1✔
754
            .borrow_fn
1✔
755
            .expect("Arc<[i32]> should have borrow_fn");
1✔
756
        let borrowed_ptr = unsafe { borrow_fn(arc_slice_ptr) };
1✔
757

758
        // Convert the wide pointer to a slice reference
759
        let slice = unsafe { borrowed_ptr.get::<[i32]>() };
1✔
760
        assert_eq!(slice, &[1, 2, 3, 4, 5]);
1✔
761

762
        // 5. Clean up - the Arc<[i32]> was boxed by convert_fn, we need to deallocate the Box
763
        unsafe {
1✔
764
            let _ = Box::from_raw(arc_slice_ptr.as_ptr::<Arc<[i32]>>() as *mut Arc<[i32]>);
1✔
765
        }
1✔
766

767
        Ok(())
1✔
768
    }
1✔
769

770
    #[test]
771
    fn test_arc_vtable_7_slice_builder_free() -> eyre::Result<()> {
1✔
772
        facet_testhelpers::setup();
1✔
773

774
        // Get the shapes we'll be working with
775
        let arc_slice_shape = <Arc<[String]>>::SHAPE;
1✔
776
        let arc_slice_def = arc_slice_shape
1✔
777
            .def
1✔
778
            .into_smart_pointer()
1✔
779
            .expect("Arc<[String]> should have a smart pointer definition");
1✔
780

781
        // Get the slice builder vtable
782
        let slice_builder_vtable = arc_slice_def
1✔
783
            .vtable
1✔
784
            .slice_builder_vtable
1✔
785
            .expect("Arc<[String]> should have slice_builder_vtable");
1✔
786

787
        // 1. Create a new builder
788
        let builder_ptr = (slice_builder_vtable.new_fn)();
1✔
789

790
        // 2. Push some items to the builder
791
        let push_fn = slice_builder_vtable.push_fn;
1✔
792
        let strings = ["hello", "world", "test"];
1✔
793
        for &s in &strings {
4✔
794
            let mut value = String::from(s);
3✔
795
            let value_ptr = PtrMut::new(&raw mut value);
3✔
796
            unsafe { push_fn(builder_ptr, value_ptr) };
3✔
797
            core::mem::forget(value); // Value now owned by the builder
3✔
798
        }
3✔
799

800
        // 3. Instead of converting, test the free function
801
        // This simulates abandoning the builder without creating the Arc
802
        let free_fn = slice_builder_vtable.free_fn;
1✔
803
        unsafe { free_fn(builder_ptr) };
1✔
804

805
        // If we get here without panicking, the free worked correctly
806
        Ok(())
1✔
807
    }
1✔
808
}
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