xref: /openbmc/linux/rust/kernel/sync/arc.rs (revision 695c312ec5a68e4373d063ee649c7b925ffb5da7)
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 
180c7ae432SWedson Almeida Filho use crate::{
190c7ae432SWedson Almeida Filho     bindings,
2092c4a1e7SBenno Lossin     error::{self, Error},
21701608bdSBenno Lossin     init::{self, InPlaceInit, Init, PinInit},
22701608bdSBenno Lossin     try_init,
230c7ae432SWedson Almeida Filho     types::{ForeignOwnable, Opaque},
240c7ae432SWedson Almeida Filho };
259dc04365SWedson Almeida Filho use alloc::boxed::Box;
26f75cb6fcSWedson Almeida Filho use core::{
27d6dbca35SBenno Lossin     alloc::AllocError,
2800140a83SBoqun Feng     fmt,
29f75cb6fcSWedson Almeida Filho     marker::{PhantomData, Unsize},
3070e42ebbSWedson Almeida Filho     mem::{ManuallyDrop, MaybeUninit},
3170e42ebbSWedson Almeida Filho     ops::{Deref, DerefMut},
3270e42ebbSWedson Almeida Filho     pin::Pin,
33f75cb6fcSWedson Almeida Filho     ptr::NonNull,
34f75cb6fcSWedson Almeida Filho };
35701608bdSBenno Lossin use macros::pin_data;
369dc04365SWedson Almeida Filho 
371edd0337SAsahi Lina mod std_vendor;
381edd0337SAsahi Lina 
399dc04365SWedson Almeida Filho /// A reference-counted pointer to an instance of `T`.
409dc04365SWedson Almeida Filho ///
419dc04365SWedson Almeida Filho /// The reference count is incremented when new instances of [`Arc`] are created, and decremented
429dc04365SWedson Almeida Filho /// when they are dropped. When the count reaches zero, the underlying `T` is also dropped.
439dc04365SWedson Almeida Filho ///
449dc04365SWedson Almeida Filho /// # Invariants
459dc04365SWedson Almeida Filho ///
469dc04365SWedson Almeida Filho /// The reference count on an instance of [`Arc`] is always non-zero.
479dc04365SWedson Almeida Filho /// The object pointed to by [`Arc`] is always pinned.
489dc04365SWedson Almeida Filho ///
499dc04365SWedson Almeida Filho /// # Examples
509dc04365SWedson Almeida Filho ///
519dc04365SWedson Almeida Filho /// ```
529dc04365SWedson Almeida Filho /// use kernel::sync::Arc;
539dc04365SWedson Almeida Filho ///
549dc04365SWedson Almeida Filho /// struct Example {
559dc04365SWedson Almeida Filho ///     a: u32,
569dc04365SWedson Almeida Filho ///     b: u32,
579dc04365SWedson Almeida Filho /// }
589dc04365SWedson Almeida Filho ///
599dc04365SWedson Almeida Filho /// // Create a ref-counted instance of `Example`.
609dc04365SWedson Almeida Filho /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
619dc04365SWedson Almeida Filho ///
629dc04365SWedson Almeida Filho /// // Get a new pointer to `obj` and increment the refcount.
639dc04365SWedson Almeida Filho /// let cloned = obj.clone();
649dc04365SWedson Almeida Filho ///
659dc04365SWedson Almeida Filho /// // Assert that both `obj` and `cloned` point to the same underlying object.
669dc04365SWedson Almeida Filho /// assert!(core::ptr::eq(&*obj, &*cloned));
679dc04365SWedson Almeida Filho ///
689dc04365SWedson Almeida Filho /// // Destroy `obj` and decrement its refcount.
699dc04365SWedson Almeida Filho /// drop(obj);
709dc04365SWedson Almeida Filho ///
719dc04365SWedson Almeida Filho /// // Check that the values are still accessible through `cloned`.
729dc04365SWedson Almeida Filho /// assert_eq!(cloned.a, 10);
739dc04365SWedson Almeida Filho /// assert_eq!(cloned.b, 20);
749dc04365SWedson Almeida Filho ///
759dc04365SWedson Almeida Filho /// // The refcount drops to zero when `cloned` goes out of scope, and the memory is freed.
76bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
779dc04365SWedson Almeida Filho /// ```
7853528772SWedson Almeida Filho ///
7953528772SWedson Almeida Filho /// Using `Arc<T>` as the type of `self`:
8053528772SWedson Almeida Filho ///
8153528772SWedson Almeida Filho /// ```
8253528772SWedson Almeida Filho /// use kernel::sync::Arc;
8353528772SWedson Almeida Filho ///
8453528772SWedson Almeida Filho /// struct Example {
8553528772SWedson Almeida Filho ///     a: u32,
8653528772SWedson Almeida Filho ///     b: u32,
8753528772SWedson Almeida Filho /// }
8853528772SWedson Almeida Filho ///
8953528772SWedson Almeida Filho /// impl Example {
9053528772SWedson Almeida Filho ///     fn take_over(self: Arc<Self>) {
9153528772SWedson Almeida Filho ///         // ...
9253528772SWedson Almeida Filho ///     }
9353528772SWedson Almeida Filho ///
9453528772SWedson Almeida Filho ///     fn use_reference(self: &Arc<Self>) {
9553528772SWedson Almeida Filho ///         // ...
9653528772SWedson Almeida Filho ///     }
9753528772SWedson Almeida Filho /// }
9853528772SWedson Almeida Filho ///
9953528772SWedson Almeida Filho /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
10053528772SWedson Almeida Filho /// obj.use_reference();
10153528772SWedson Almeida Filho /// obj.take_over();
102bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
10353528772SWedson Almeida Filho /// ```
104f75cb6fcSWedson Almeida Filho ///
105f75cb6fcSWedson Almeida Filho /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`:
106f75cb6fcSWedson Almeida Filho ///
107f75cb6fcSWedson Almeida Filho /// ```
1080748424aSWedson Almeida Filho /// use kernel::sync::{Arc, ArcBorrow};
109f75cb6fcSWedson Almeida Filho ///
1100748424aSWedson Almeida Filho /// trait MyTrait {
1110748424aSWedson Almeida Filho ///     // Trait has a function whose `self` type is `Arc<Self>`.
1120748424aSWedson Almeida Filho ///     fn example1(self: Arc<Self>) {}
1130748424aSWedson Almeida Filho ///
1140748424aSWedson Almeida Filho ///     // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`.
1150748424aSWedson Almeida Filho ///     fn example2(self: ArcBorrow<'_, Self>) {}
1160748424aSWedson Almeida Filho /// }
117f75cb6fcSWedson Almeida Filho ///
118f75cb6fcSWedson Almeida Filho /// struct Example;
119f75cb6fcSWedson Almeida Filho /// impl MyTrait for Example {}
120f75cb6fcSWedson Almeida Filho ///
121f75cb6fcSWedson Almeida Filho /// // `obj` has type `Arc<Example>`.
122f75cb6fcSWedson Almeida Filho /// let obj: Arc<Example> = Arc::try_new(Example)?;
123f75cb6fcSWedson Almeida Filho ///
124f75cb6fcSWedson Almeida Filho /// // `coerced` has type `Arc<dyn MyTrait>`.
125f75cb6fcSWedson Almeida Filho /// let coerced: Arc<dyn MyTrait> = obj;
126bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
127f75cb6fcSWedson Almeida Filho /// ```
1289dc04365SWedson Almeida Filho pub struct Arc<T: ?Sized> {
1299dc04365SWedson Almeida Filho     ptr: NonNull<ArcInner<T>>,
1309dc04365SWedson Almeida Filho     _p: PhantomData<ArcInner<T>>,
1319dc04365SWedson Almeida Filho }
1329dc04365SWedson Almeida Filho 
133701608bdSBenno Lossin #[pin_data]
1349dc04365SWedson Almeida Filho #[repr(C)]
1359dc04365SWedson Almeida Filho struct ArcInner<T: ?Sized> {
1369dc04365SWedson Almeida Filho     refcount: Opaque<bindings::refcount_t>,
1379dc04365SWedson Almeida Filho     data: T,
1389dc04365SWedson Almeida Filho }
1399dc04365SWedson Almeida Filho 
14053528772SWedson Almeida Filho // This is to allow [`Arc`] (and variants) to be used as the type of `self`.
14153528772SWedson Almeida Filho impl<T: ?Sized> core::ops::Receiver for Arc<T> {}
14253528772SWedson Almeida Filho 
143f75cb6fcSWedson Almeida Filho // This is to allow coercion from `Arc<T>` to `Arc<U>` if `T` can be converted to the
144f75cb6fcSWedson Almeida Filho // dynamically-sized type (DST) `U`.
145f75cb6fcSWedson Almeida Filho impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
146f75cb6fcSWedson Almeida Filho 
1470748424aSWedson Almeida Filho // This is to allow `Arc<U>` to be dispatched on when `Arc<T>` can be coerced into `Arc<U>`.
1480748424aSWedson Almeida Filho impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
1490748424aSWedson Almeida Filho 
1509dc04365SWedson Almeida Filho // SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
1519dc04365SWedson Almeida Filho // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
152f8110cd1SAlice Ryhl // `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
153f8110cd1SAlice Ryhl // mutable reference when the reference count reaches zero and `T` is dropped.
1549dc04365SWedson Almeida Filho unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
1559dc04365SWedson Almeida Filho 
156d701e061SAlice Ryhl // SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync`
157d701e061SAlice Ryhl // because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
158d701e061SAlice Ryhl // it needs `T` to be `Send` because any thread that has a `&Arc<T>` may clone it and get an
159d701e061SAlice Ryhl // `Arc<T>` on that thread, so the thread may ultimately access `T` using a mutable reference when
160d701e061SAlice Ryhl // the reference count reaches zero and `T` is dropped.
1619dc04365SWedson Almeida Filho unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
1629dc04365SWedson Almeida Filho 
1639dc04365SWedson Almeida Filho impl<T> Arc<T> {
1649dc04365SWedson Almeida Filho     /// Constructs a new reference counted instance of `T`.
try_new(contents: T) -> Result<Self, AllocError>165d6dbca35SBenno Lossin     pub fn try_new(contents: T) -> Result<Self, AllocError> {
1669dc04365SWedson Almeida Filho         // INVARIANT: The refcount is initialised to a non-zero value.
1679dc04365SWedson Almeida Filho         let value = ArcInner {
1689dc04365SWedson Almeida Filho             // SAFETY: There are no safety requirements for this FFI call.
1699dc04365SWedson Almeida Filho             refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
1709dc04365SWedson Almeida Filho             data: contents,
1719dc04365SWedson Almeida Filho         };
1729dc04365SWedson Almeida Filho 
1739dc04365SWedson Almeida Filho         let inner = Box::try_new(value)?;
1749dc04365SWedson Almeida Filho 
1759dc04365SWedson Almeida Filho         // SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
1769dc04365SWedson Almeida Filho         // `Arc` object.
1779dc04365SWedson Almeida Filho         Ok(unsafe { Self::from_inner(Box::leak(inner).into()) })
1789dc04365SWedson Almeida Filho     }
17992c4a1e7SBenno Lossin 
18092c4a1e7SBenno Lossin     /// Use the given initializer to in-place initialize a `T`.
18192c4a1e7SBenno Lossin     ///
18292c4a1e7SBenno Lossin     /// If `T: !Unpin` it will not be able to move afterwards.
18392c4a1e7SBenno Lossin     #[inline]
pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Self> where Error: From<E>,18492c4a1e7SBenno Lossin     pub fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Self>
18592c4a1e7SBenno Lossin     where
18692c4a1e7SBenno Lossin         Error: From<E>,
18792c4a1e7SBenno Lossin     {
18892c4a1e7SBenno Lossin         UniqueArc::pin_init(init).map(|u| u.into())
18992c4a1e7SBenno Lossin     }
19092c4a1e7SBenno Lossin 
19192c4a1e7SBenno Lossin     /// Use the given initializer to in-place initialize a `T`.
19292c4a1e7SBenno Lossin     ///
193eed7a146SMiguel Ojeda     /// This is equivalent to [`Arc<T>::pin_init`], since an [`Arc`] is always pinned.
19492c4a1e7SBenno Lossin     #[inline]
init<E>(init: impl Init<T, E>) -> error::Result<Self> where Error: From<E>,19592c4a1e7SBenno Lossin     pub fn init<E>(init: impl Init<T, E>) -> error::Result<Self>
19692c4a1e7SBenno Lossin     where
19792c4a1e7SBenno Lossin         Error: From<E>,
19892c4a1e7SBenno Lossin     {
19992c4a1e7SBenno Lossin         UniqueArc::init(init).map(|u| u.into())
20092c4a1e7SBenno Lossin     }
2019dc04365SWedson Almeida Filho }
2029dc04365SWedson Almeida Filho 
2039dc04365SWedson Almeida Filho impl<T: ?Sized> Arc<T> {
2049dc04365SWedson Almeida Filho     /// Constructs a new [`Arc`] from an existing [`ArcInner`].
2059dc04365SWedson Almeida Filho     ///
2069dc04365SWedson Almeida Filho     /// # Safety
2079dc04365SWedson Almeida Filho     ///
2089dc04365SWedson Almeida Filho     /// The caller must ensure that `inner` points to a valid location and has a non-zero reference
2099dc04365SWedson Almeida Filho     /// count, one of which will be owned by the new [`Arc`] instance.
from_inner(inner: NonNull<ArcInner<T>>) -> Self2109dc04365SWedson Almeida Filho     unsafe fn from_inner(inner: NonNull<ArcInner<T>>) -> Self {
2119dc04365SWedson Almeida Filho         // INVARIANT: By the safety requirements, the invariants hold.
2129dc04365SWedson Almeida Filho         Arc {
2139dc04365SWedson Almeida Filho             ptr: inner,
2149dc04365SWedson Almeida Filho             _p: PhantomData,
2159dc04365SWedson Almeida Filho         }
2169dc04365SWedson Almeida Filho     }
21717f67160SWedson Almeida Filho 
21817f67160SWedson Almeida Filho     /// Returns an [`ArcBorrow`] from the given [`Arc`].
21917f67160SWedson Almeida Filho     ///
22017f67160SWedson Almeida Filho     /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
22117f67160SWedson Almeida Filho     /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
22217f67160SWedson Almeida Filho     #[inline]
as_arc_borrow(&self) -> ArcBorrow<'_, T>22317f67160SWedson Almeida Filho     pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
22417f67160SWedson Almeida Filho         // SAFETY: The constraint that the lifetime of the shared reference must outlive that of
22517f67160SWedson Almeida Filho         // the returned `ArcBorrow` ensures that the object remains alive and that no mutable
22617f67160SWedson Almeida Filho         // reference can be created.
22717f67160SWedson Almeida Filho         unsafe { ArcBorrow::new(self.ptr) }
22817f67160SWedson Almeida Filho     }
229bd780aeaSAlice Ryhl 
230bd780aeaSAlice Ryhl     /// Compare whether two [`Arc`] pointers reference the same underlying object.
ptr_eq(this: &Self, other: &Self) -> bool231bd780aeaSAlice Ryhl     pub fn ptr_eq(this: &Self, other: &Self) -> bool {
232bd780aeaSAlice Ryhl         core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
233bd780aeaSAlice Ryhl     }
2349dc04365SWedson Almeida Filho }
2359dc04365SWedson Almeida Filho 
2360c7ae432SWedson Almeida Filho impl<T: 'static> ForeignOwnable for Arc<T> {
2370c7ae432SWedson Almeida Filho     type Borrowed<'a> = ArcBorrow<'a, T>;
2380c7ae432SWedson Almeida Filho 
into_foreign(self) -> *const core::ffi::c_void2390c7ae432SWedson Almeida Filho     fn into_foreign(self) -> *const core::ffi::c_void {
2400c7ae432SWedson Almeida Filho         ManuallyDrop::new(self).ptr.as_ptr() as _
2410c7ae432SWedson Almeida Filho     }
2420c7ae432SWedson Almeida Filho 
borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T>2430c7ae432SWedson Almeida Filho     unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
2440c7ae432SWedson Almeida Filho         // SAFETY: By the safety requirement of this function, we know that `ptr` came from
2450c7ae432SWedson Almeida Filho         // a previous call to `Arc::into_foreign`.
2460c7ae432SWedson Almeida Filho         let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
2470c7ae432SWedson Almeida Filho 
2480c7ae432SWedson Almeida Filho         // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
2491d24eb2dSAlice Ryhl         // for the lifetime of the returned value.
2500c7ae432SWedson Almeida Filho         unsafe { ArcBorrow::new(inner) }
2510c7ae432SWedson Almeida Filho     }
2520c7ae432SWedson Almeida Filho 
from_foreign(ptr: *const core::ffi::c_void) -> Self2530c7ae432SWedson Almeida Filho     unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
2540c7ae432SWedson Almeida Filho         // SAFETY: By the safety requirement of this function, we know that `ptr` came from
2550c7ae432SWedson Almeida Filho         // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
2560c7ae432SWedson Almeida Filho         // holds a reference count increment that is transferrable to us.
2570c7ae432SWedson Almeida Filho         unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) }
2580c7ae432SWedson Almeida Filho     }
2590c7ae432SWedson Almeida Filho }
2600c7ae432SWedson Almeida Filho 
2619dc04365SWedson Almeida Filho impl<T: ?Sized> Deref for Arc<T> {
2629dc04365SWedson Almeida Filho     type Target = T;
2639dc04365SWedson Almeida Filho 
deref(&self) -> &Self::Target2649dc04365SWedson Almeida Filho     fn deref(&self) -> &Self::Target {
2659dc04365SWedson Almeida Filho         // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
2669dc04365SWedson Almeida Filho         // safe to dereference it.
2679dc04365SWedson Almeida Filho         unsafe { &self.ptr.as_ref().data }
2689dc04365SWedson Almeida Filho     }
2699dc04365SWedson Almeida Filho }
2709dc04365SWedson Almeida Filho 
27147329ba1SAlice Ryhl impl<T: ?Sized> AsRef<T> for Arc<T> {
as_ref(&self) -> &T27247329ba1SAlice Ryhl     fn as_ref(&self) -> &T {
27347329ba1SAlice Ryhl         self.deref()
27447329ba1SAlice Ryhl     }
27547329ba1SAlice Ryhl }
27647329ba1SAlice Ryhl 
2779dc04365SWedson Almeida Filho impl<T: ?Sized> Clone for Arc<T> {
clone(&self) -> Self2789dc04365SWedson Almeida Filho     fn clone(&self) -> Self {
2799dc04365SWedson Almeida Filho         // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
2809dc04365SWedson Almeida Filho         // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
2819dc04365SWedson Almeida Filho         // safe to increment the refcount.
2829dc04365SWedson Almeida Filho         unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
2839dc04365SWedson Almeida Filho 
2849dc04365SWedson Almeida Filho         // SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
2859dc04365SWedson Almeida Filho         unsafe { Self::from_inner(self.ptr) }
2869dc04365SWedson Almeida Filho     }
2879dc04365SWedson Almeida Filho }
2889dc04365SWedson Almeida Filho 
2899dc04365SWedson Almeida Filho impl<T: ?Sized> Drop for Arc<T> {
drop(&mut self)2909dc04365SWedson Almeida Filho     fn drop(&mut self) {
2919dc04365SWedson Almeida Filho         // SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot
2929dc04365SWedson Almeida Filho         // touch `refcount` after it's decremented to a non-zero value because another thread/CPU
2939dc04365SWedson Almeida Filho         // may concurrently decrement it to zero and free it. It is ok to have a raw pointer to
2949dc04365SWedson Almeida Filho         // freed/invalid memory as long as it is never dereferenced.
2959dc04365SWedson Almeida Filho         let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
2969dc04365SWedson Almeida Filho 
2979dc04365SWedson Almeida Filho         // INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and
2989dc04365SWedson Almeida Filho         // this instance is being dropped, so the broken invariant is not observable.
2999dc04365SWedson Almeida Filho         // SAFETY: Also by the type invariant, we are allowed to decrement the refcount.
3009dc04365SWedson Almeida Filho         let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
3019dc04365SWedson Almeida Filho         if is_zero {
3029dc04365SWedson Almeida Filho             // The count reached zero, we must free the memory.
3039dc04365SWedson Almeida Filho             //
3049dc04365SWedson Almeida Filho             // SAFETY: The pointer was initialised from the result of `Box::leak`.
305*31a254f6SMiguel Ojeda             unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
3069dc04365SWedson Almeida Filho         }
3079dc04365SWedson Almeida Filho     }
3089dc04365SWedson Almeida Filho }
30917f67160SWedson Almeida Filho 
31070e42ebbSWedson Almeida Filho impl<T: ?Sized> From<UniqueArc<T>> for Arc<T> {
from(item: UniqueArc<T>) -> Self31170e42ebbSWedson Almeida Filho     fn from(item: UniqueArc<T>) -> Self {
31270e42ebbSWedson Almeida Filho         item.inner
31370e42ebbSWedson Almeida Filho     }
31470e42ebbSWedson Almeida Filho }
31570e42ebbSWedson Almeida Filho 
31670e42ebbSWedson Almeida Filho impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
from(item: Pin<UniqueArc<T>>) -> Self31770e42ebbSWedson Almeida Filho     fn from(item: Pin<UniqueArc<T>>) -> Self {
31870e42ebbSWedson Almeida Filho         // SAFETY: The type invariants of `Arc` guarantee that the data is pinned.
31970e42ebbSWedson Almeida Filho         unsafe { Pin::into_inner_unchecked(item).inner }
32070e42ebbSWedson Almeida Filho     }
32170e42ebbSWedson Almeida Filho }
32270e42ebbSWedson Almeida Filho 
32317f67160SWedson Almeida Filho /// A borrowed reference to an [`Arc`] instance.
32417f67160SWedson Almeida Filho ///
32517f67160SWedson Almeida Filho /// For cases when one doesn't ever need to increment the refcount on the allocation, it is simpler
32617f67160SWedson Almeida Filho /// to use just `&T`, which we can trivially get from an `Arc<T>` instance.
32717f67160SWedson Almeida Filho ///
32817f67160SWedson Almeida Filho /// However, when one may need to increment the refcount, it is preferable to use an `ArcBorrow<T>`
32917f67160SWedson Almeida Filho /// over `&Arc<T>` because the latter results in a double-indirection: a pointer (shared reference)
33017f67160SWedson Almeida Filho /// to a pointer (`Arc<T>`) to the object (`T`). An [`ArcBorrow`] eliminates this double
33117f67160SWedson Almeida Filho /// indirection while still allowing one to increment the refcount and getting an `Arc<T>` when/if
33217f67160SWedson Almeida Filho /// needed.
33317f67160SWedson Almeida Filho ///
33417f67160SWedson Almeida Filho /// # Invariants
33517f67160SWedson Almeida Filho ///
33617f67160SWedson Almeida Filho /// There are no mutable references to the underlying [`Arc`], and it remains valid for the
33717f67160SWedson Almeida Filho /// lifetime of the [`ArcBorrow`] instance.
33817f67160SWedson Almeida Filho ///
33917f67160SWedson Almeida Filho /// # Example
34017f67160SWedson Almeida Filho ///
34117f67160SWedson Almeida Filho /// ```
342bfa7dff0SMiguel Ojeda /// use kernel::sync::{Arc, ArcBorrow};
34317f67160SWedson Almeida Filho ///
34417f67160SWedson Almeida Filho /// struct Example;
34517f67160SWedson Almeida Filho ///
34617f67160SWedson Almeida Filho /// fn do_something(e: ArcBorrow<'_, Example>) -> Arc<Example> {
34717f67160SWedson Almeida Filho ///     e.into()
34817f67160SWedson Almeida Filho /// }
34917f67160SWedson Almeida Filho ///
35017f67160SWedson Almeida Filho /// let obj = Arc::try_new(Example)?;
35117f67160SWedson Almeida Filho /// let cloned = do_something(obj.as_arc_borrow());
35217f67160SWedson Almeida Filho ///
35317f67160SWedson Almeida Filho /// // Assert that both `obj` and `cloned` point to the same underlying object.
35417f67160SWedson Almeida Filho /// assert!(core::ptr::eq(&*obj, &*cloned));
355bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
35617f67160SWedson Almeida Filho /// ```
35792a655aeSWedson Almeida Filho ///
35892a655aeSWedson Almeida Filho /// Using `ArcBorrow<T>` as the type of `self`:
35992a655aeSWedson Almeida Filho ///
36092a655aeSWedson Almeida Filho /// ```
361bfa7dff0SMiguel Ojeda /// use kernel::sync::{Arc, ArcBorrow};
36292a655aeSWedson Almeida Filho ///
36392a655aeSWedson Almeida Filho /// struct Example {
36492a655aeSWedson Almeida Filho ///     a: u32,
36592a655aeSWedson Almeida Filho ///     b: u32,
36692a655aeSWedson Almeida Filho /// }
36792a655aeSWedson Almeida Filho ///
36892a655aeSWedson Almeida Filho /// impl Example {
36992a655aeSWedson Almeida Filho ///     fn use_reference(self: ArcBorrow<'_, Self>) {
37092a655aeSWedson Almeida Filho ///         // ...
37192a655aeSWedson Almeida Filho ///     }
37292a655aeSWedson Almeida Filho /// }
37392a655aeSWedson Almeida Filho ///
37492a655aeSWedson Almeida Filho /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
37592a655aeSWedson Almeida Filho /// obj.as_arc_borrow().use_reference();
376bfa7dff0SMiguel Ojeda /// # Ok::<(), Error>(())
37792a655aeSWedson Almeida Filho /// ```
37817f67160SWedson Almeida Filho pub struct ArcBorrow<'a, T: ?Sized + 'a> {
37917f67160SWedson Almeida Filho     inner: NonNull<ArcInner<T>>,
38017f67160SWedson Almeida Filho     _p: PhantomData<&'a ()>,
38117f67160SWedson Almeida Filho }
38217f67160SWedson Almeida Filho 
38392a655aeSWedson Almeida Filho // This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
38492a655aeSWedson Almeida Filho impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
38592a655aeSWedson Almeida Filho 
3860748424aSWedson Almeida Filho // This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
3870748424aSWedson Almeida Filho // `ArcBorrow<U>`.
3880748424aSWedson Almeida Filho impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'_, U>>
3890748424aSWedson Almeida Filho     for ArcBorrow<'_, T>
3900748424aSWedson Almeida Filho {
3910748424aSWedson Almeida Filho }
3920748424aSWedson Almeida Filho 
39317f67160SWedson Almeida Filho impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
clone(&self) -> Self39417f67160SWedson Almeida Filho     fn clone(&self) -> Self {
39517f67160SWedson Almeida Filho         *self
39617f67160SWedson Almeida Filho     }
39717f67160SWedson Almeida Filho }
39817f67160SWedson Almeida Filho 
39917f67160SWedson Almeida Filho impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
40017f67160SWedson Almeida Filho 
40117f67160SWedson Almeida Filho impl<T: ?Sized> ArcBorrow<'_, T> {
40217f67160SWedson Almeida Filho     /// Creates a new [`ArcBorrow`] instance.
40317f67160SWedson Almeida Filho     ///
40417f67160SWedson Almeida Filho     /// # Safety
40517f67160SWedson Almeida Filho     ///
40617f67160SWedson Almeida Filho     /// Callers must ensure the following for the lifetime of the returned [`ArcBorrow`] instance:
40717f67160SWedson Almeida Filho     /// 1. That `inner` remains valid;
40817f67160SWedson Almeida Filho     /// 2. That no mutable references to `inner` are created.
new(inner: NonNull<ArcInner<T>>) -> Self40917f67160SWedson Almeida Filho     unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
41017f67160SWedson Almeida Filho         // INVARIANT: The safety requirements guarantee the invariants.
41117f67160SWedson Almeida Filho         Self {
41217f67160SWedson Almeida Filho             inner,
41317f67160SWedson Almeida Filho             _p: PhantomData,
41417f67160SWedson Almeida Filho         }
41517f67160SWedson Almeida Filho     }
41617f67160SWedson Almeida Filho }
41717f67160SWedson Almeida Filho 
41817f67160SWedson Almeida Filho impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
from(b: ArcBorrow<'_, T>) -> Self41917f67160SWedson Almeida Filho     fn from(b: ArcBorrow<'_, T>) -> Self {
42017f67160SWedson Almeida Filho         // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop`
42117f67160SWedson Almeida Filho         // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the
42217f67160SWedson Almeida Filho         // increment.
42317f67160SWedson Almeida Filho         ManuallyDrop::new(unsafe { Arc::from_inner(b.inner) })
42417f67160SWedson Almeida Filho             .deref()
42517f67160SWedson Almeida Filho             .clone()
42617f67160SWedson Almeida Filho     }
42717f67160SWedson Almeida Filho }
42817f67160SWedson Almeida Filho 
42917f67160SWedson Almeida Filho impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
43017f67160SWedson Almeida Filho     type Target = T;
43117f67160SWedson Almeida Filho 
deref(&self) -> &Self::Target43217f67160SWedson Almeida Filho     fn deref(&self) -> &Self::Target {
43317f67160SWedson Almeida Filho         // SAFETY: By the type invariant, the underlying object is still alive with no mutable
43417f67160SWedson Almeida Filho         // references to it, so it is safe to create a shared reference.
43517f67160SWedson Almeida Filho         unsafe { &self.inner.as_ref().data }
43617f67160SWedson Almeida Filho     }
43717f67160SWedson Almeida Filho }
43870e42ebbSWedson Almeida Filho 
43970e42ebbSWedson Almeida Filho /// A refcounted object that is known to have a refcount of 1.
44070e42ebbSWedson Almeida Filho ///
44170e42ebbSWedson Almeida Filho /// It is mutable and can be converted to an [`Arc`] so that it can be shared.
44270e42ebbSWedson Almeida Filho ///
44370e42ebbSWedson Almeida Filho /// # Invariants
44470e42ebbSWedson Almeida Filho ///
44570e42ebbSWedson Almeida Filho /// `inner` always has a reference count of 1.
44670e42ebbSWedson Almeida Filho ///
44770e42ebbSWedson Almeida Filho /// # Examples
44870e42ebbSWedson Almeida Filho ///
44970e42ebbSWedson Almeida Filho /// In the following example, we make changes to the inner object before turning it into an
45070e42ebbSWedson Almeida Filho /// `Arc<Test>` object (after which point, it cannot be mutated directly). Note that `x.into()`
45170e42ebbSWedson Almeida Filho /// cannot fail.
45270e42ebbSWedson Almeida Filho ///
45370e42ebbSWedson Almeida Filho /// ```
45470e42ebbSWedson Almeida Filho /// use kernel::sync::{Arc, UniqueArc};
45570e42ebbSWedson Almeida Filho ///
45670e42ebbSWedson Almeida Filho /// struct Example {
45770e42ebbSWedson Almeida Filho ///     a: u32,
45870e42ebbSWedson Almeida Filho ///     b: u32,
45970e42ebbSWedson Almeida Filho /// }
46070e42ebbSWedson Almeida Filho ///
46170e42ebbSWedson Almeida Filho /// fn test() -> Result<Arc<Example>> {
46270e42ebbSWedson Almeida Filho ///     let mut x = UniqueArc::try_new(Example { a: 10, b: 20 })?;
46370e42ebbSWedson Almeida Filho ///     x.a += 1;
46470e42ebbSWedson Almeida Filho ///     x.b += 1;
46570e42ebbSWedson Almeida Filho ///     Ok(x.into())
46670e42ebbSWedson Almeida Filho /// }
46770e42ebbSWedson Almeida Filho ///
46870e42ebbSWedson Almeida Filho /// # test().unwrap();
46970e42ebbSWedson Almeida Filho /// ```
47070e42ebbSWedson Almeida Filho ///
47170e42ebbSWedson Almeida Filho /// In the following example we first allocate memory for a ref-counted `Example` but we don't
47270e42ebbSWedson Almeida Filho /// initialise it on allocation. We do initialise it later with a call to [`UniqueArc::write`],
47370e42ebbSWedson Almeida Filho /// followed by a conversion to `Arc<Example>`. This is particularly useful when allocation happens
47470e42ebbSWedson Almeida Filho /// in one context (e.g., sleepable) and initialisation in another (e.g., atomic):
47570e42ebbSWedson Almeida Filho ///
47670e42ebbSWedson Almeida Filho /// ```
47770e42ebbSWedson Almeida Filho /// use kernel::sync::{Arc, UniqueArc};
47870e42ebbSWedson Almeida Filho ///
47970e42ebbSWedson Almeida Filho /// struct Example {
48070e42ebbSWedson Almeida Filho ///     a: u32,
48170e42ebbSWedson Almeida Filho ///     b: u32,
48270e42ebbSWedson Almeida Filho /// }
48370e42ebbSWedson Almeida Filho ///
48470e42ebbSWedson Almeida Filho /// fn test() -> Result<Arc<Example>> {
48570e42ebbSWedson Almeida Filho ///     let x = UniqueArc::try_new_uninit()?;
48670e42ebbSWedson Almeida Filho ///     Ok(x.write(Example { a: 10, b: 20 }).into())
48770e42ebbSWedson Almeida Filho /// }
48870e42ebbSWedson Almeida Filho ///
48970e42ebbSWedson Almeida Filho /// # test().unwrap();
49070e42ebbSWedson Almeida Filho /// ```
49170e42ebbSWedson Almeida Filho ///
49270e42ebbSWedson Almeida Filho /// In the last example below, the caller gets a pinned instance of `Example` while converting to
49370e42ebbSWedson Almeida Filho /// `Arc<Example>`; this is useful in scenarios where one needs a pinned reference during
49470e42ebbSWedson Almeida Filho /// initialisation, for example, when initialising fields that are wrapped in locks.
49570e42ebbSWedson Almeida Filho ///
49670e42ebbSWedson Almeida Filho /// ```
49770e42ebbSWedson Almeida Filho /// use kernel::sync::{Arc, UniqueArc};
49870e42ebbSWedson Almeida Filho ///
49970e42ebbSWedson Almeida Filho /// struct Example {
50070e42ebbSWedson Almeida Filho ///     a: u32,
50170e42ebbSWedson Almeida Filho ///     b: u32,
50270e42ebbSWedson Almeida Filho /// }
50370e42ebbSWedson Almeida Filho ///
50470e42ebbSWedson Almeida Filho /// fn test() -> Result<Arc<Example>> {
50570e42ebbSWedson Almeida Filho ///     let mut pinned = Pin::from(UniqueArc::try_new(Example { a: 10, b: 20 })?);
50670e42ebbSWedson Almeida Filho ///     // We can modify `pinned` because it is `Unpin`.
50770e42ebbSWedson Almeida Filho ///     pinned.as_mut().a += 1;
50870e42ebbSWedson Almeida Filho ///     Ok(pinned.into())
50970e42ebbSWedson Almeida Filho /// }
51070e42ebbSWedson Almeida Filho ///
51170e42ebbSWedson Almeida Filho /// # test().unwrap();
51270e42ebbSWedson Almeida Filho /// ```
51370e42ebbSWedson Almeida Filho pub struct UniqueArc<T: ?Sized> {
51470e42ebbSWedson Almeida Filho     inner: Arc<T>,
51570e42ebbSWedson Almeida Filho }
51670e42ebbSWedson Almeida Filho 
51770e42ebbSWedson Almeida Filho impl<T> UniqueArc<T> {
51870e42ebbSWedson Almeida Filho     /// Tries to allocate a new [`UniqueArc`] instance.
try_new(value: T) -> Result<Self, AllocError>519d6dbca35SBenno Lossin     pub fn try_new(value: T) -> Result<Self, AllocError> {
52070e42ebbSWedson Almeida Filho         Ok(Self {
52170e42ebbSWedson Almeida Filho             // INVARIANT: The newly-created object has a ref-count of 1.
52270e42ebbSWedson Almeida Filho             inner: Arc::try_new(value)?,
52370e42ebbSWedson Almeida Filho         })
52470e42ebbSWedson Almeida Filho     }
52570e42ebbSWedson Almeida Filho 
52670e42ebbSWedson Almeida Filho     /// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError>527d6dbca35SBenno Lossin     pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
528701608bdSBenno Lossin         // INVARIANT: The refcount is initialised to a non-zero value.
529701608bdSBenno Lossin         let inner = Box::try_init::<AllocError>(try_init!(ArcInner {
530701608bdSBenno Lossin             // SAFETY: There are no safety requirements for this FFI call.
531701608bdSBenno Lossin             refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
532701608bdSBenno Lossin             data <- init::uninit::<T, AllocError>(),
533701608bdSBenno Lossin         }? AllocError))?;
534701608bdSBenno Lossin         Ok(UniqueArc {
53570e42ebbSWedson Almeida Filho             // INVARIANT: The newly-created object has a ref-count of 1.
536701608bdSBenno Lossin             // SAFETY: The pointer from the `Box` is valid.
537701608bdSBenno Lossin             inner: unsafe { Arc::from_inner(Box::leak(inner).into()) },
53870e42ebbSWedson Almeida Filho         })
53970e42ebbSWedson Almeida Filho     }
54070e42ebbSWedson Almeida Filho }
54170e42ebbSWedson Almeida Filho 
54270e42ebbSWedson Almeida Filho impl<T> UniqueArc<MaybeUninit<T>> {
54370e42ebbSWedson Almeida Filho     /// Converts a `UniqueArc<MaybeUninit<T>>` into a `UniqueArc<T>` by writing a value into it.
write(mut self, value: T) -> UniqueArc<T>54470e42ebbSWedson Almeida Filho     pub fn write(mut self, value: T) -> UniqueArc<T> {
54570e42ebbSWedson Almeida Filho         self.deref_mut().write(value);
54631d95c2fSAsahi Lina         // SAFETY: We just wrote the value to be initialized.
54731d95c2fSAsahi Lina         unsafe { self.assume_init() }
54831d95c2fSAsahi Lina     }
54931d95c2fSAsahi Lina 
55031d95c2fSAsahi Lina     /// Unsafely assume that `self` is initialized.
55131d95c2fSAsahi Lina     ///
55231d95c2fSAsahi Lina     /// # Safety
55331d95c2fSAsahi Lina     ///
55431d95c2fSAsahi Lina     /// The caller guarantees that the value behind this pointer has been initialized. It is
55531d95c2fSAsahi Lina     /// *immediate* UB to call this when the value is not initialized.
assume_init(self) -> UniqueArc<T>55631d95c2fSAsahi Lina     pub unsafe fn assume_init(self) -> UniqueArc<T> {
55770e42ebbSWedson Almeida Filho         let inner = ManuallyDrop::new(self).inner.ptr;
55870e42ebbSWedson Almeida Filho         UniqueArc {
55970e42ebbSWedson Almeida Filho             // SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be
56070e42ebbSWedson Almeida Filho             // dropped). The types are compatible because `MaybeUninit<T>` is compatible with `T`.
56170e42ebbSWedson Almeida Filho             inner: unsafe { Arc::from_inner(inner.cast()) },
56270e42ebbSWedson Almeida Filho         }
56370e42ebbSWedson Almeida Filho     }
5641944caa8SBenno Lossin 
5651944caa8SBenno Lossin     /// Initialize `self` using the given initializer.
init_with<E>(mut self, init: impl Init<T, E>) -> core::result::Result<UniqueArc<T>, E>5661944caa8SBenno Lossin     pub fn init_with<E>(mut self, init: impl Init<T, E>) -> core::result::Result<UniqueArc<T>, E> {
5671944caa8SBenno Lossin         // SAFETY: The supplied pointer is valid for initialization.
5681944caa8SBenno Lossin         match unsafe { init.__init(self.as_mut_ptr()) } {
5691944caa8SBenno Lossin             // SAFETY: Initialization completed successfully.
5701944caa8SBenno Lossin             Ok(()) => Ok(unsafe { self.assume_init() }),
5711944caa8SBenno Lossin             Err(err) => Err(err),
5721944caa8SBenno Lossin         }
5731944caa8SBenno Lossin     }
5741944caa8SBenno Lossin 
5751944caa8SBenno Lossin     /// Pin-initialize `self` using the given pin-initializer.
pin_init_with<E>( mut self, init: impl PinInit<T, E>, ) -> core::result::Result<Pin<UniqueArc<T>>, E>5761944caa8SBenno Lossin     pub fn pin_init_with<E>(
5771944caa8SBenno Lossin         mut self,
5781944caa8SBenno Lossin         init: impl PinInit<T, E>,
5791944caa8SBenno Lossin     ) -> core::result::Result<Pin<UniqueArc<T>>, E> {
5801944caa8SBenno Lossin         // SAFETY: The supplied pointer is valid for initialization and we will later pin the value
5811944caa8SBenno Lossin         // to ensure it does not move.
5821944caa8SBenno Lossin         match unsafe { init.__pinned_init(self.as_mut_ptr()) } {
5831944caa8SBenno Lossin             // SAFETY: Initialization completed successfully.
5841944caa8SBenno Lossin             Ok(()) => Ok(unsafe { self.assume_init() }.into()),
5851944caa8SBenno Lossin             Err(err) => Err(err),
5861944caa8SBenno Lossin         }
5871944caa8SBenno Lossin     }
58870e42ebbSWedson Almeida Filho }
58970e42ebbSWedson Almeida Filho 
59070e42ebbSWedson Almeida Filho impl<T: ?Sized> From<UniqueArc<T>> for Pin<UniqueArc<T>> {
from(obj: UniqueArc<T>) -> Self59170e42ebbSWedson Almeida Filho     fn from(obj: UniqueArc<T>) -> Self {
59270e42ebbSWedson Almeida Filho         // SAFETY: It is not possible to move/replace `T` inside a `Pin<UniqueArc<T>>` (unless `T`
59370e42ebbSWedson Almeida Filho         // is `Unpin`), so it is ok to convert it to `Pin<UniqueArc<T>>`.
59470e42ebbSWedson Almeida Filho         unsafe { Pin::new_unchecked(obj) }
59570e42ebbSWedson Almeida Filho     }
59670e42ebbSWedson Almeida Filho }
59770e42ebbSWedson Almeida Filho 
59870e42ebbSWedson Almeida Filho impl<T: ?Sized> Deref for UniqueArc<T> {
59970e42ebbSWedson Almeida Filho     type Target = T;
60070e42ebbSWedson Almeida Filho 
deref(&self) -> &Self::Target60170e42ebbSWedson Almeida Filho     fn deref(&self) -> &Self::Target {
60270e42ebbSWedson Almeida Filho         self.inner.deref()
60370e42ebbSWedson Almeida Filho     }
60470e42ebbSWedson Almeida Filho }
60570e42ebbSWedson Almeida Filho 
60670e42ebbSWedson Almeida Filho impl<T: ?Sized> DerefMut for UniqueArc<T> {
deref_mut(&mut self) -> &mut Self::Target60770e42ebbSWedson Almeida Filho     fn deref_mut(&mut self) -> &mut Self::Target {
60870e42ebbSWedson Almeida Filho         // SAFETY: By the `Arc` type invariant, there is necessarily a reference to the object, so
60970e42ebbSWedson Almeida Filho         // it is safe to dereference it. Additionally, we know there is only one reference when
61070e42ebbSWedson Almeida Filho         // it's inside a `UniqueArc`, so it is safe to get a mutable reference.
61170e42ebbSWedson Almeida Filho         unsafe { &mut self.inner.ptr.as_mut().data }
61270e42ebbSWedson Almeida Filho     }
61370e42ebbSWedson Almeida Filho }
61400140a83SBoqun Feng 
61500140a83SBoqun Feng impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result61600140a83SBoqun Feng     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61700140a83SBoqun Feng         fmt::Display::fmt(self.deref(), f)
61800140a83SBoqun Feng     }
61900140a83SBoqun Feng }
62000140a83SBoqun Feng 
62100140a83SBoqun Feng impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result62200140a83SBoqun Feng     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62300140a83SBoqun Feng         fmt::Display::fmt(self.deref(), f)
62400140a83SBoqun Feng     }
62500140a83SBoqun Feng }
62600140a83SBoqun Feng 
62700140a83SBoqun Feng impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result62800140a83SBoqun Feng     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62900140a83SBoqun Feng         fmt::Debug::fmt(self.deref(), f)
63000140a83SBoqun Feng     }
63100140a83SBoqun Feng }
63200140a83SBoqun Feng 
63300140a83SBoqun Feng impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result63400140a83SBoqun Feng     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63500140a83SBoqun Feng         fmt::Debug::fmt(self.deref(), f)
63600140a83SBoqun Feng     }
63700140a83SBoqun Feng }
638