xref: /linux/rust/kernel/sync/arc.rs (revision 78c3925c048c752334873f56c3a3d1c9d53e0416)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! A reference-counted pointer.
4 //!
5 //! This module implements a way for users to create reference-counted objects and pointers to
6 //! them. Such a pointer automatically increments and decrements the count, and drops the
7 //! underlying object when it reaches zero. It is also safe to use concurrently from multiple
8 //! threads.
9 //!
10 //! It is different from the standard library's [`Arc`] in a few ways:
11 //! 1. It is backed by the kernel's `refcount_t` type.
12 //! 2. It does not support weak references, which allows it to be half the size.
13 //! 3. It saturates the reference count instead of aborting when it goes over a threshold.
14 //! 4. It does not provide a `get_mut` method, so the ref counted object is pinned.
15 //!
16 //! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
17 
18 use crate::{
19     bindings,
20     error::{self, Error},
21     init::{self, InPlaceInit, Init, PinInit},
22     try_init,
23     types::{ForeignOwnable, Opaque},
24 };
25 use alloc::boxed::Box;
26 use core::{
27     alloc::{AllocError, Layout},
28     fmt,
29     marker::{PhantomData, Unsize},
30     mem::{ManuallyDrop, MaybeUninit},
31     ops::{Deref, DerefMut},
32     pin::Pin,
33     ptr::NonNull,
34 };
35 use macros::pin_data;
36 
37 mod std_vendor;
38 
39 /// A reference-counted pointer to an instance of `T`.
40 ///
41 /// The reference count is incremented when new instances of [`Arc`] are created, and decremented
42 /// when they are dropped. When the count reaches zero, the underlying `T` is also dropped.
43 ///
44 /// # Invariants
45 ///
46 /// The reference count on an instance of [`Arc`] is always non-zero.
47 /// The object pointed to by [`Arc`] is always pinned.
48 ///
49 /// # Examples
50 ///
51 /// ```
52 /// use kernel::sync::Arc;
53 ///
54 /// struct Example {
55 ///     a: u32,
56 ///     b: u32,
57 /// }
58 ///
59 /// // Create a refcounted instance of `Example`.
60 /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
61 ///
62 /// // Get a new pointer to `obj` and increment the refcount.
63 /// let cloned = obj.clone();
64 ///
65 /// // Assert that both `obj` and `cloned` point to the same underlying object.
66 /// assert!(core::ptr::eq(&*obj, &*cloned));
67 ///
68 /// // Destroy `obj` and decrement its refcount.
69 /// drop(obj);
70 ///
71 /// // Check that the values are still accessible through `cloned`.
72 /// assert_eq!(cloned.a, 10);
73 /// assert_eq!(cloned.b, 20);
74 ///
75 /// // The refcount drops to zero when `cloned` goes out of scope, and the memory is freed.
76 /// # Ok::<(), Error>(())
77 /// ```
78 ///
79 /// Using `Arc<T>` as the type of `self`:
80 ///
81 /// ```
82 /// use kernel::sync::Arc;
83 ///
84 /// struct Example {
85 ///     a: u32,
86 ///     b: u32,
87 /// }
88 ///
89 /// impl Example {
90 ///     fn take_over(self: Arc<Self>) {
91 ///         // ...
92 ///     }
93 ///
94 ///     fn use_reference(self: &Arc<Self>) {
95 ///         // ...
96 ///     }
97 /// }
98 ///
99 /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
100 /// obj.use_reference();
101 /// obj.take_over();
102 /// # Ok::<(), Error>(())
103 /// ```
104 ///
105 /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`:
106 ///
107 /// ```
108 /// use kernel::sync::{Arc, ArcBorrow};
109 ///
110 /// trait MyTrait {
111 ///     // Trait has a function whose `self` type is `Arc<Self>`.
112 ///     fn example1(self: Arc<Self>) {}
113 ///
114 ///     // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`.
115 ///     fn example2(self: ArcBorrow<'_, Self>) {}
116 /// }
117 ///
118 /// struct Example;
119 /// impl MyTrait for Example {}
120 ///
121 /// // `obj` has type `Arc<Example>`.
122 /// let obj: Arc<Example> = Arc::try_new(Example)?;
123 ///
124 /// // `coerced` has type `Arc<dyn MyTrait>`.
125 /// let coerced: Arc<dyn MyTrait> = obj;
126 /// # Ok::<(), Error>(())
127 /// ```
128 pub struct Arc<T: ?Sized> {
129     ptr: NonNull<ArcInner<T>>,
130     _p: PhantomData<ArcInner<T>>,
131 }
132 
133 #[pin_data]
134 #[repr(C)]
135 struct ArcInner<T: ?Sized> {
136     refcount: Opaque<bindings::refcount_t>,
137     data: T,
138 }
139 
140 // This is to allow [`Arc`] (and variants) to be used as the type of `self`.
141 impl<T: ?Sized> core::ops::Receiver for Arc<T> {}
142 
143 // This is to allow coercion from `Arc<T>` to `Arc<U>` if `T` can be converted to the
144 // dynamically-sized type (DST) `U`.
145 impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
146 
147 // This is to allow `Arc<U>` to be dispatched on when `Arc<T>` can be coerced into `Arc<U>`.
148 impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
149 
150 // SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
151 // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
152 // `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
153 // mutable reference when the reference count reaches zero and `T` is dropped.
154 unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
155 
156 // SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync`
157 // because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
158 // it needs `T` to be `Send` because any thread that has a `&Arc<T>` may clone it and get an
159 // `Arc<T>` on that thread, so the thread may ultimately access `T` using a mutable reference when
160 // the reference count reaches zero and `T` is dropped.
161 unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
162 
163 impl<T> Arc<T> {
164     /// Constructs a new reference counted instance of `T`.
165     pub fn try_new(contents: T) -> Result<Self, AllocError> {
166         // INVARIANT: The refcount is initialised to a non-zero value.
167         let value = ArcInner {
168             // SAFETY: There are no safety requirements for this FFI call.
169             refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
170             data: contents,
171         };
172 
173         let inner = Box::try_new(value)?;
174 
175         // SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
176         // `Arc` object.
177         Ok(unsafe { Self::from_inner(Box::leak(inner).into()) })
178     }
179 
180     /// Use the given initializer to in-place initialize a `T`.
181     ///
182     /// If `T: !Unpin` it will not be able to move afterwards.
183     #[inline]
184     pub fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Self>
185     where
186         Error: From<E>,
187     {
188         UniqueArc::pin_init(init).map(|u| u.into())
189     }
190 
191     /// Use the given initializer to in-place initialize a `T`.
192     ///
193     /// This is equivalent to [`Arc<T>::pin_init`], since an [`Arc`] is always pinned.
194     #[inline]
195     pub fn init<E>(init: impl Init<T, E>) -> error::Result<Self>
196     where
197         Error: From<E>,
198     {
199         UniqueArc::init(init).map(|u| u.into())
200     }
201 }
202 
203 impl<T: ?Sized> Arc<T> {
204     /// Constructs a new [`Arc`] from an existing [`ArcInner`].
205     ///
206     /// # Safety
207     ///
208     /// The caller must ensure that `inner` points to a valid location and has a non-zero reference
209     /// count, one of which will be owned by the new [`Arc`] instance.
210     unsafe fn from_inner(inner: NonNull<ArcInner<T>>) -> Self {
211         // INVARIANT: By the safety requirements, the invariants hold.
212         Arc {
213             ptr: inner,
214             _p: PhantomData,
215         }
216     }
217 
218     /// Convert the [`Arc`] into a raw pointer.
219     ///
220     /// The raw pointer has ownership of the refcount that this Arc object owned.
221     pub fn into_raw(self) -> *const T {
222         let ptr = self.ptr.as_ptr();
223         core::mem::forget(self);
224         // SAFETY: The pointer is valid.
225         unsafe { core::ptr::addr_of!((*ptr).data) }
226     }
227 
228     /// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
229     ///
230     /// # Safety
231     ///
232     /// `ptr` must have been returned by a previous call to [`Arc::into_raw`]. Additionally, it
233     /// must not be called more than once for each previous call to [`Arc::into_raw`].
234     pub unsafe fn from_raw(ptr: *const T) -> Self {
235         let refcount_layout = Layout::new::<bindings::refcount_t>();
236         // SAFETY: The caller guarantees that the pointer is valid.
237         let val_layout = Layout::for_value(unsafe { &*ptr });
238         // SAFETY: We're computing the layout of a real struct that existed when compiling this
239         // binary, so its layout is not so large that it can trigger arithmetic overflow.
240         let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
241 
242         // Pointer casts leave the metadata unchanged. This is okay because the metadata of `T` and
243         // `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as its last field.
244         //
245         // This is documented at:
246         // <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
247         let ptr = ptr as *const ArcInner<T>;
248 
249         // SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the
250         // pointer, since it originates from a previous call to `Arc::into_raw` and is still valid.
251         let ptr = unsafe { ptr.byte_sub(val_offset) };
252 
253         // SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
254         // reference count held then will be owned by the new `Arc` object.
255         unsafe { Self::from_inner(NonNull::new_unchecked(ptr.cast_mut())) }
256     }
257 
258     /// Returns an [`ArcBorrow`] from the given [`Arc`].
259     ///
260     /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
261     /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
262     #[inline]
263     pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
264         // SAFETY: The constraint that the lifetime of the shared reference must outlive that of
265         // the returned `ArcBorrow` ensures that the object remains alive and that no mutable
266         // reference can be created.
267         unsafe { ArcBorrow::new(self.ptr) }
268     }
269 
270     /// Compare whether two [`Arc`] pointers reference the same underlying object.
271     pub fn ptr_eq(this: &Self, other: &Self) -> bool {
272         core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
273     }
274 }
275 
276 impl<T: 'static> ForeignOwnable for Arc<T> {
277     type Borrowed<'a> = ArcBorrow<'a, T>;
278 
279     fn into_foreign(self) -> *const core::ffi::c_void {
280         ManuallyDrop::new(self).ptr.as_ptr() as _
281     }
282 
283     unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
284         // SAFETY: By the safety requirement of this function, we know that `ptr` came from
285         // a previous call to `Arc::into_foreign`.
286         let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
287 
288         // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
289         // for the lifetime of the returned value.
290         unsafe { ArcBorrow::new(inner) }
291     }
292 
293     unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
294         // SAFETY: By the safety requirement of this function, we know that `ptr` came from
295         // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
296         // holds a reference count increment that is transferrable to us.
297         unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) }
298     }
299 }
300 
301 impl<T: ?Sized> Deref for Arc<T> {
302     type Target = T;
303 
304     fn deref(&self) -> &Self::Target {
305         // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
306         // safe to dereference it.
307         unsafe { &self.ptr.as_ref().data }
308     }
309 }
310 
311 impl<T: ?Sized> AsRef<T> for Arc<T> {
312     fn as_ref(&self) -> &T {
313         self.deref()
314     }
315 }
316 
317 impl<T: ?Sized> Clone for Arc<T> {
318     fn clone(&self) -> Self {
319         // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
320         // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
321         // safe to increment the refcount.
322         unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
323 
324         // SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
325         unsafe { Self::from_inner(self.ptr) }
326     }
327 }
328 
329 impl<T: ?Sized> Drop for Arc<T> {
330     fn drop(&mut self) {
331         // SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot
332         // touch `refcount` after it's decremented to a non-zero value because another thread/CPU
333         // may concurrently decrement it to zero and free it. It is ok to have a raw pointer to
334         // freed/invalid memory as long as it is never dereferenced.
335         let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
336 
337         // INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and
338         // this instance is being dropped, so the broken invariant is not observable.
339         // SAFETY: Also by the type invariant, we are allowed to decrement the refcount.
340         let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
341         if is_zero {
342             // The count reached zero, we must free the memory.
343             //
344             // SAFETY: The pointer was initialised from the result of `Box::leak`.
345             unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
346         }
347     }
348 }
349 
350 impl<T: ?Sized> From<UniqueArc<T>> for Arc<T> {
351     fn from(item: UniqueArc<T>) -> Self {
352         item.inner
353     }
354 }
355 
356 impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
357     fn from(item: Pin<UniqueArc<T>>) -> Self {
358         // SAFETY: The type invariants of `Arc` guarantee that the data is pinned.
359         unsafe { Pin::into_inner_unchecked(item).inner }
360     }
361 }
362 
363 /// A borrowed reference to an [`Arc`] instance.
364 ///
365 /// For cases when one doesn't ever need to increment the refcount on the allocation, it is simpler
366 /// to use just `&T`, which we can trivially get from an [`Arc<T>`] instance.
367 ///
368 /// However, when one may need to increment the refcount, it is preferable to use an `ArcBorrow<T>`
369 /// over `&Arc<T>` because the latter results in a double-indirection: a pointer (shared reference)
370 /// to a pointer ([`Arc<T>`]) to the object (`T`). An [`ArcBorrow`] eliminates this double
371 /// indirection while still allowing one to increment the refcount and getting an [`Arc<T>`] when/if
372 /// needed.
373 ///
374 /// # Invariants
375 ///
376 /// There are no mutable references to the underlying [`Arc`], and it remains valid for the
377 /// lifetime of the [`ArcBorrow`] instance.
378 ///
379 /// # Example
380 ///
381 /// ```
382 /// use kernel::sync::{Arc, ArcBorrow};
383 ///
384 /// struct Example;
385 ///
386 /// fn do_something(e: ArcBorrow<'_, Example>) -> Arc<Example> {
387 ///     e.into()
388 /// }
389 ///
390 /// let obj = Arc::try_new(Example)?;
391 /// let cloned = do_something(obj.as_arc_borrow());
392 ///
393 /// // Assert that both `obj` and `cloned` point to the same underlying object.
394 /// assert!(core::ptr::eq(&*obj, &*cloned));
395 /// # Ok::<(), Error>(())
396 /// ```
397 ///
398 /// Using `ArcBorrow<T>` as the type of `self`:
399 ///
400 /// ```
401 /// use kernel::sync::{Arc, ArcBorrow};
402 ///
403 /// struct Example {
404 ///     a: u32,
405 ///     b: u32,
406 /// }
407 ///
408 /// impl Example {
409 ///     fn use_reference(self: ArcBorrow<'_, Self>) {
410 ///         // ...
411 ///     }
412 /// }
413 ///
414 /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
415 /// obj.as_arc_borrow().use_reference();
416 /// # Ok::<(), Error>(())
417 /// ```
418 pub struct ArcBorrow<'a, T: ?Sized + 'a> {
419     inner: NonNull<ArcInner<T>>,
420     _p: PhantomData<&'a ()>,
421 }
422 
423 // This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
424 impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
425 
426 // This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
427 // `ArcBorrow<U>`.
428 impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'_, U>>
429     for ArcBorrow<'_, T>
430 {
431 }
432 
433 impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
434     fn clone(&self) -> Self {
435         *self
436     }
437 }
438 
439 impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
440 
441 impl<T: ?Sized> ArcBorrow<'_, T> {
442     /// Creates a new [`ArcBorrow`] instance.
443     ///
444     /// # Safety
445     ///
446     /// Callers must ensure the following for the lifetime of the returned [`ArcBorrow`] instance:
447     /// 1. That `inner` remains valid;
448     /// 2. That no mutable references to `inner` are created.
449     unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
450         // INVARIANT: The safety requirements guarantee the invariants.
451         Self {
452             inner,
453             _p: PhantomData,
454         }
455     }
456 }
457 
458 impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
459     fn from(b: ArcBorrow<'_, T>) -> Self {
460         // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop`
461         // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the
462         // increment.
463         ManuallyDrop::new(unsafe { Arc::from_inner(b.inner) })
464             .deref()
465             .clone()
466     }
467 }
468 
469 impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
470     type Target = T;
471 
472     fn deref(&self) -> &Self::Target {
473         // SAFETY: By the type invariant, the underlying object is still alive with no mutable
474         // references to it, so it is safe to create a shared reference.
475         unsafe { &self.inner.as_ref().data }
476     }
477 }
478 
479 /// A refcounted object that is known to have a refcount of 1.
480 ///
481 /// It is mutable and can be converted to an [`Arc`] so that it can be shared.
482 ///
483 /// # Invariants
484 ///
485 /// `inner` always has a reference count of 1.
486 ///
487 /// # Examples
488 ///
489 /// In the following example, we make changes to the inner object before turning it into an
490 /// `Arc<Test>` object (after which point, it cannot be mutated directly). Note that `x.into()`
491 /// cannot fail.
492 ///
493 /// ```
494 /// use kernel::sync::{Arc, UniqueArc};
495 ///
496 /// struct Example {
497 ///     a: u32,
498 ///     b: u32,
499 /// }
500 ///
501 /// fn test() -> Result<Arc<Example>> {
502 ///     let mut x = UniqueArc::try_new(Example { a: 10, b: 20 })?;
503 ///     x.a += 1;
504 ///     x.b += 1;
505 ///     Ok(x.into())
506 /// }
507 ///
508 /// # test().unwrap();
509 /// ```
510 ///
511 /// In the following example we first allocate memory for a refcounted `Example` but we don't
512 /// initialise it on allocation. We do initialise it later with a call to [`UniqueArc::write`],
513 /// followed by a conversion to `Arc<Example>`. This is particularly useful when allocation happens
514 /// in one context (e.g., sleepable) and initialisation in another (e.g., atomic):
515 ///
516 /// ```
517 /// use kernel::sync::{Arc, UniqueArc};
518 ///
519 /// struct Example {
520 ///     a: u32,
521 ///     b: u32,
522 /// }
523 ///
524 /// fn test() -> Result<Arc<Example>> {
525 ///     let x = UniqueArc::try_new_uninit()?;
526 ///     Ok(x.write(Example { a: 10, b: 20 }).into())
527 /// }
528 ///
529 /// # test().unwrap();
530 /// ```
531 ///
532 /// In the last example below, the caller gets a pinned instance of `Example` while converting to
533 /// `Arc<Example>`; this is useful in scenarios where one needs a pinned reference during
534 /// initialisation, for example, when initialising fields that are wrapped in locks.
535 ///
536 /// ```
537 /// use kernel::sync::{Arc, UniqueArc};
538 ///
539 /// struct Example {
540 ///     a: u32,
541 ///     b: u32,
542 /// }
543 ///
544 /// fn test() -> Result<Arc<Example>> {
545 ///     let mut pinned = Pin::from(UniqueArc::try_new(Example { a: 10, b: 20 })?);
546 ///     // We can modify `pinned` because it is `Unpin`.
547 ///     pinned.as_mut().a += 1;
548 ///     Ok(pinned.into())
549 /// }
550 ///
551 /// # test().unwrap();
552 /// ```
553 pub struct UniqueArc<T: ?Sized> {
554     inner: Arc<T>,
555 }
556 
557 impl<T> UniqueArc<T> {
558     /// Tries to allocate a new [`UniqueArc`] instance.
559     pub fn try_new(value: T) -> Result<Self, AllocError> {
560         Ok(Self {
561             // INVARIANT: The newly-created object has a refcount of 1.
562             inner: Arc::try_new(value)?,
563         })
564     }
565 
566     /// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
567     pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
568         // INVARIANT: The refcount is initialised to a non-zero value.
569         let inner = Box::try_init::<AllocError>(try_init!(ArcInner {
570             // SAFETY: There are no safety requirements for this FFI call.
571             refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
572             data <- init::uninit::<T, AllocError>(),
573         }? AllocError))?;
574         Ok(UniqueArc {
575             // INVARIANT: The newly-created object has a refcount of 1.
576             // SAFETY: The pointer from the `Box` is valid.
577             inner: unsafe { Arc::from_inner(Box::leak(inner).into()) },
578         })
579     }
580 }
581 
582 impl<T> UniqueArc<MaybeUninit<T>> {
583     /// Converts a `UniqueArc<MaybeUninit<T>>` into a `UniqueArc<T>` by writing a value into it.
584     pub fn write(mut self, value: T) -> UniqueArc<T> {
585         self.deref_mut().write(value);
586         // SAFETY: We just wrote the value to be initialized.
587         unsafe { self.assume_init() }
588     }
589 
590     /// Unsafely assume that `self` is initialized.
591     ///
592     /// # Safety
593     ///
594     /// The caller guarantees that the value behind this pointer has been initialized. It is
595     /// *immediate* UB to call this when the value is not initialized.
596     pub unsafe fn assume_init(self) -> UniqueArc<T> {
597         let inner = ManuallyDrop::new(self).inner.ptr;
598         UniqueArc {
599             // SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be
600             // dropped). The types are compatible because `MaybeUninit<T>` is compatible with `T`.
601             inner: unsafe { Arc::from_inner(inner.cast()) },
602         }
603     }
604 
605     /// Initialize `self` using the given initializer.
606     pub fn init_with<E>(mut self, init: impl Init<T, E>) -> core::result::Result<UniqueArc<T>, E> {
607         // SAFETY: The supplied pointer is valid for initialization.
608         match unsafe { init.__init(self.as_mut_ptr()) } {
609             // SAFETY: Initialization completed successfully.
610             Ok(()) => Ok(unsafe { self.assume_init() }),
611             Err(err) => Err(err),
612         }
613     }
614 
615     /// Pin-initialize `self` using the given pin-initializer.
616     pub fn pin_init_with<E>(
617         mut self,
618         init: impl PinInit<T, E>,
619     ) -> core::result::Result<Pin<UniqueArc<T>>, E> {
620         // SAFETY: The supplied pointer is valid for initialization and we will later pin the value
621         // to ensure it does not move.
622         match unsafe { init.__pinned_init(self.as_mut_ptr()) } {
623             // SAFETY: Initialization completed successfully.
624             Ok(()) => Ok(unsafe { self.assume_init() }.into()),
625             Err(err) => Err(err),
626         }
627     }
628 }
629 
630 impl<T: ?Sized> From<UniqueArc<T>> for Pin<UniqueArc<T>> {
631     fn from(obj: UniqueArc<T>) -> Self {
632         // SAFETY: It is not possible to move/replace `T` inside a `Pin<UniqueArc<T>>` (unless `T`
633         // is `Unpin`), so it is ok to convert it to `Pin<UniqueArc<T>>`.
634         unsafe { Pin::new_unchecked(obj) }
635     }
636 }
637 
638 impl<T: ?Sized> Deref for UniqueArc<T> {
639     type Target = T;
640 
641     fn deref(&self) -> &Self::Target {
642         self.inner.deref()
643     }
644 }
645 
646 impl<T: ?Sized> DerefMut for UniqueArc<T> {
647     fn deref_mut(&mut self) -> &mut Self::Target {
648         // SAFETY: By the `Arc` type invariant, there is necessarily a reference to the object, so
649         // it is safe to dereference it. Additionally, we know there is only one reference when
650         // it's inside a `UniqueArc`, so it is safe to get a mutable reference.
651         unsafe { &mut self.inner.ptr.as_mut().data }
652     }
653 }
654 
655 impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
656     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
657         fmt::Display::fmt(self.deref(), f)
658     }
659 }
660 
661 impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
662     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
663         fmt::Display::fmt(self.deref(), f)
664     }
665 }
666 
667 impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
668     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
669         fmt::Debug::fmt(self.deref(), f)
670     }
671 }
672 
673 impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {
674     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
675         fmt::Debug::fmt(self.deref(), f)
676     }
677 }
678