rune/runtime/
any_obj.rs

1use core::any::TypeId;
2use core::cell::Cell;
3use core::fmt;
4use core::mem::{needs_drop, offset_of, replace, ManuallyDrop};
5use core::ptr::{self, addr_of, addr_of_mut, drop_in_place, NonNull};
6
7use crate::alloc::alloc::Global;
8use crate::alloc::{self, Box};
9use crate::{Any, Hash};
10
11use super::{
12    Access, AccessError, AnyTypeInfo, BorrowMut, BorrowRef, Mut, RawAccessGuard, RawAnyGuard, Ref,
13    RefVtable, Snapshot, TypeInfo, VmErrorKind,
14};
15
16#[derive(Debug)]
17#[cfg_attr(test, derive(PartialEq))]
18pub(super) enum AnyObjErrorKind {
19    Cast(AnyTypeInfo, TypeInfo),
20    AccessError(AccessError),
21}
22
23/// Errors caused when accessing or coercing an [`AnyObj`].
24#[cfg_attr(test, derive(PartialEq))]
25pub struct AnyObjError {
26    kind: AnyObjErrorKind,
27}
28
29impl AnyObjError {
30    fn new(kind: AnyObjErrorKind) -> Self {
31        Self { kind }
32    }
33
34    #[inline]
35    pub(super) fn into_kind(self) -> AnyObjErrorKind {
36        self.kind
37    }
38}
39
40impl core::error::Error for AnyObjError {}
41
42impl fmt::Display for AnyObjError {
43    #[inline]
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match &self.kind {
46            AnyObjErrorKind::Cast(expected, actual) => {
47                write!(f, "Failed to cast `{actual}` to `{expected}`")
48            }
49            AnyObjErrorKind::AccessError(error) => error.fmt(f),
50        }
51    }
52}
53
54impl fmt::Debug for AnyObjError {
55    #[inline]
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        self.kind.fmt(f)
58    }
59}
60
61impl From<AccessError> for AnyObjError {
62    #[inline]
63    fn from(error: AccessError) -> Self {
64        Self::new(AnyObjErrorKind::AccessError(error))
65    }
66}
67
68/// Guard which decrements and releases shared storage for the guarded reference.
69struct AnyObjDecShared {
70    shared: NonNull<Shared>,
71}
72
73impl Drop for AnyObjDecShared {
74    fn drop(&mut self) {
75        // Safety: We know that the inner value is live in this instance.
76        unsafe {
77            Shared::dec(self.shared);
78        }
79    }
80}
81
82/// Guard which decrements and releases shared storage for the guarded reference.
83pub(crate) struct AnyObjDrop {
84    #[allow(unused)]
85    shared: NonNull<Shared>,
86}
87
88impl Drop for AnyObjDrop {
89    fn drop(&mut self) {
90        // Safety: We know that the inner value is live in this instance.
91        unsafe {
92            self.shared.as_ref().access.take();
93
94            Shared::dec(self.shared);
95        }
96    }
97}
98
99pub(crate) struct RawAnyObjGuard {
100    #[allow(unused)]
101    guard: RawAccessGuard,
102    #[allow(unused)]
103    dec_shared: AnyObjDecShared,
104}
105
106/// A type-erased wrapper for a reference, whether it is mutable or not.
107pub struct AnyObj {
108    shared: NonNull<Shared>,
109}
110
111impl AnyObj {
112    /// Construct an Any that wraps an owned object.
113    pub(crate) fn new<T>(data: T) -> alloc::Result<Self>
114    where
115        T: Any,
116    {
117        let vtable = &Vtable {
118            kind: Kind::Own,
119            type_id: TypeId::of::<T>,
120            debug: debug_ref_impl::<T>,
121            type_info: T::ANY_TYPE_INFO,
122            type_hash: T::HASH,
123            drop_value: const {
124                if needs_drop::<T>() {
125                    Some(drop_value::<T>)
126                } else {
127                    None
128                }
129            },
130            drop: drop_box::<ManuallyDrop<T>>,
131            clone: clone_own::<T>,
132        };
133
134        let shared = Shared {
135            access: Access::new(),
136            count: Cell::new(1),
137            vtable,
138            data,
139        };
140
141        let shared = NonNull::from(Box::leak(Box::try_new(shared)?)).cast();
142        Ok(Self { shared })
143    }
144
145    /// Construct an Any that wraps a pointer.
146    ///
147    /// # Safety
148    ///
149    /// Caller must ensure that the returned `AnyObj` doesn't outlive the
150    /// reference it is wrapping.
151    pub(crate) unsafe fn from_ref<T>(data: *const T) -> alloc::Result<Self>
152    where
153        T: Any,
154    {
155        let vtable = &Vtable {
156            kind: Kind::Ref,
157            type_id: TypeId::of::<T>,
158            debug: debug_ref_impl::<T>,
159            type_info: T::ANY_TYPE_INFO,
160            type_hash: T::HASH,
161            drop_value: None,
162            drop: drop_box::<NonNull<T>>,
163            clone: clone_ref::<T>,
164        };
165
166        let shared = Shared {
167            access: Access::new(),
168            count: Cell::new(1),
169            vtable,
170            data: NonNull::new_unchecked(data.cast_mut()),
171        };
172
173        let shared = NonNull::from(Box::leak(Box::try_new(shared)?)).cast();
174        Ok(Self { shared })
175    }
176
177    /// Construct an Any that wraps a mutable pointer.
178    ///
179    /// # Safety
180    ///
181    /// Caller must ensure that the returned `AnyObj` doesn't outlive the
182    /// reference it is wrapping.
183    pub(crate) unsafe fn from_mut<T>(data: *mut T) -> alloc::Result<Self>
184    where
185        T: Any,
186    {
187        let vtable = &Vtable {
188            kind: Kind::Mut,
189            type_id: TypeId::of::<T>,
190            debug: debug_mut_impl::<T>,
191            type_info: T::ANY_TYPE_INFO,
192            type_hash: T::HASH,
193            drop_value: None,
194            drop: drop_box::<NonNull<T>>,
195            clone: clone_mut::<T>,
196        };
197
198        let shared = Shared {
199            access: Access::new(),
200            count: Cell::new(1),
201            vtable,
202            data: NonNull::new_unchecked(data),
203        };
204
205        let shared = NonNull::from(Box::leak(Box::try_new(shared)?)).cast();
206        Ok(Self { shared })
207    }
208
209    /// Downcast into an owned value of type `T`.
210    pub(crate) fn downcast<T>(self) -> Result<T, AnyObjError>
211    where
212        T: Any,
213    {
214        let vtable = vtable(&self);
215
216        if (vtable.type_id)() != TypeId::of::<T>() {
217            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
218                T::ANY_TYPE_INFO,
219                vtable.type_info(),
220            )));
221        }
222
223        if !matches!(vtable.kind, Kind::Own) {
224            return Err(AnyObjError::from(AccessError::not_owned(
225                vtable.type_info(),
226            )));
227        }
228
229        // SAFETY: We've checked for the appropriate type just above.
230        unsafe {
231            self.shared.as_ref().access.try_take()?;
232            let data = vtable.as_ptr::<T>(self.shared);
233            Ok(data.read())
234        }
235    }
236
237    /// Take the interior value and drop it if necessary.
238    pub(crate) fn drop(self) -> Result<(), AccessError> {
239        let vtable = vtable(&self);
240
241        if !matches!(vtable.kind, Kind::Own) {
242            return Err(AccessError::not_owned(vtable.type_info()));
243        }
244
245        // SAFETY: We've checked for the appropriate type just above.
246        unsafe {
247            self.shared.as_ref().access.try_take()?;
248
249            if let Some(drop_value) = vtable.drop_value {
250                drop_value(self.shared);
251            }
252
253            Ok(())
254        }
255    }
256
257    /// Take the interior value and return a handle to the taken value.
258    pub(crate) fn take(self) -> Result<Self, VmErrorKind> {
259        let vtable = vtable(&self);
260
261        if !matches!(vtable.kind, Kind::Own) {
262            return Err(VmErrorKind::from(AccessError::not_owned(
263                vtable.type_info(),
264            )));
265        }
266
267        // SAFETY: We've checked for the appropriate type just above.
268        unsafe {
269            self.shared.as_ref().access.try_take()?;
270            Ok((vtable.clone)(self.shared)?)
271        }
272    }
273
274    /// Downcast into an owned value of type [`Ref<T>`].
275    ///
276    /// # Errors
277    ///
278    /// This errors in case the underlying value is not owned, non-owned
279    /// references cannot be coerced into [`Ref<T>`].
280    pub(crate) fn into_ref<T>(self) -> Result<Ref<T>, AnyObjError>
281    where
282        T: Any,
283    {
284        let vtable = vtable(&self);
285
286        if (vtable.type_id)() != TypeId::of::<T>() {
287            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
288                T::ANY_TYPE_INFO,
289                vtable.type_info(),
290            )));
291        }
292
293        if !matches!(vtable.kind, Kind::Own) {
294            return Err(AnyObjError::from(AccessError::not_owned(
295                vtable.type_info(),
296            )));
297        }
298
299        // SAFETY: We've checked for the appropriate type just above.
300        unsafe {
301            self.shared.as_ref().access.try_shared()?;
302            let this = ManuallyDrop::new(self);
303            let data = vtable.as_ptr(this.shared);
304
305            let vtable = &RefVtable {
306                drop: |shared: NonNull<()>| {
307                    let shared = shared.cast::<Shared>();
308                    shared.as_ref().access.release();
309                    Shared::dec(shared)
310                },
311            };
312
313            let guard = RawAnyGuard::new(this.shared.cast(), vtable);
314            Ok(Ref::new(data, guard))
315        }
316    }
317
318    /// Downcast into an owned value of type [`Mut<T>`].
319    ///
320    /// # Errors
321    ///
322    /// This errors in case the underlying value is not owned, non-owned
323    /// references cannot be coerced into [`Mut<T>`].
324    pub(crate) fn into_mut<T>(self) -> Result<Mut<T>, AnyObjError>
325    where
326        T: Any,
327    {
328        let vtable = vtable(&self);
329
330        if (vtable.type_id)() != TypeId::of::<T>() {
331            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
332                T::ANY_TYPE_INFO,
333                vtable.type_info(),
334            )));
335        }
336
337        if !matches!(vtable.kind, Kind::Own) {
338            return Err(AnyObjError::from(AccessError::not_owned(
339                vtable.type_info(),
340            )));
341        }
342
343        // SAFETY: We've checked for the appropriate type just above.
344        unsafe {
345            self.shared.as_ref().access.try_exclusive()?;
346            let this = ManuallyDrop::new(self);
347            let data = vtable.as_ptr(this.shared);
348
349            let vtable = &RefVtable {
350                drop: |shared: NonNull<()>| {
351                    let shared = shared.cast::<Shared>();
352                    shared.as_ref().access.release();
353                    Shared::dec(shared)
354                },
355            };
356
357            let guard = RawAnyGuard::new(this.shared.cast(), vtable);
358            Ok(Mut::new(data, guard))
359        }
360    }
361
362    /// Get a reference to the interior value while checking for shared access.
363    ///
364    /// This prevents other exclusive accesses from being performed while the
365    /// guard returned from this function is live.
366    pub fn borrow_ref<T>(&self) -> Result<BorrowRef<'_, T>, AnyObjError>
367    where
368        T: Any,
369    {
370        let vtable = vtable(self);
371
372        if (vtable.type_id)() != TypeId::of::<T>() {
373            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
374                T::ANY_TYPE_INFO,
375                vtable.type_info(),
376            )));
377        }
378
379        // SAFETY: We've checked for the appropriate type just above.
380        unsafe {
381            let guard = self.shared.as_ref().access.shared()?;
382            let data = vtable.as_ptr(self.shared);
383            Ok(BorrowRef::new(data, guard.into_raw()))
384        }
385    }
386
387    /// Try to borrow a reference to the interior value while checking for
388    /// shared access.
389    ///
390    /// Returns `None` if the interior type is not `T`.
391    ///
392    /// This prevents other exclusive accesses from being performed while the
393    /// guard returned from this function is alive.
394    pub fn try_borrow_ref<T>(&self) -> Result<Option<BorrowRef<'_, T>>, AccessError>
395    where
396        T: Any,
397    {
398        let vtable = vtable(self);
399
400        if (vtable.type_id)() != TypeId::of::<T>() {
401            return Ok(None);
402        }
403
404        // SAFETY: We've checked for the appropriate type just above.
405        unsafe {
406            let guard = self.shared.as_ref().access.shared()?;
407            let data = vtable.as_ptr(self.shared);
408            Ok(Some(BorrowRef::new(data, guard.into_raw())))
409        }
410    }
411
412    /// Try to borrow a reference to the interior value while checking for
413    /// exclusive access.
414    ///
415    /// Returns `None` if the interior type is not `T`.
416    ///
417    /// This prevents other exclusive accesses from being performed while the
418    /// guard returned from this function is alive.
419    pub fn try_borrow_mut<T>(&self) -> Result<Option<BorrowMut<'_, T>>, AccessError>
420    where
421        T: Any,
422    {
423        let vtable = vtable(self);
424
425        if (vtable.type_id)() != TypeId::of::<T>() {
426            return Ok(None);
427        }
428
429        // SAFETY: We've checked for the appropriate type just above.
430        unsafe {
431            let guard = self.shared.as_ref().access.exclusive()?;
432            let data = vtable.as_ptr(self.shared);
433            Ok(Some(BorrowMut::new(data, guard.into_raw())))
434        }
435    }
436
437    /// Returns some mutable reference to the boxed value if it is of type `T`.
438    pub fn borrow_mut<T>(&self) -> Result<BorrowMut<'_, T>, AnyObjError>
439    where
440        T: Any,
441    {
442        let vtable = vtable(self);
443
444        if (vtable.type_id)() != TypeId::of::<T>() {
445            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
446                T::ANY_TYPE_INFO,
447                vtable.type_info(),
448            )));
449        }
450
451        if matches!(vtable.kind, Kind::Ref) {
452            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
453                T::ANY_TYPE_INFO,
454                vtable.type_info(),
455            )));
456        }
457
458        // SAFETY: We've checked for the appropriate type just above.
459        unsafe {
460            let guard = self.shared.as_ref().access.exclusive()?;
461            let data = vtable.as_ptr(self.shared);
462            Ok(BorrowMut::new(data, guard.into_raw()))
463        }
464    }
465
466    /// Get a reference to the interior value while checking for shared access.
467    ///
468    /// This prevents other exclusive accesses from being performed while the
469    /// guard returned from this function is live.
470    pub(crate) fn borrow_ref_ptr<T>(self) -> Result<(NonNull<T>, RawAnyObjGuard), AnyObjError>
471    where
472        T: Any,
473    {
474        let vtable = vtable(&self);
475
476        if (vtable.type_id)() != TypeId::of::<T>() {
477            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
478                T::ANY_TYPE_INFO,
479                vtable.type_info(),
480            )));
481        }
482
483        // SAFETY: We've checked for the appropriate type just above.
484        unsafe {
485            let guard = self.shared.as_ref().access.shared()?.into_raw();
486            let this = ManuallyDrop::new(self);
487
488            let data = vtable.as_ptr(this.shared);
489
490            let guard = RawAnyObjGuard {
491                guard,
492                dec_shared: AnyObjDecShared {
493                    shared: this.shared,
494                },
495            };
496
497            Ok((data, guard))
498        }
499    }
500
501    /// Returns some mutable reference to the boxed value if it is of type `T`.
502    pub(crate) fn borrow_mut_ptr<T>(self) -> Result<(NonNull<T>, RawAnyObjGuard), AnyObjError>
503    where
504        T: Any,
505    {
506        let vtable = vtable(&self);
507
508        if (vtable.type_id)() != TypeId::of::<T>() {
509            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
510                T::ANY_TYPE_INFO,
511                vtable.type_info(),
512            )));
513        }
514
515        if matches!(vtable.kind, Kind::Ref) {
516            return Err(AnyObjError::new(AnyObjErrorKind::Cast(
517                T::ANY_TYPE_INFO,
518                vtable.type_info(),
519            )));
520        }
521
522        // SAFETY: We've checked for the appropriate type just above.
523        unsafe {
524            let guard = self.shared.as_ref().access.exclusive()?.into_raw();
525            let this = ManuallyDrop::new(self);
526
527            let data = vtable.as_ptr(this.shared);
528
529            let guard = RawAnyObjGuard {
530                guard,
531                dec_shared: AnyObjDecShared {
532                    shared: this.shared,
533                },
534            };
535
536            Ok((data, guard))
537        }
538    }
539
540    /// Deconstruct the shared value into a guard and shared box.
541    ///
542    /// # Safety
543    ///
544    /// The content of the shared value will be forcibly destructed once the
545    /// returned guard is dropped, unchecked use of the shared value after this
546    /// point will lead to undefined behavior.
547    pub(crate) unsafe fn into_drop_guard(self) -> (Self, AnyObjDrop) {
548        // Increment the reference count by one to account for the guard holding
549        // onto it.
550        Shared::inc(self.shared);
551
552        let guard = AnyObjDrop {
553            shared: self.shared,
554        };
555
556        (self, guard)
557    }
558
559    /// Test if the value is sharable.
560    pub(crate) fn is_readable(&self) -> bool {
561        // Safety: Since we have a reference to this shared, we know that the
562        // inner is available.
563        unsafe { self.shared.as_ref().access.is_shared() }
564    }
565
566    /// Test if the value is exclusively accessible.
567    pub(crate) fn is_writable(&self) -> bool {
568        unsafe {
569            let shared = self.shared.as_ref();
570            !matches!(shared.vtable.kind, Kind::Ref) && shared.access.is_exclusive()
571        }
572    }
573
574    /// Get access snapshot of shared value.
575    pub(crate) fn snapshot(&self) -> Snapshot {
576        unsafe { self.shared.as_ref().access.snapshot() }
577    }
578
579    /// Debug format the current any type.
580    pub(crate) fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
581        (vtable(self).debug)(f)
582    }
583
584    /// Access the underlying type id for the data.
585    pub(crate) fn type_hash(&self) -> Hash {
586        vtable(self).type_hash
587    }
588
589    /// Access full type info for type.
590    pub(crate) fn type_info(&self) -> TypeInfo {
591        TypeInfo::any_type_info(vtable(self).type_info)
592    }
593}
594
595impl Clone for AnyObj {
596    #[inline]
597    fn clone(&self) -> Self {
598        // SAFETY: We know that the inner value is live in this instance.
599        unsafe {
600            Shared::inc(self.shared);
601        }
602
603        Self {
604            shared: self.shared,
605        }
606    }
607
608    #[inline]
609    fn clone_from(&mut self, source: &Self) {
610        if ptr::eq(self.shared.as_ptr(), source.shared.as_ptr()) {
611            return;
612        }
613
614        let old = replace(&mut self.shared, source.shared);
615
616        // SAFETY: We know that the inner value is live in both instances.
617        unsafe {
618            Shared::dec(old);
619            Shared::inc(self.shared);
620        }
621    }
622}
623
624impl fmt::Debug for AnyObj {
625    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
626        self.debug(f)
627    }
628}
629
630impl Drop for AnyObj {
631    fn drop(&mut self) {
632        // Safety: We know that the inner value is live in this instance.
633        unsafe {
634            Shared::dec(self.shared);
635        }
636    }
637}
638
639/// The signature of a pointer coercion function.
640type TypeIdFn = fn() -> TypeId;
641
642/// The signature of a descriptive type name function.
643type DebugFn = fn(&mut fmt::Formatter<'_>) -> fmt::Result;
644
645/// The kind of the stored value in the `AnyObj`.
646enum Kind {
647    /// Underlying access is shared.
648    Ref,
649    /// Underlying access is exclusive.
650    Mut,
651    /// Underlying access is owned.
652    Own,
653}
654
655struct Vtable {
656    /// The statically known kind of reference being stored.
657    kind: Kind,
658    /// Punt the inner pointer to the type corresponding to the type hash.
659    type_id: TypeIdFn,
660    /// Type information for diagnostics.
661    debug: DebugFn,
662    /// Type information.
663    type_info: AnyTypeInfo,
664    /// Type hash of the interior type.
665    type_hash: Hash,
666    /// Value drop implementation. Set to `None` if the underlying value does
667    /// not need to be dropped.
668    drop_value: Option<unsafe fn(NonNull<Shared>)>,
669    /// Only drop the box implementation.
670    drop: unsafe fn(NonNull<Shared>),
671    /// Clone the literal content of the shared value.
672    clone: unsafe fn(NonNull<Shared>) -> alloc::Result<AnyObj>,
673}
674
675impl Vtable {
676    #[inline]
677    fn type_info(&self) -> TypeInfo {
678        TypeInfo::any_type_info(self.type_info)
679    }
680
681    fn as_ptr<T>(&self, base: NonNull<Shared>) -> NonNull<T> {
682        if matches!(self.kind, Kind::Own) {
683            unsafe { base.byte_add(offset_of!(Shared<T>, data)).cast() }
684        } else {
685            unsafe {
686                base.byte_add(offset_of!(Shared<NonNull<T>>, data))
687                    .cast()
688                    .read()
689            }
690        }
691    }
692}
693
694#[repr(C)]
695struct Shared<T = ()> {
696    /// The currently handed out access to the shared data.
697    access: Access,
698    /// The number of strong references to the shared data.
699    count: Cell<usize>,
700    /// Vtable of the shared value.
701    vtable: &'static Vtable,
702    /// Data of the shared reference.
703    data: T,
704}
705
706impl Shared {
707    /// Increment the reference count of the inner value.
708    #[inline]
709    unsafe fn inc(this: NonNull<Self>) {
710        let count_ref = &*addr_of!((*this.as_ptr()).count);
711        let count = count_ref.get();
712
713        debug_assert_ne!(
714            count, 0,
715            "Reference count of zero should only happen if Shared is incorrectly implemented"
716        );
717
718        if count == usize::MAX {
719            crate::alloc::abort();
720        }
721
722        count_ref.set(count + 1);
723    }
724
725    /// Decrement the reference count in inner, and free the underlying data if
726    /// it has reached zero.
727    ///
728    /// # Safety
729    ///
730    /// ProtocolCaller needs to ensure that `this` is a valid pointer.
731    #[inline]
732    unsafe fn dec(this: NonNull<Self>) {
733        let count_ref = &*addr_of!((*this.as_ptr()).count);
734        let count = count_ref.get();
735
736        debug_assert_ne!(
737            count, 0,
738            "Reference count of zero should only happen if Shared is incorrectly implemented"
739        );
740
741        let count = count - 1;
742        count_ref.set(count);
743
744        if count == 0 {
745            let vtable = *addr_of!((*this.as_ptr()).vtable);
746
747            if let Some(drop_value) = vtable.drop_value {
748                let access = &*addr_of!((*this.as_ptr()).access);
749
750                if !access.is_taken() {
751                    drop_value(this);
752                }
753            }
754
755            (vtable.drop)(this);
756        }
757    }
758}
759
760fn debug_ref_impl<T>(f: &mut fmt::Formatter<'_>) -> fmt::Result
761where
762    T: ?Sized + Any,
763{
764    write!(f, "&{}", T::ITEM)
765}
766
767fn debug_mut_impl<T>(f: &mut fmt::Formatter<'_>) -> fmt::Result
768where
769    T: ?Sized + Any,
770{
771    write!(f, "&mut {}", T::ITEM)
772}
773
774unsafe fn drop_value<T>(this: NonNull<Shared>) {
775    let data = addr_of_mut!((*this.cast::<Shared<T>>().as_ptr()).data);
776    drop_in_place(data);
777}
778
779unsafe fn drop_box<T>(this: NonNull<Shared>) {
780    drop(Box::from_raw_in(this.cast::<Shared<T>>().as_ptr(), Global))
781}
782
783unsafe fn clone_own<T>(this: NonNull<Shared>) -> alloc::Result<AnyObj>
784where
785    T: Any,
786{
787    // NB: We read the value without deallocating it from the previous location,
788    // since that would cause the returned value to be invalid.
789    let value = addr_of_mut!((*this.cast::<Shared<T>>().as_ptr()).data).read();
790    AnyObj::new(value)
791}
792
793unsafe fn clone_ref<T>(this: NonNull<Shared>) -> alloc::Result<AnyObj>
794where
795    T: Any,
796{
797    let value = addr_of_mut!((*this.cast::<Shared<NonNull<T>>>().as_ptr()).data).read();
798    AnyObj::from_ref(value.as_ptr().cast_const())
799}
800
801unsafe fn clone_mut<T>(this: NonNull<Shared>) -> alloc::Result<AnyObj>
802where
803    T: Any,
804{
805    let value = addr_of_mut!((*this.cast::<Shared<NonNull<T>>>().as_ptr()).data).read();
806    AnyObj::from_mut(value.as_ptr())
807}
808
809#[inline]
810fn vtable(any: &AnyObj) -> &'static Vtable {
811    unsafe { addr_of!((*any.shared.as_ptr()).vtable).read() }
812}