xref: /openbmc/linux/rust/kernel/sync/arc.rs (revision 92a655ae00a2f15fdd80eb96cd23526c0d8f0bfd)
19dc04365SWedson Almeida Filho // SPDX-License-Identifier: GPL-2.0
29dc04365SWedson Almeida Filho 
39dc04365SWedson Almeida Filho //! A reference-counted pointer.
49dc04365SWedson Almeida Filho //!
59dc04365SWedson Almeida Filho //! This module implements a way for users to create reference-counted objects and pointers to
69dc04365SWedson Almeida Filho //! them. Such a pointer automatically increments and decrements the count, and drops the
79dc04365SWedson Almeida Filho //! underlying object when it reaches zero. It is also safe to use concurrently from multiple
89dc04365SWedson Almeida Filho //! threads.
99dc04365SWedson Almeida Filho //!
109dc04365SWedson Almeida Filho //! It is different from the standard library's [`Arc`] in a few ways:
119dc04365SWedson Almeida Filho //! 1. It is backed by the kernel's `refcount_t` type.
129dc04365SWedson Almeida Filho //! 2. It does not support weak references, which allows it to be half the size.
139dc04365SWedson Almeida Filho //! 3. It saturates the reference count instead of aborting when it goes over a threshold.
149dc04365SWedson Almeida Filho //! 4. It does not provide a `get_mut` method, so the ref counted object is pinned.
159dc04365SWedson Almeida Filho //!
169dc04365SWedson Almeida Filho //! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
179dc04365SWedson Almeida Filho 
189dc04365SWedson Almeida Filho use crate::{bindings, error::Result, types::Opaque};
199dc04365SWedson Almeida Filho use alloc::boxed::Box;
20f75cb6fcSWedson Almeida Filho use core::{
21f75cb6fcSWedson Almeida Filho     marker::{PhantomData, Unsize},
2217f67160SWedson Almeida Filho     mem::ManuallyDrop,
23f75cb6fcSWedson Almeida Filho     ops::Deref,
24f75cb6fcSWedson Almeida Filho     ptr::NonNull,
25f75cb6fcSWedson Almeida Filho };
269dc04365SWedson Almeida Filho 
279dc04365SWedson Almeida Filho /// A reference-counted pointer to an instance of `T`.
289dc04365SWedson Almeida Filho ///
299dc04365SWedson Almeida Filho /// The reference count is incremented when new instances of [`Arc`] are created, and decremented
309dc04365SWedson Almeida Filho /// when they are dropped. When the count reaches zero, the underlying `T` is also dropped.
319dc04365SWedson Almeida Filho ///
329dc04365SWedson Almeida Filho /// # Invariants
339dc04365SWedson Almeida Filho ///
349dc04365SWedson Almeida Filho /// The reference count on an instance of [`Arc`] is always non-zero.
359dc04365SWedson Almeida Filho /// The object pointed to by [`Arc`] is always pinned.
369dc04365SWedson Almeida Filho ///
379dc04365SWedson Almeida Filho /// # Examples
389dc04365SWedson Almeida Filho ///
399dc04365SWedson Almeida Filho /// ```
409dc04365SWedson Almeida Filho /// use kernel::sync::Arc;
419dc04365SWedson Almeida Filho ///
429dc04365SWedson Almeida Filho /// struct Example {
439dc04365SWedson Almeida Filho ///     a: u32,
449dc04365SWedson Almeida Filho ///     b: u32,
459dc04365SWedson Almeida Filho /// }
469dc04365SWedson Almeida Filho ///
479dc04365SWedson Almeida Filho /// // Create a ref-counted instance of `Example`.
489dc04365SWedson Almeida Filho /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
499dc04365SWedson Almeida Filho ///
509dc04365SWedson Almeida Filho /// // Get a new pointer to `obj` and increment the refcount.
519dc04365SWedson Almeida Filho /// let cloned = obj.clone();
529dc04365SWedson Almeida Filho ///
539dc04365SWedson Almeida Filho /// // Assert that both `obj` and `cloned` point to the same underlying object.
549dc04365SWedson Almeida Filho /// assert!(core::ptr::eq(&*obj, &*cloned));
559dc04365SWedson Almeida Filho ///
569dc04365SWedson Almeida Filho /// // Destroy `obj` and decrement its refcount.
579dc04365SWedson Almeida Filho /// drop(obj);
589dc04365SWedson Almeida Filho ///
599dc04365SWedson Almeida Filho /// // Check that the values are still accessible through `cloned`.
609dc04365SWedson Almeida Filho /// assert_eq!(cloned.a, 10);
619dc04365SWedson Almeida Filho /// assert_eq!(cloned.b, 20);
629dc04365SWedson Almeida Filho ///
639dc04365SWedson Almeida Filho /// // The refcount drops to zero when `cloned` goes out of scope, and the memory is freed.
649dc04365SWedson Almeida Filho /// ```
6553528772SWedson Almeida Filho ///
6653528772SWedson Almeida Filho /// Using `Arc<T>` as the type of `self`:
6753528772SWedson Almeida Filho ///
6853528772SWedson Almeida Filho /// ```
6953528772SWedson Almeida Filho /// use kernel::sync::Arc;
7053528772SWedson Almeida Filho ///
7153528772SWedson Almeida Filho /// struct Example {
7253528772SWedson Almeida Filho ///     a: u32,
7353528772SWedson Almeida Filho ///     b: u32,
7453528772SWedson Almeida Filho /// }
7553528772SWedson Almeida Filho ///
7653528772SWedson Almeida Filho /// impl Example {
7753528772SWedson Almeida Filho ///     fn take_over(self: Arc<Self>) {
7853528772SWedson Almeida Filho ///         // ...
7953528772SWedson Almeida Filho ///     }
8053528772SWedson Almeida Filho ///
8153528772SWedson Almeida Filho ///     fn use_reference(self: &Arc<Self>) {
8253528772SWedson Almeida Filho ///         // ...
8353528772SWedson Almeida Filho ///     }
8453528772SWedson Almeida Filho /// }
8553528772SWedson Almeida Filho ///
8653528772SWedson Almeida Filho /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
8753528772SWedson Almeida Filho /// obj.use_reference();
8853528772SWedson Almeida Filho /// obj.take_over();
8953528772SWedson Almeida Filho /// ```
90f75cb6fcSWedson Almeida Filho ///
91f75cb6fcSWedson Almeida Filho /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`:
92f75cb6fcSWedson Almeida Filho ///
93f75cb6fcSWedson Almeida Filho /// ```
94f75cb6fcSWedson Almeida Filho /// use kernel::sync::Arc;
95f75cb6fcSWedson Almeida Filho ///
96f75cb6fcSWedson Almeida Filho /// trait MyTrait {}
97f75cb6fcSWedson Almeida Filho ///
98f75cb6fcSWedson Almeida Filho /// struct Example;
99f75cb6fcSWedson Almeida Filho /// impl MyTrait for Example {}
100f75cb6fcSWedson Almeida Filho ///
101f75cb6fcSWedson Almeida Filho /// // `obj` has type `Arc<Example>`.
102f75cb6fcSWedson Almeida Filho /// let obj: Arc<Example> = Arc::try_new(Example)?;
103f75cb6fcSWedson Almeida Filho ///
104f75cb6fcSWedson Almeida Filho /// // `coerced` has type `Arc<dyn MyTrait>`.
105f75cb6fcSWedson Almeida Filho /// let coerced: Arc<dyn MyTrait> = obj;
106f75cb6fcSWedson Almeida Filho /// ```
1079dc04365SWedson Almeida Filho pub struct Arc<T: ?Sized> {
1089dc04365SWedson Almeida Filho     ptr: NonNull<ArcInner<T>>,
1099dc04365SWedson Almeida Filho     _p: PhantomData<ArcInner<T>>,
1109dc04365SWedson Almeida Filho }
1119dc04365SWedson Almeida Filho 
1129dc04365SWedson Almeida Filho #[repr(C)]
1139dc04365SWedson Almeida Filho struct ArcInner<T: ?Sized> {
1149dc04365SWedson Almeida Filho     refcount: Opaque<bindings::refcount_t>,
1159dc04365SWedson Almeida Filho     data: T,
1169dc04365SWedson Almeida Filho }
1179dc04365SWedson Almeida Filho 
11853528772SWedson Almeida Filho // This is to allow [`Arc`] (and variants) to be used as the type of `self`.
11953528772SWedson Almeida Filho impl<T: ?Sized> core::ops::Receiver for Arc<T> {}
12053528772SWedson Almeida Filho 
121f75cb6fcSWedson Almeida Filho // This is to allow coercion from `Arc<T>` to `Arc<U>` if `T` can be converted to the
122f75cb6fcSWedson Almeida Filho // dynamically-sized type (DST) `U`.
123f75cb6fcSWedson Almeida Filho impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
124f75cb6fcSWedson Almeida Filho 
1259dc04365SWedson Almeida Filho // SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
1269dc04365SWedson Almeida Filho // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
1279dc04365SWedson Almeida Filho // `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` directly, for
1289dc04365SWedson Almeida Filho // example, when the reference count reaches zero and `T` is dropped.
1299dc04365SWedson Almeida Filho unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
1309dc04365SWedson Almeida Filho 
1319dc04365SWedson Almeida Filho // SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync` for the
1329dc04365SWedson Almeida Filho // same reason as above. `T` needs to be `Send` as well because a thread can clone an `&Arc<T>`
1339dc04365SWedson Almeida Filho // into an `Arc<T>`, which may lead to `T` being accessed by the same reasoning as above.
1349dc04365SWedson Almeida Filho unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
1359dc04365SWedson Almeida Filho 
1369dc04365SWedson Almeida Filho impl<T> Arc<T> {
1379dc04365SWedson Almeida Filho     /// Constructs a new reference counted instance of `T`.
1389dc04365SWedson Almeida Filho     pub fn try_new(contents: T) -> Result<Self> {
1399dc04365SWedson Almeida Filho         // INVARIANT: The refcount is initialised to a non-zero value.
1409dc04365SWedson Almeida Filho         let value = ArcInner {
1419dc04365SWedson Almeida Filho             // SAFETY: There are no safety requirements for this FFI call.
1429dc04365SWedson Almeida Filho             refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
1439dc04365SWedson Almeida Filho             data: contents,
1449dc04365SWedson Almeida Filho         };
1459dc04365SWedson Almeida Filho 
1469dc04365SWedson Almeida Filho         let inner = Box::try_new(value)?;
1479dc04365SWedson Almeida Filho 
1489dc04365SWedson Almeida Filho         // SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
1499dc04365SWedson Almeida Filho         // `Arc` object.
1509dc04365SWedson Almeida Filho         Ok(unsafe { Self::from_inner(Box::leak(inner).into()) })
1519dc04365SWedson Almeida Filho     }
1529dc04365SWedson Almeida Filho }
1539dc04365SWedson Almeida Filho 
1549dc04365SWedson Almeida Filho impl<T: ?Sized> Arc<T> {
1559dc04365SWedson Almeida Filho     /// Constructs a new [`Arc`] from an existing [`ArcInner`].
1569dc04365SWedson Almeida Filho     ///
1579dc04365SWedson Almeida Filho     /// # Safety
1589dc04365SWedson Almeida Filho     ///
1599dc04365SWedson Almeida Filho     /// The caller must ensure that `inner` points to a valid location and has a non-zero reference
1609dc04365SWedson Almeida Filho     /// count, one of which will be owned by the new [`Arc`] instance.
1619dc04365SWedson Almeida Filho     unsafe fn from_inner(inner: NonNull<ArcInner<T>>) -> Self {
1629dc04365SWedson Almeida Filho         // INVARIANT: By the safety requirements, the invariants hold.
1639dc04365SWedson Almeida Filho         Arc {
1649dc04365SWedson Almeida Filho             ptr: inner,
1659dc04365SWedson Almeida Filho             _p: PhantomData,
1669dc04365SWedson Almeida Filho         }
1679dc04365SWedson Almeida Filho     }
16817f67160SWedson Almeida Filho 
16917f67160SWedson Almeida Filho     /// Returns an [`ArcBorrow`] from the given [`Arc`].
17017f67160SWedson Almeida Filho     ///
17117f67160SWedson Almeida Filho     /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
17217f67160SWedson Almeida Filho     /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
17317f67160SWedson Almeida Filho     #[inline]
17417f67160SWedson Almeida Filho     pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
17517f67160SWedson Almeida Filho         // SAFETY: The constraint that the lifetime of the shared reference must outlive that of
17617f67160SWedson Almeida Filho         // the returned `ArcBorrow` ensures that the object remains alive and that no mutable
17717f67160SWedson Almeida Filho         // reference can be created.
17817f67160SWedson Almeida Filho         unsafe { ArcBorrow::new(self.ptr) }
17917f67160SWedson Almeida Filho     }
1809dc04365SWedson Almeida Filho }
1819dc04365SWedson Almeida Filho 
1829dc04365SWedson Almeida Filho impl<T: ?Sized> Deref for Arc<T> {
1839dc04365SWedson Almeida Filho     type Target = T;
1849dc04365SWedson Almeida Filho 
1859dc04365SWedson Almeida Filho     fn deref(&self) -> &Self::Target {
1869dc04365SWedson Almeida Filho         // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
1879dc04365SWedson Almeida Filho         // safe to dereference it.
1889dc04365SWedson Almeida Filho         unsafe { &self.ptr.as_ref().data }
1899dc04365SWedson Almeida Filho     }
1909dc04365SWedson Almeida Filho }
1919dc04365SWedson Almeida Filho 
1929dc04365SWedson Almeida Filho impl<T: ?Sized> Clone for Arc<T> {
1939dc04365SWedson Almeida Filho     fn clone(&self) -> Self {
1949dc04365SWedson Almeida Filho         // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
1959dc04365SWedson Almeida Filho         // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
1969dc04365SWedson Almeida Filho         // safe to increment the refcount.
1979dc04365SWedson Almeida Filho         unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
1989dc04365SWedson Almeida Filho 
1999dc04365SWedson Almeida Filho         // SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
2009dc04365SWedson Almeida Filho         unsafe { Self::from_inner(self.ptr) }
2019dc04365SWedson Almeida Filho     }
2029dc04365SWedson Almeida Filho }
2039dc04365SWedson Almeida Filho 
2049dc04365SWedson Almeida Filho impl<T: ?Sized> Drop for Arc<T> {
2059dc04365SWedson Almeida Filho     fn drop(&mut self) {
2069dc04365SWedson Almeida Filho         // SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot
2079dc04365SWedson Almeida Filho         // touch `refcount` after it's decremented to a non-zero value because another thread/CPU
2089dc04365SWedson Almeida Filho         // may concurrently decrement it to zero and free it. It is ok to have a raw pointer to
2099dc04365SWedson Almeida Filho         // freed/invalid memory as long as it is never dereferenced.
2109dc04365SWedson Almeida Filho         let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
2119dc04365SWedson Almeida Filho 
2129dc04365SWedson Almeida Filho         // INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and
2139dc04365SWedson Almeida Filho         // this instance is being dropped, so the broken invariant is not observable.
2149dc04365SWedson Almeida Filho         // SAFETY: Also by the type invariant, we are allowed to decrement the refcount.
2159dc04365SWedson Almeida Filho         let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
2169dc04365SWedson Almeida Filho         if is_zero {
2179dc04365SWedson Almeida Filho             // The count reached zero, we must free the memory.
2189dc04365SWedson Almeida Filho             //
2199dc04365SWedson Almeida Filho             // SAFETY: The pointer was initialised from the result of `Box::leak`.
2209dc04365SWedson Almeida Filho             unsafe { Box::from_raw(self.ptr.as_ptr()) };
2219dc04365SWedson Almeida Filho         }
2229dc04365SWedson Almeida Filho     }
2239dc04365SWedson Almeida Filho }
22417f67160SWedson Almeida Filho 
22517f67160SWedson Almeida Filho /// A borrowed reference to an [`Arc`] instance.
22617f67160SWedson Almeida Filho ///
22717f67160SWedson Almeida Filho /// For cases when one doesn't ever need to increment the refcount on the allocation, it is simpler
22817f67160SWedson Almeida Filho /// to use just `&T`, which we can trivially get from an `Arc<T>` instance.
22917f67160SWedson Almeida Filho ///
23017f67160SWedson Almeida Filho /// However, when one may need to increment the refcount, it is preferable to use an `ArcBorrow<T>`
23117f67160SWedson Almeida Filho /// over `&Arc<T>` because the latter results in a double-indirection: a pointer (shared reference)
23217f67160SWedson Almeida Filho /// to a pointer (`Arc<T>`) to the object (`T`). An [`ArcBorrow`] eliminates this double
23317f67160SWedson Almeida Filho /// indirection while still allowing one to increment the refcount and getting an `Arc<T>` when/if
23417f67160SWedson Almeida Filho /// needed.
23517f67160SWedson Almeida Filho ///
23617f67160SWedson Almeida Filho /// # Invariants
23717f67160SWedson Almeida Filho ///
23817f67160SWedson Almeida Filho /// There are no mutable references to the underlying [`Arc`], and it remains valid for the
23917f67160SWedson Almeida Filho /// lifetime of the [`ArcBorrow`] instance.
24017f67160SWedson Almeida Filho ///
24117f67160SWedson Almeida Filho /// # Example
24217f67160SWedson Almeida Filho ///
24317f67160SWedson Almeida Filho /// ```
24417f67160SWedson Almeida Filho /// use crate::sync::{Arc, ArcBorrow};
24517f67160SWedson Almeida Filho ///
24617f67160SWedson Almeida Filho /// struct Example;
24717f67160SWedson Almeida Filho ///
24817f67160SWedson Almeida Filho /// fn do_something(e: ArcBorrow<'_, Example>) -> Arc<Example> {
24917f67160SWedson Almeida Filho ///     e.into()
25017f67160SWedson Almeida Filho /// }
25117f67160SWedson Almeida Filho ///
25217f67160SWedson Almeida Filho /// let obj = Arc::try_new(Example)?;
25317f67160SWedson Almeida Filho /// let cloned = do_something(obj.as_arc_borrow());
25417f67160SWedson Almeida Filho ///
25517f67160SWedson Almeida Filho /// // Assert that both `obj` and `cloned` point to the same underlying object.
25617f67160SWedson Almeida Filho /// assert!(core::ptr::eq(&*obj, &*cloned));
25717f67160SWedson Almeida Filho /// ```
258*92a655aeSWedson Almeida Filho ///
259*92a655aeSWedson Almeida Filho /// Using `ArcBorrow<T>` as the type of `self`:
260*92a655aeSWedson Almeida Filho ///
261*92a655aeSWedson Almeida Filho /// ```
262*92a655aeSWedson Almeida Filho /// use crate::sync::{Arc, ArcBorrow};
263*92a655aeSWedson Almeida Filho ///
264*92a655aeSWedson Almeida Filho /// struct Example {
265*92a655aeSWedson Almeida Filho ///     a: u32,
266*92a655aeSWedson Almeida Filho ///     b: u32,
267*92a655aeSWedson Almeida Filho /// }
268*92a655aeSWedson Almeida Filho ///
269*92a655aeSWedson Almeida Filho /// impl Example {
270*92a655aeSWedson Almeida Filho ///     fn use_reference(self: ArcBorrow<'_, Self>) {
271*92a655aeSWedson Almeida Filho ///         // ...
272*92a655aeSWedson Almeida Filho ///     }
273*92a655aeSWedson Almeida Filho /// }
274*92a655aeSWedson Almeida Filho ///
275*92a655aeSWedson Almeida Filho /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
276*92a655aeSWedson Almeida Filho /// obj.as_arc_borrow().use_reference();
277*92a655aeSWedson Almeida Filho /// ```
27817f67160SWedson Almeida Filho pub struct ArcBorrow<'a, T: ?Sized + 'a> {
27917f67160SWedson Almeida Filho     inner: NonNull<ArcInner<T>>,
28017f67160SWedson Almeida Filho     _p: PhantomData<&'a ()>,
28117f67160SWedson Almeida Filho }
28217f67160SWedson Almeida Filho 
283*92a655aeSWedson Almeida Filho // This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
284*92a655aeSWedson Almeida Filho impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
285*92a655aeSWedson Almeida Filho 
28617f67160SWedson Almeida Filho impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
28717f67160SWedson Almeida Filho     fn clone(&self) -> Self {
28817f67160SWedson Almeida Filho         *self
28917f67160SWedson Almeida Filho     }
29017f67160SWedson Almeida Filho }
29117f67160SWedson Almeida Filho 
29217f67160SWedson Almeida Filho impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
29317f67160SWedson Almeida Filho 
29417f67160SWedson Almeida Filho impl<T: ?Sized> ArcBorrow<'_, T> {
29517f67160SWedson Almeida Filho     /// Creates a new [`ArcBorrow`] instance.
29617f67160SWedson Almeida Filho     ///
29717f67160SWedson Almeida Filho     /// # Safety
29817f67160SWedson Almeida Filho     ///
29917f67160SWedson Almeida Filho     /// Callers must ensure the following for the lifetime of the returned [`ArcBorrow`] instance:
30017f67160SWedson Almeida Filho     /// 1. That `inner` remains valid;
30117f67160SWedson Almeida Filho     /// 2. That no mutable references to `inner` are created.
30217f67160SWedson Almeida Filho     unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
30317f67160SWedson Almeida Filho         // INVARIANT: The safety requirements guarantee the invariants.
30417f67160SWedson Almeida Filho         Self {
30517f67160SWedson Almeida Filho             inner,
30617f67160SWedson Almeida Filho             _p: PhantomData,
30717f67160SWedson Almeida Filho         }
30817f67160SWedson Almeida Filho     }
30917f67160SWedson Almeida Filho }
31017f67160SWedson Almeida Filho 
31117f67160SWedson Almeida Filho impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
31217f67160SWedson Almeida Filho     fn from(b: ArcBorrow<'_, T>) -> Self {
31317f67160SWedson Almeida Filho         // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop`
31417f67160SWedson Almeida Filho         // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the
31517f67160SWedson Almeida Filho         // increment.
31617f67160SWedson Almeida Filho         ManuallyDrop::new(unsafe { Arc::from_inner(b.inner) })
31717f67160SWedson Almeida Filho             .deref()
31817f67160SWedson Almeida Filho             .clone()
31917f67160SWedson Almeida Filho     }
32017f67160SWedson Almeida Filho }
32117f67160SWedson Almeida Filho 
32217f67160SWedson Almeida Filho impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
32317f67160SWedson Almeida Filho     type Target = T;
32417f67160SWedson Almeida Filho 
32517f67160SWedson Almeida Filho     fn deref(&self) -> &Self::Target {
32617f67160SWedson Almeida Filho         // SAFETY: By the type invariant, the underlying object is still alive with no mutable
32717f67160SWedson Almeida Filho         // references to it, so it is safe to create a shared reference.
32817f67160SWedson Almeida Filho         unsafe { &self.inner.as_ref().data }
32917f67160SWedson Almeida Filho     }
33017f67160SWedson Almeida Filho }
331