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

facet-rs / facet / 15825042488

23 Jun 2025 01:03PM UTC coverage: 61.402% (-0.09%) from 61.492%
15825042488

Pull #813

github

web-flow
Merge 28e79deda into 0648bb765
Pull Request #813: Attempt to support `Arc<[u8]>`

84 of 170 new or added lines in 4 files covered. (49.41%)

3 existing lines in 1 file now uncovered.

10676 of 17387 relevant lines covered (61.4%)

155.03 hits per line

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

74.4
/facet-core/src/impls_alloc/arc.rs
1
use alloc::sync::{Arc, Weak};
2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

196
        crate::Shape::builder_for_sized::<Self>()
197
            .type_identifier("Arc")
198
            .type_params(&[crate::TypeParam {
199
                name: "T",
200
                shape: || <[U]>::SHAPE,
201
            }])
202
            .ty(Type::User(UserType::Opaque))
203
            .def(Def::SmartPointer(
204
                SmartPointerDef::builder()
205
                    .pointee(|| <[U]>::SHAPE)
206
                    .flags(SmartPointerFlags::ATOMIC)
207
                    .known(KnownSmartPointer::Arc)
208
                    .weak(|| <Weak<[U]> as Facet>::SHAPE)
209
                    .vtable(
210
                        &const {
211
                            SmartPointerVTable::builder()
212
                                .borrow_fn(|this| unsafe {
NEW
213
                                    let concrete = this.get::<Arc<[U]>>();
×
NEW
214
                                    let s: &[U] = concrete;
×
NEW
215
                                    PtrConstWide::new(&raw const *s).into()
×
NEW
216
                                })
×
217
                                .new_into_fn(|_this, _ptr| todo!())
218
                                .downgrade_into_fn(|_strong, _weak| todo!())
219
                                .build()
220
                        },
221
                    )
222
                    .build(),
223
            ))
224
            .inner(inner_shape::<U>)
225
            .build()
226
    };
227
}
228

229
unsafe impl<'a, T: Facet<'a>> Facet<'a> for Weak<T> {
230
    const VTABLE: &'static ValueVTable = &const {
231
        value_vtable!(alloc::sync::Weak<T>, |f, opts| {
×
232
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
233
            if let Some(opts) = opts.for_children() {
×
234
                write!(f, "<")?;
×
235
                (T::SHAPE.vtable.type_name())(f, opts)?;
×
236
                write!(f, ">")?;
×
237
            } else {
238
                write!(f, "<…>")?;
×
239
            }
240
            Ok(())
×
241
        })
×
242
    };
243

244
    const SHAPE: &'static crate::Shape<'static> = &const {
245
        // Function to return inner type's shape
246
        fn inner_shape<'a, T: Facet<'a>>() -> &'static Shape<'static> {
×
247
            T::SHAPE
×
248
        }
×
249

250
        crate::Shape::builder_for_sized::<Self>()
251
            .type_identifier("Weak")
252
            .type_params(&[crate::TypeParam {
253
                name: "T",
254
                shape: || T::SHAPE,
255
            }])
256
            .ty(Type::User(UserType::Opaque))
257
            .def(Def::SmartPointer(
258
                SmartPointerDef::builder()
259
                    .pointee(|| T::SHAPE)
260
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
261
                    .known(KnownSmartPointer::ArcWeak)
262
                    .strong(|| <Arc<T> as Facet>::SHAPE)
263
                    .vtable(
264
                        &const {
265
                            SmartPointerVTable::builder()
266
                                .upgrade_into_fn(|weak, strong| unsafe {
267
                                    Some(strong.put(weak.get::<Self>().upgrade()?))
2✔
268
                                })
2✔
269
                                .build()
270
                        },
271
                    )
272
                    .build(),
273
            ))
274
            .inner(inner_shape::<T>)
275
            .build()
276
    };
277
}
278

279
unsafe impl<'a> Facet<'a> for Weak<str> {
280
    const VTABLE: &'static ValueVTable = &const {
281
        value_vtable!(alloc::sync::Weak<str>, |f, opts| {
×
282
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
283
            if let Some(opts) = opts.for_children() {
×
284
                write!(f, "<")?;
×
285
                (str::SHAPE.vtable.type_name())(f, opts)?;
×
286
                write!(f, ">")?;
×
287
            } else {
288
                write!(f, "<…>")?;
×
289
            }
290
            Ok(())
×
291
        })
×
292
    };
293

294
    const SHAPE: &'static crate::Shape<'static> = &const {
295
        // Function to return inner type's shape
296
        fn inner_shape() -> &'static Shape<'static> {
×
297
            str::SHAPE
×
298
        }
×
299

300
        crate::Shape::builder_for_sized::<Self>()
301
            .type_identifier("Weak")
302
            .type_params(&[crate::TypeParam {
303
                name: "T",
304
                shape: || str::SHAPE,
305
            }])
306
            .ty(Type::User(UserType::Opaque))
307
            .def(Def::SmartPointer(
308
                SmartPointerDef::builder()
309
                    .pointee(|| str::SHAPE)
310
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
311
                    .known(KnownSmartPointer::ArcWeak)
312
                    .strong(|| <Arc<str> as Facet>::SHAPE)
313
                    .vtable(
314
                        &const {
315
                            SmartPointerVTable::builder()
316
                                .upgrade_into_fn(|_weak, _strong| todo!())
317
                                .build()
318
                        },
319
                    )
320
                    .build(),
321
            ))
322
            .inner(inner_shape)
323
            .build()
324
    };
325
}
326

327
unsafe impl<'a, U: Facet<'a>> Facet<'a> for Weak<[U]> {
328
    const VTABLE: &'static ValueVTable = &const {
NEW
329
        value_vtable!(alloc::sync::Weak<[U]>, |f, opts| {
×
NEW
330
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
NEW
331
            if let Some(opts) = opts.for_children() {
×
NEW
332
                write!(f, "<")?;
×
NEW
333
                (<[U]>::SHAPE.vtable.type_name())(f, opts)?;
×
NEW
334
                write!(f, ">")?;
×
335
            } else {
NEW
336
                write!(f, "<…>")?;
×
337
            }
NEW
338
            Ok(())
×
NEW
339
        })
×
340
    };
341

342
    const SHAPE: &'static crate::Shape<'static> = &const {
NEW
343
        fn inner_shape<'a, U: Facet<'a>>() -> &'static Shape<'static> {
×
NEW
344
            <[U]>::SHAPE
×
NEW
345
        }
×
346

347
        crate::Shape::builder_for_sized::<Self>()
348
            .type_identifier("Weak")
349
            .type_params(&[crate::TypeParam {
350
                name: "T",
351
                shape: || <[U]>::SHAPE,
352
            }])
353
            .ty(Type::User(UserType::Opaque))
354
            .def(Def::SmartPointer(
355
                SmartPointerDef::builder()
356
                    .pointee(|| <[U]>::SHAPE)
357
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
358
                    .known(KnownSmartPointer::ArcWeak)
359
                    .strong(|| <Arc<[U]> as Facet>::SHAPE)
360
                    .vtable(
361
                        &const {
362
                            SmartPointerVTable::builder()
363
                                .upgrade_into_fn(|weak, strong| unsafe {
NEW
364
                                    Some(strong.put(weak.get::<Self>().upgrade()?))
×
NEW
365
                                })
×
366
                                .build()
367
                        },
368
                    )
369
                    .build(),
370
            ))
371
            .inner(inner_shape::<U>)
372
            .build()
373
    };
374
}
375

376
#[cfg(test)]
377
mod tests {
378
    use alloc::string::String;
379
    use alloc::sync::{Arc, Weak as ArcWeak};
380

381
    use super::*;
382

383
    #[test]
384
    fn test_arc_type_params() {
1✔
385
        let [type_param_1] = <Arc<i32>>::SHAPE.type_params else {
1✔
386
            panic!("Arc<T> should only have 1 type param")
×
387
        };
388
        assert_eq!(type_param_1.shape(), i32::SHAPE);
1✔
389
    }
1✔
390

391
    #[test]
392
    fn test_arc_vtable_1_new_borrow_drop() -> eyre::Result<()> {
1✔
393
        facet_testhelpers::setup();
1✔
394

395
        let arc_shape = <Arc<String>>::SHAPE;
1✔
396
        let arc_def = arc_shape
1✔
397
            .def
1✔
398
            .into_smart_pointer()
1✔
399
            .expect("Arc<T> should have a smart pointer definition");
1✔
400

401
        // Allocate memory for the Arc
402
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
403

404
        // Get the function pointer for creating a new Arc from a value
405
        let new_into_fn = arc_def
1✔
406
            .vtable
1✔
407
            .new_into_fn
1✔
408
            .expect("Arc<T> should have new_into_fn");
1✔
409

410
        // Create the value and initialize the Arc
411
        let mut value = String::from("example");
1✔
412
        let arc_ptr = unsafe { new_into_fn(arc_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
413
        // The value now belongs to the Arc, prevent its drop
414
        core::mem::forget(value);
1✔
415

416
        // Get the function pointer for borrowing the inner value
417
        let borrow_fn = arc_def
1✔
418
            .vtable
1✔
419
            .borrow_fn
1✔
420
            .expect("Arc<T> should have borrow_fn");
1✔
421

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

427
        // Get the function pointer for dropping the Arc
428
        let drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)()
1✔
429
            .expect("Arc<T> should have drop_in_place");
1✔
430

431
        // Drop the Arc in place
432
        // SAFETY: arc_ptr points to a valid Arc<String>
433
        unsafe { drop_fn(arc_ptr) };
1✔
434

435
        // Deallocate the memory
436
        // SAFETY: arc_ptr was allocated by arc_shape and is now dropped (but memory is still valid)
437
        unsafe { arc_shape.deallocate_mut(arc_ptr)? };
1✔
438

439
        Ok(())
1✔
440
    }
1✔
441

442
    #[test]
443
    fn test_arc_vtable_2_downgrade_upgrade_drop() -> eyre::Result<()> {
1✔
444
        facet_testhelpers::setup();
1✔
445

446
        let arc_shape = <Arc<String>>::SHAPE;
1✔
447
        let arc_def = arc_shape
1✔
448
            .def
1✔
449
            .into_smart_pointer()
1✔
450
            .expect("Arc<T> should have a smart pointer definition");
1✔
451

452
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
453
        let weak_def = weak_shape
1✔
454
            .def
1✔
455
            .into_smart_pointer()
1✔
456
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
457

458
        // 1. Create the first Arc (arc1)
459
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
460
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
461
        let mut value = String::from("example");
1✔
462
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
463
        core::mem::forget(value); // Value now owned by arc1
1✔
464

465
        // 2. Downgrade arc1 to create a weak pointer (weak1)
466
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
467
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
468
        // SAFETY: arc1_ptr points to a valid Arc, weak1_uninit_ptr is allocated for a Weak
469
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
470

471
        // 3. Upgrade weak1 to create a second Arc (arc2)
472
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
473
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
474
        // SAFETY: weak1_ptr points to a valid Weak, arc2_uninit_ptr is allocated for an Arc.
475
        // Upgrade should succeed as arc1 still exists.
476
        let arc2_ptr = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) }
1✔
477
            .expect("Upgrade should succeed while original Arc exists");
1✔
478

479
        // Check the content of the upgraded Arc
480
        let borrow_fn = arc_def.vtable.borrow_fn.unwrap();
1✔
481
        // SAFETY: arc2_ptr points to a valid Arc<String>
482
        let borrowed_ptr = unsafe { borrow_fn(arc2_ptr.as_const()) };
1✔
483
        // SAFETY: borrowed_ptr points to a valid String
484
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
1✔
485

486
        // 4. Drop everything and free memory
487
        let arc_drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
488
        let weak_drop_fn = (weak_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
489

490
        unsafe {
491
            // Drop Arcs
492
            arc_drop_fn(arc1_ptr);
1✔
493
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
494
            arc_drop_fn(arc2_ptr);
1✔
495
            arc_shape.deallocate_mut(arc2_ptr)?;
1✔
496

497
            // Drop Weak
498
            weak_drop_fn(weak1_ptr);
1✔
499
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
500
        }
501

502
        Ok(())
1✔
503
    }
1✔
504

505
    #[test]
506
    fn test_arc_vtable_3_downgrade_drop_try_upgrade() -> eyre::Result<()> {
1✔
507
        facet_testhelpers::setup();
1✔
508

509
        let arc_shape = <Arc<String>>::SHAPE;
1✔
510
        let arc_def = arc_shape
1✔
511
            .def
1✔
512
            .into_smart_pointer()
1✔
513
            .expect("Arc<T> should have a smart pointer definition");
1✔
514

515
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
516
        let weak_def = weak_shape
1✔
517
            .def
1✔
518
            .into_smart_pointer()
1✔
519
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
520

521
        // 1. Create the strong Arc (arc1)
522
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
523
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
524
        let mut value = String::from("example");
1✔
525
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
526
        core::mem::forget(value);
1✔
527

528
        // 2. Downgrade arc1 to create a weak pointer (weak1)
529
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
530
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
531
        // SAFETY: arc1_ptr is valid, weak1_uninit_ptr is allocated for Weak
532
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
533

534
        // 3. Drop and free the strong pointer (arc1)
535
        let arc_drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
536
        unsafe {
537
            arc_drop_fn(arc1_ptr);
1✔
538
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
539
        }
540

541
        // 4. Attempt to upgrade the weak pointer (weak1)
542
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
543
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
544
        // SAFETY: weak1_ptr is valid (though points to dropped data), arc2_uninit_ptr is allocated for Arc
545
        let upgrade_result = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) };
1✔
546

547
        // Assert that the upgrade failed
548
        assert!(
1✔
549
            upgrade_result.is_none(),
1✔
550
            "Upgrade should fail after the strong Arc is dropped"
×
551
        );
552

553
        // 5. Clean up: Deallocate the memory intended for the failed upgrade and drop/deallocate the weak pointer
554
        let weak_drop_fn = (weak_shape.vtable.sized().unwrap().drop_in_place)().unwrap();
1✔
555
        unsafe {
556
            // Deallocate the *uninitialized* memory allocated for the failed upgrade attempt
557
            arc_shape.deallocate_uninit(arc2_uninit_ptr)?;
1✔
558

559
            // Drop and deallocate the weak pointer
560
            weak_drop_fn(weak1_ptr);
1✔
561
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
562
        }
563

564
        Ok(())
1✔
565
    }
1✔
566

567
    #[test]
568
    fn test_arc_vtable_4_try_from() -> eyre::Result<()> {
1✔
569
        facet_testhelpers::setup();
1✔
570

571
        // Get the shapes we'll be working with
572
        let string_shape = <String>::SHAPE;
1✔
573
        let arc_shape = <Arc<String>>::SHAPE;
1✔
574
        let arc_def = arc_shape
1✔
575
            .def
1✔
576
            .into_smart_pointer()
1✔
577
            .expect("Arc<T> should have a smart pointer definition");
1✔
578

579
        // 1. Create a String value
580
        let value = String::from("try_from test");
1✔
581
        let value_ptr = PtrConst::new(&value as *const String as *const u8);
1✔
582

583
        // 2. Allocate memory for the Arc<String>
584
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
585

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

590
        // 4. Try to convert String to Arc<String>
591
        let arc_ptr = unsafe { try_from_fn(value_ptr, string_shape, arc_uninit_ptr) }
1✔
592
            .expect("try_from should succeed");
1✔
593
        core::mem::forget(value);
1✔
594

595
        // 5. Borrow the inner value and verify it's correct
596
        let borrow_fn = arc_def
1✔
597
            .vtable
1✔
598
            .borrow_fn
1✔
599
            .expect("Arc<T> should have borrow_fn");
1✔
600
        let borrowed_ptr = unsafe { borrow_fn(arc_ptr.as_const()) };
1✔
601

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

605
        // 6. Clean up
606
        let drop_fn = (arc_shape.vtable.sized().unwrap().drop_in_place)()
1✔
607
            .expect("Arc<T> should have drop_in_place");
1✔
608

609
        unsafe {
610
            drop_fn(arc_ptr);
1✔
611
            arc_shape.deallocate_mut(arc_ptr)?;
1✔
612
        }
613

614
        Ok(())
1✔
615
    }
1✔
616

617
    #[test]
618
    fn test_arc_vtable_5_try_into_inner() -> eyre::Result<()> {
1✔
619
        facet_testhelpers::setup();
1✔
620

621
        // Get the shapes we'll be working with
622
        let string_shape = <String>::SHAPE;
1✔
623
        let arc_shape = <Arc<String>>::SHAPE;
1✔
624
        let arc_def = arc_shape
1✔
625
            .def
1✔
626
            .into_smart_pointer()
1✔
627
            .expect("Arc<T> should have a smart pointer definition");
1✔
628

629
        // 1. Create an Arc<String>
630
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
631
        let new_into_fn = arc_def
1✔
632
            .vtable
1✔
633
            .new_into_fn
1✔
634
            .expect("Arc<T> should have new_into_fn");
1✔
635

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

640
        // 2. Allocate memory for the extracted String
641
        let string_uninit_ptr = string_shape.allocate()?;
1✔
642

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

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

652
        // 5. Verify the extracted String
653
        assert_eq!(
1✔
654
            unsafe { string_ptr.as_const().get::<String>() },
1✔
655
            "try_into_inner test"
656
        );
657

658
        // 6. Clean up
659
        let string_drop_fn = (string_shape.vtable.sized().unwrap().drop_in_place)()
1✔
660
            .expect("String should have drop_in_place");
1✔
661

662
        unsafe {
663
            // The Arc should already be dropped by try_into_inner
664
            // But we still need to deallocate its memory
665
            arc_shape.deallocate_mut(arc_ptr)?;
1✔
666

667
            // Drop and deallocate the extracted String
668
            string_drop_fn(string_ptr);
1✔
669
            string_shape.deallocate_mut(string_ptr)?;
1✔
670
        }
671

672
        Ok(())
1✔
673
    }
1✔
674
}
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