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

facet-rs / facet / 15225382078

24 May 2025 08:45AM UTC coverage: 57.627% (-0.02%) from 57.643%
15225382078

push

github

fasterthanlime
Add `Shape.type_identifier` to access type name in const contexts

55 of 103 new or added lines in 23 files covered. (53.4%)

4 existing lines in 3 files now uncovered.

9909 of 17195 relevant lines covered (57.63%)

137.48 hits per line

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

89.11
/facet-core/src/impls_alloc/arc.rs
1
use core::alloc::Layout;
2

3
use crate::{
4
    Def, Facet, KnownSmartPointer, PtrConst, PtrMut, PtrUninit, Shape, ShapeLayout,
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 alloc::sync::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> + ?Sized>(
4✔
13
            src_ptr: PtrConst<'src>,
4✔
14
            src_shape: &'shape Shape<'shape>,
4✔
15
            dst: PtrUninit<'dst>,
4✔
16
        ) -> Result<PtrMut<'dst>, TryFromError<'shape>> {
4✔
17
            if src_shape.id != T::SHAPE.id {
4✔
18
                return Err(TryFromError::UnsupportedSourceShape {
×
19
                    src_shape,
×
20
                    expected: &[T::SHAPE],
×
21
                });
×
22
            }
4✔
23

24
            if let ShapeLayout::Unsized = T::SHAPE.layout {
4✔
25
                panic!("can't try_from with unsized type");
×
26
            }
4✔
27

28
            use alloc::sync::Arc;
29

30
            // Get the layout for T
31
            let layout = match T::SHAPE.layout {
4✔
32
                ShapeLayout::Sized(layout) => layout,
4✔
33
                ShapeLayout::Unsized => panic!("Unsized type not supported"),
×
34
            };
35

36
            // We'll create a new memory location, copy the value, then create an Arc from it
37
            let size_of_arc_header = core::mem::size_of::<usize>() * 2;
4✔
38

39
            // Use Layout::extend to combine header and value layout with correct alignment and padding
40
            let header_layout =
4✔
41
                Layout::from_size_align(size_of_arc_header, core::mem::align_of::<usize>())
4✔
42
                    .unwrap();
4✔
43
            let (arc_layout, value_offset) = header_layout.extend(layout).unwrap();
4✔
44

45
            // To ensure that our allocation is correct for the Arc memory model,
46
            // round up the allocation to the next multiple of 8 (Arc's alignment)
47
            let adjusted_size = (arc_layout.size() + 7) & !7;
4✔
48
            let final_layout =
4✔
49
                unsafe { Layout::from_size_align_unchecked(adjusted_size, arc_layout.align()) };
4✔
50

51
            let mem = unsafe { alloc::alloc::alloc(final_layout) };
4✔
52

53
            unsafe {
4✔
54
                // Copy the Arc header (refcounts, vtable pointer, etc.) from a dummy Arc<()>
4✔
55
                let dummy_arc = Arc::new(());
4✔
56
                let header_start = (Arc::as_ptr(&dummy_arc) as *const u8).sub(size_of_arc_header);
4✔
57
                core::ptr::copy_nonoverlapping(header_start, mem, size_of_arc_header);
4✔
58

4✔
59
                // Copy the source value into the memory area at the correct value offset after the Arc header
4✔
60
                core::ptr::copy_nonoverlapping(
4✔
61
                    src_ptr.as_byte_ptr(),
4✔
62
                    mem.add(value_offset),
4✔
63
                    layout.size(),
4✔
64
                );
4✔
65
            }
4✔
66

67
            // Create an Arc from our allocated and initialized memory
68
            let ptr = unsafe { mem.add(value_offset) };
4✔
69
            let t_ptr: *mut T = unsafe { core::mem::transmute_copy(&ptr) };
4✔
70
            let arc = unsafe { Arc::from_raw(t_ptr) };
4✔
71

72
            // Move the Arc into the destination and return a PtrMut for it
73
            Ok(unsafe { dst.put(arc) })
4✔
74
        }
4✔
75

76
        unsafe fn try_into_inner<'a, 'src, 'dst, T: Facet<'a> + ?Sized>(
1✔
77
            src_ptr: PtrMut<'src>,
1✔
78
            dst: PtrUninit<'dst>,
1✔
79
        ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
1✔
80
            use alloc::sync::Arc;
81

82
            // Read the Arc from the source pointer
83
            let mut arc = unsafe { src_ptr.read::<Arc<T>>() };
1✔
84

85
            // For unsized types, we need to know how many bytes to copy.
86
            let size = match T::SHAPE.layout {
1✔
87
                ShapeLayout::Sized(layout) => layout.size(),
1✔
88
                _ => panic!("cannot try_into_inner with unsized type"),
×
89
            };
90

91
            // Check if we have exclusive access to the Arc (strong count = 1)
92
            if let Some(inner_ref) = Arc::get_mut(&mut arc) {
1✔
93
                // We have exclusive access, so we can safely copy the inner value
94
                let inner_ptr = inner_ref as *const T as *const u8;
1✔
95

96
                unsafe {
97
                    // Copy the inner value to the destination
98
                    core::ptr::copy_nonoverlapping(inner_ptr, dst.as_mut_byte_ptr(), size);
1✔
99

100
                    // Prevent dropping the Arc normally which would also drop the inner value
101
                    // that we've already copied
102
                    let raw_ptr = Arc::into_raw(arc);
1✔
103

104
                    // We need to deallocate the Arc without running destructors
105
                    // Get the Arc layout
106
                    let size_of_arc_header = core::mem::size_of::<usize>() * 2;
1✔
107
                    let layout = match T::SHAPE.layout {
1✔
108
                        ShapeLayout::Sized(layout) => layout,
1✔
109
                        _ => unreachable!("We already checked that T is sized"),
×
110
                    };
111
                    let arc_layout = Layout::from_size_align_unchecked(
1✔
112
                        size_of_arc_header + size,
1✔
113
                        layout.align(),
1✔
114
                    );
115

116
                    // Get the start of the allocation (header is before the data)
117
                    let allocation_start = (raw_ptr as *mut u8).sub(size_of_arc_header);
1✔
118

119
                    // Deallocate the memory without running any destructors
120
                    alloc::alloc::dealloc(allocation_start, arc_layout);
1✔
121

122
                    // Return a PtrMut to the destination, which now owns the value
123
                    Ok(PtrMut::new(dst.as_mut_byte_ptr()))
1✔
124
                }
125
            } else {
126
                // Arc is shared, so we can't extract the inner value
127
                core::mem::forget(arc);
×
128
                Err(TryIntoInnerError::Unavailable)
×
129
            }
130
        }
1✔
131

132
        unsafe fn try_borrow_inner<'a, 'src, T: Facet<'a>>(
×
133
            src_ptr: PtrConst<'src>,
×
134
        ) -> Result<PtrConst<'src>, TryBorrowInnerError> {
×
135
            let arc = unsafe { src_ptr.get::<alloc::sync::Arc<T>>() };
×
136
            Ok(PtrConst::new(&**arc))
×
137
        }
×
138

139
        let mut vtable = value_vtable!(alloc::sync::Arc<T>, |f, opts| {
9✔
140
            write!(f, "{}", Self::SHAPE.type_identifier)?;
9✔
141
            if let Some(opts) = opts.for_children() {
9✔
142
                write!(f, "<")?;
9✔
143
                (T::SHAPE.vtable.type_name)(f, opts)?;
9✔
144
                write!(f, ">")?;
9✔
145
            } else {
146
                write!(f, "<…>")?;
×
147
            }
148
            Ok(())
9✔
149
        });
9✔
150

151
        vtable.try_from = || Some(try_from::<T>);
4✔
152
        vtable.try_into_inner = || Some(try_into_inner::<T>);
1✔
153
        vtable.try_borrow_inner = || Some(try_borrow_inner::<T>);
×
154
        vtable
155
    };
156

157
    const SHAPE: &'static crate::Shape<'static> = &const {
158
        // Function to return inner type's shape
159
        fn inner_shape<'a, T: Facet<'a> + ?Sized>() -> &'static Shape<'static> {
5✔
160
            T::SHAPE
5✔
161
        }
5✔
162

163
        crate::Shape::builder_for_sized::<Self>()
164
            .type_identifier("Arc")
165
            .type_params(&[crate::TypeParam {
166
                name: "T",
167
                shape: || T::SHAPE,
168
            }])
169
            .ty(Type::User(UserType::Opaque))
170
            .def(Def::SmartPointer(
171
                SmartPointerDef::builder()
172
                    .pointee(|| T::SHAPE)
173
                    .flags(SmartPointerFlags::ATOMIC)
174
                    .known(KnownSmartPointer::Arc)
175
                    .weak(|| <alloc::sync::Weak<T> as Facet>::SHAPE)
176
                    .vtable(
177
                        &const {
178
                            SmartPointerVTable::builder()
179
                                .borrow_fn(|this| {
3✔
180
                                    let ptr = Self::as_ptr(unsafe { this.get() });
3✔
181
                                    PtrConst::new(ptr)
3✔
182
                                })
3✔
183
                                .new_into_fn(|this, ptr| {
4✔
184
                                    use alloc::sync::Arc;
185

186
                                    let layout = match T::SHAPE.layout {
4✔
187
                                        ShapeLayout::Sized(layout) => layout,
4✔
188
                                        ShapeLayout::Unsized => panic!("nope"),
×
189
                                    };
190

191
                                    let size_of_arc_header = core::mem::size_of::<usize>() * 2;
4✔
192

193
                                    // we don't know the layout of dummy_arc, but we can tell its size and we can copy it
194
                                    // in front of the `PtrMut`
195
                                    let arc_layout = unsafe {
4✔
196
                                        Layout::from_size_align_unchecked(
4✔
197
                                            size_of_arc_header + layout.size(),
4✔
198
                                            layout.align(),
4✔
199
                                        )
200
                                    };
201
                                    let mem = unsafe { alloc::alloc::alloc(arc_layout) };
4✔
202

203
                                    unsafe {
4✔
204
                                        // Copy the Arc header (including refcounts, vtable pointers, etc.) from a freshly-allocated Arc<()>
4✔
205
                                        // so that the struct before the T value is a valid Arc header.
4✔
206
                                        let dummy_arc = alloc::sync::Arc::new(());
4✔
207
                                        let header_start = (Arc::as_ptr(&dummy_arc) as *const u8)
4✔
208
                                            .sub(size_of_arc_header);
4✔
209
                                        core::ptr::copy_nonoverlapping(
4✔
210
                                            header_start,
4✔
211
                                            mem,
4✔
212
                                            size_of_arc_header,
4✔
213
                                        );
4✔
214

4✔
215
                                        // Copy the value for T, pointed to by `ptr`, into the bytes just after the Arc header
4✔
216
                                        core::ptr::copy_nonoverlapping(
4✔
217
                                            ptr.as_byte_ptr(),
4✔
218
                                            mem.add(size_of_arc_header),
4✔
219
                                            layout.size(),
4✔
220
                                        );
4✔
221
                                    }
4✔
222

223
                                    // Safety: `mem` is valid and contains a valid Arc header and valid T.
224
                                    let ptr = unsafe { mem.add(size_of_arc_header) };
4✔
225
                                    let t_ptr: *mut T = unsafe { core::mem::transmute_copy(&ptr) };
4✔
226
                                    // Safety: This is the pointer to the Arc header + value; from_raw assumes a pointer to T located immediately after the Arc header.
227
                                    let arc = unsafe { Arc::from_raw(t_ptr) };
4✔
228
                                    // Move the Arc into the destination (this) and return a PtrMut for it.
229
                                    unsafe { this.put(arc) }
4✔
230
                                })
4✔
231
                                .downgrade_into_fn(|strong, weak| unsafe {
232
                                    weak.put(alloc::sync::Arc::downgrade(strong.get::<Self>()))
2✔
233
                                })
2✔
234
                                .build()
235
                        },
236
                    )
237
                    .build(),
238
            ))
239
            .inner(inner_shape::<T>)
240
            .build()
241
    };
242
}
243

244
unsafe impl<'a, T: Facet<'a>> Facet<'a> for alloc::sync::Weak<T> {
245
    const VTABLE: &'static ValueVTable = &const {
246
        value_vtable!(alloc::sync::Weak<T>, |f, opts| {
×
NEW
247
            write!(f, "{}", Self::SHAPE.type_identifier)?;
×
248
            if let Some(opts) = opts.for_children() {
×
249
                write!(f, "<")?;
×
250
                (T::SHAPE.vtable.type_name)(f, opts)?;
×
251
                write!(f, ">")?;
×
252
            } else {
253
                write!(f, "<…>")?;
×
254
            }
255
            Ok(())
×
256
        })
×
257
    };
258

259
    const SHAPE: &'static crate::Shape<'static> = &const {
260
        // Function to return inner type's shape
261
        fn inner_shape<'a, T: Facet<'a> + ?Sized>() -> &'static Shape<'static> {
×
262
            T::SHAPE
×
263
        }
×
264

265
        crate::Shape::builder_for_sized::<Self>()
266
            .type_identifier("Weak")
267
            .type_params(&[crate::TypeParam {
268
                name: "T",
269
                shape: || T::SHAPE,
270
            }])
271
            .ty(Type::User(UserType::Opaque))
272
            .def(Def::SmartPointer(
273
                SmartPointerDef::builder()
274
                    .pointee(|| T::SHAPE)
275
                    .flags(SmartPointerFlags::ATOMIC.union(SmartPointerFlags::WEAK))
276
                    .known(KnownSmartPointer::ArcWeak)
277
                    .strong(|| <alloc::sync::Arc<T> as Facet>::SHAPE)
278
                    .vtable(
279
                        &const {
280
                            SmartPointerVTable::builder()
281
                                .upgrade_into_fn(|weak, strong| unsafe {
282
                                    Some(strong.put(weak.get::<Self>().upgrade()?))
2✔
283
                                })
2✔
284
                                .build()
285
                        },
286
                    )
287
                    .build(),
288
            ))
289
            .inner(inner_shape::<T>)
290
            .build()
291
    };
292
}
293

294
#[cfg(test)]
295
mod tests {
296
    use alloc::string::String;
297
    use alloc::sync::{Arc, Weak as ArcWeak};
298

299
    use super::*;
300

301
    #[test]
302
    fn test_arc_type_params() {
1✔
303
        let [type_param_1] = <Arc<i32>>::SHAPE.type_params else {
1✔
304
            panic!("Arc<T> should only have 1 type param")
×
305
        };
306
        assert_eq!(type_param_1.shape(), i32::SHAPE);
1✔
307
    }
1✔
308

309
    #[test]
310
    fn test_arc_vtable_1_new_borrow_drop() -> eyre::Result<()> {
1✔
311
        facet_testhelpers::setup();
1✔
312

313
        let arc_shape = <Arc<String>>::SHAPE;
1✔
314
        let arc_def = arc_shape
1✔
315
            .def
1✔
316
            .into_smart_pointer()
1✔
317
            .expect("Arc<T> should have a smart pointer definition");
1✔
318

319
        // Allocate memory for the Arc
320
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
321

322
        // Get the function pointer for creating a new Arc from a value
323
        let new_into_fn = arc_def
1✔
324
            .vtable
1✔
325
            .new_into_fn
1✔
326
            .expect("Arc<T> should have new_into_fn");
1✔
327

328
        // Create the value and initialize the Arc
329
        let mut value = String::from("example");
1✔
330
        let arc_ptr = unsafe { new_into_fn(arc_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
331
        // The value now belongs to the Arc, prevent its drop
332
        core::mem::forget(value);
1✔
333

334
        // Get the function pointer for borrowing the inner value
335
        let borrow_fn = arc_def
1✔
336
            .vtable
1✔
337
            .borrow_fn
1✔
338
            .expect("Arc<T> should have borrow_fn");
1✔
339

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

345
        // Get the function pointer for dropping the Arc
346
        let drop_fn = (arc_shape.vtable.drop_in_place)().expect("Arc<T> should have drop_in_place");
1✔
347

348
        // Drop the Arc in place
349
        // SAFETY: arc_ptr points to a valid Arc<String>
350
        unsafe { drop_fn(arc_ptr) };
1✔
351

352
        // Deallocate the memory
353
        // SAFETY: arc_ptr was allocated by arc_shape and is now dropped (but memory is still valid)
354
        unsafe { arc_shape.deallocate_mut(arc_ptr)? };
1✔
355

356
        Ok(())
1✔
357
    }
1✔
358

359
    #[test]
360
    fn test_arc_vtable_2_downgrade_upgrade_drop() -> eyre::Result<()> {
1✔
361
        facet_testhelpers::setup();
1✔
362

363
        let arc_shape = <Arc<String>>::SHAPE;
1✔
364
        let arc_def = arc_shape
1✔
365
            .def
1✔
366
            .into_smart_pointer()
1✔
367
            .expect("Arc<T> should have a smart pointer definition");
1✔
368

369
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
370
        let weak_def = weak_shape
1✔
371
            .def
1✔
372
            .into_smart_pointer()
1✔
373
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
374

375
        // 1. Create the first Arc (arc1)
376
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
377
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
378
        let mut value = String::from("example");
1✔
379
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
380
        core::mem::forget(value); // Value now owned by arc1
1✔
381

382
        // 2. Downgrade arc1 to create a weak pointer (weak1)
383
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
384
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
385
        // SAFETY: arc1_ptr points to a valid Arc, weak1_uninit_ptr is allocated for a Weak
386
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
387

388
        // 3. Upgrade weak1 to create a second Arc (arc2)
389
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
390
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
391
        // SAFETY: weak1_ptr points to a valid Weak, arc2_uninit_ptr is allocated for an Arc.
392
        // Upgrade should succeed as arc1 still exists.
393
        let arc2_ptr = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) }
1✔
394
            .expect("Upgrade should succeed while original Arc exists");
1✔
395

396
        // Check the content of the upgraded Arc
397
        let borrow_fn = arc_def.vtable.borrow_fn.unwrap();
1✔
398
        // SAFETY: arc2_ptr points to a valid Arc<String>
399
        let borrowed_ptr = unsafe { borrow_fn(arc2_ptr.as_const()) };
1✔
400
        // SAFETY: borrowed_ptr points to a valid String
401
        assert_eq!(unsafe { borrowed_ptr.get::<String>() }, "example");
1✔
402

403
        // 4. Drop everything and free memory
404
        let arc_drop_fn = (arc_shape.vtable.drop_in_place)().unwrap();
1✔
405
        let weak_drop_fn = (weak_shape.vtable.drop_in_place)().unwrap();
1✔
406

407
        unsafe {
408
            // Drop Arcs
409
            arc_drop_fn(arc1_ptr);
1✔
410
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
411
            arc_drop_fn(arc2_ptr);
1✔
412
            arc_shape.deallocate_mut(arc2_ptr)?;
1✔
413

414
            // Drop Weak
415
            weak_drop_fn(weak1_ptr);
1✔
416
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
417
        }
418

419
        Ok(())
1✔
420
    }
1✔
421

422
    #[test]
423
    fn test_arc_vtable_3_downgrade_drop_try_upgrade() -> eyre::Result<()> {
1✔
424
        facet_testhelpers::setup();
1✔
425

426
        let arc_shape = <Arc<String>>::SHAPE;
1✔
427
        let arc_def = arc_shape
1✔
428
            .def
1✔
429
            .into_smart_pointer()
1✔
430
            .expect("Arc<T> should have a smart pointer definition");
1✔
431

432
        let weak_shape = <ArcWeak<String>>::SHAPE;
1✔
433
        let weak_def = weak_shape
1✔
434
            .def
1✔
435
            .into_smart_pointer()
1✔
436
            .expect("ArcWeak<T> should have a smart pointer definition");
1✔
437

438
        // 1. Create the strong Arc (arc1)
439
        let arc1_uninit_ptr = arc_shape.allocate()?;
1✔
440
        let new_into_fn = arc_def.vtable.new_into_fn.unwrap();
1✔
441
        let mut value = String::from("example");
1✔
442
        let arc1_ptr = unsafe { new_into_fn(arc1_uninit_ptr, PtrMut::new(&raw mut value)) };
1✔
443
        core::mem::forget(value);
1✔
444

445
        // 2. Downgrade arc1 to create a weak pointer (weak1)
446
        let weak1_uninit_ptr = weak_shape.allocate()?;
1✔
447
        let downgrade_into_fn = arc_def.vtable.downgrade_into_fn.unwrap();
1✔
448
        // SAFETY: arc1_ptr is valid, weak1_uninit_ptr is allocated for Weak
449
        let weak1_ptr = unsafe { downgrade_into_fn(arc1_ptr, weak1_uninit_ptr) };
1✔
450

451
        // 3. Drop and free the strong pointer (arc1)
452
        let arc_drop_fn = (arc_shape.vtable.drop_in_place)().unwrap();
1✔
453
        unsafe {
454
            arc_drop_fn(arc1_ptr);
1✔
455
            arc_shape.deallocate_mut(arc1_ptr)?;
1✔
456
        }
457

458
        // 4. Attempt to upgrade the weak pointer (weak1)
459
        let upgrade_into_fn = weak_def.vtable.upgrade_into_fn.unwrap();
1✔
460
        let arc2_uninit_ptr = arc_shape.allocate()?;
1✔
461
        // SAFETY: weak1_ptr is valid (though points to dropped data), arc2_uninit_ptr is allocated for Arc
462
        let upgrade_result = unsafe { upgrade_into_fn(weak1_ptr, arc2_uninit_ptr) };
1✔
463

464
        // Assert that the upgrade failed
465
        assert!(
1✔
466
            upgrade_result.is_none(),
1✔
467
            "Upgrade should fail after the strong Arc is dropped"
×
468
        );
469

470
        // 5. Clean up: Deallocate the memory intended for the failed upgrade and drop/deallocate the weak pointer
471
        let weak_drop_fn = (weak_shape.vtable.drop_in_place)().unwrap();
1✔
472
        unsafe {
473
            // Deallocate the *uninitialized* memory allocated for the failed upgrade attempt
474
            arc_shape.deallocate_uninit(arc2_uninit_ptr)?;
1✔
475

476
            // Drop and deallocate the weak pointer
477
            weak_drop_fn(weak1_ptr);
1✔
478
            weak_shape.deallocate_mut(weak1_ptr)?;
1✔
479
        }
480

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

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

488
        // Get the shapes we'll be working with
489
        let string_shape = <String>::SHAPE;
1✔
490
        let arc_shape = <Arc<String>>::SHAPE;
1✔
491
        let arc_def = arc_shape
1✔
492
            .def
1✔
493
            .into_smart_pointer()
1✔
494
            .expect("Arc<T> should have a smart pointer definition");
1✔
495

496
        // 1. Create a String value
497
        let value = String::from("try_from test");
1✔
498
        let value_ptr = PtrConst::new(&value as *const String as *const u8);
1✔
499

500
        // 2. Allocate memory for the Arc<String>
501
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
502

503
        // 3. Get the try_from function from the Arc<String> shape's ValueVTable
504
        let try_from_fn = (arc_shape.vtable.try_from)().expect("Arc<T> should have try_from");
1✔
505

506
        // 4. Try to convert String to Arc<String>
507
        let arc_ptr = unsafe { try_from_fn(value_ptr, string_shape, arc_uninit_ptr) }
1✔
508
            .expect("try_from should succeed");
1✔
509
        core::mem::forget(value);
1✔
510

511
        // 5. Borrow the inner value and verify it's correct
512
        let borrow_fn = arc_def
1✔
513
            .vtable
1✔
514
            .borrow_fn
1✔
515
            .expect("Arc<T> should have borrow_fn");
1✔
516
        let borrowed_ptr = unsafe { borrow_fn(arc_ptr.as_const()) };
1✔
517

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

521
        // 6. Clean up
522
        let drop_fn = (arc_shape.vtable.drop_in_place)().expect("Arc<T> should have drop_in_place");
1✔
523

524
        unsafe {
525
            drop_fn(arc_ptr);
1✔
526
            arc_shape.deallocate_mut(arc_ptr)?;
1✔
527
        }
528

529
        Ok(())
1✔
530
    }
1✔
531

532
    #[test]
533
    fn test_arc_vtable_5_try_into_inner() -> eyre::Result<()> {
1✔
534
        facet_testhelpers::setup();
1✔
535

536
        // Get the shapes we'll be working with
537
        let string_shape = <String>::SHAPE;
1✔
538
        let arc_shape = <Arc<String>>::SHAPE;
1✔
539
        let arc_def = arc_shape
1✔
540
            .def
1✔
541
            .into_smart_pointer()
1✔
542
            .expect("Arc<T> should have a smart pointer definition");
1✔
543

544
        // 1. Create an Arc<String>
545
        let arc_uninit_ptr = arc_shape.allocate()?;
1✔
546
        let new_into_fn = arc_def
1✔
547
            .vtable
1✔
548
            .new_into_fn
1✔
549
            .expect("Arc<T> should have new_into_fn");
1✔
550

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

555
        // 2. Allocate memory for the extracted String
556
        let string_uninit_ptr = string_shape.allocate()?;
1✔
557

558
        // 3. Get the try_into_inner function from the Arc<String>'s ValueVTable
559
        let try_into_inner_fn =
1✔
560
            (arc_shape.vtable.try_into_inner)().expect("Arc<T> Shape should have try_into_inner");
1✔
561

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

567
        // 5. Verify the extracted String
568
        assert_eq!(
1✔
569
            unsafe { string_ptr.as_const().get::<String>() },
1✔
570
            "try_into_inner test"
571
        );
572

573
        // 6. Clean up
574
        let string_drop_fn =
1✔
575
            (string_shape.vtable.drop_in_place)().expect("String should have drop_in_place");
1✔
576

577
        unsafe {
578
            // The Arc should already be dropped by try_into_inner
579
            // But we still need to deallocate its memory
580
            arc_shape.deallocate_mut(arc_ptr)?;
1✔
581

582
            // Drop and deallocate the extracted String
583
            string_drop_fn(string_ptr);
1✔
584
            string_shape.deallocate_mut(string_ptr)?;
1✔
585
        }
586

587
        Ok(())
1✔
588
    }
1✔
589
}
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