alloc/sync.rs
1#![stable(feature = "rust1", since = "1.0.0")]
2
3//! Thread-safe reference-counting pointers.
4//!
5//! See the [`Arc<T>`][Arc] documentation for more details.
6//!
7//! **Note**: This module is only available on platforms that support atomic
8//! loads and stores of pointers. This may be detected at compile time using
9//! `#[cfg(target_has_atomic = "ptr")]`.
10
11use core::any::Any;
12#[cfg(not(no_global_oom_handling))]
13use core::clone::CloneToUninit;
14use core::cmp::Ordering;
15use core::hash::{Hash, Hasher};
16use core::intrinsics::abort;
17#[cfg(not(no_global_oom_handling))]
18use core::iter;
19use core::marker::{PhantomData, Unsize};
20use core::mem::{self, ManuallyDrop, align_of_val_raw};
21use core::num::NonZeroUsize;
22use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, LegacyReceiver};
23use core::panic::{RefUnwindSafe, UnwindSafe};
24use core::pin::{Pin, PinCoerceUnsized};
25use core::ptr::{self, NonNull};
26#[cfg(not(no_global_oom_handling))]
27use core::slice::from_raw_parts_mut;
28use core::sync::atomic;
29use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
30use core::{borrow, fmt, hint};
31
32#[cfg(not(no_global_oom_handling))]
33use crate::alloc::handle_alloc_error;
34use crate::alloc::{AllocError, Allocator, Global, Layout};
35use crate::borrow::{Cow, ToOwned};
36use crate::boxed::Box;
37use crate::rc::is_dangling;
38#[cfg(not(no_global_oom_handling))]
39use crate::string::String;
40#[cfg(not(no_global_oom_handling))]
41use crate::vec::Vec;
42
43/// A soft limit on the amount of references that may be made to an `Arc`.
44///
45/// Going above this limit will abort your program (although not
46/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
47/// Trying to go above it might call a `panic` (if not actually going above it).
48///
49/// This is a global invariant, and also applies when using a compare-exchange loop.
50///
51/// See comment in `Arc::clone`.
52const MAX_REFCOUNT: usize = (isize::MAX) as usize;
53
54/// The error in case either counter reaches above `MAX_REFCOUNT`, and we can `panic` safely.
55const INTERNAL_OVERFLOW_ERROR: &str = "Arc counter overflow";
56
57#[cfg(not(sanitize = "thread"))]
58macro_rules! acquire {
59 ($x:expr) => {
60 atomic::fence(Acquire)
61 };
62}
63
64// ThreadSanitizer does not support memory fences. To avoid false positive
65// reports in Arc / Weak implementation use atomic loads for synchronization
66// instead.
67#[cfg(sanitize = "thread")]
68macro_rules! acquire {
69 ($x:expr) => {
70 $x.load(Acquire)
71 };
72}
73
74/// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
75/// Reference Counted'.
76///
77/// The type `Arc<T>` provides shared ownership of a value of type `T`,
78/// allocated in the heap. Invoking [`clone`][clone] on `Arc` produces
79/// a new `Arc` instance, which points to the same allocation on the heap as the
80/// source `Arc`, while increasing a reference count. When the last `Arc`
81/// pointer to a given allocation is destroyed, the value stored in that allocation (often
82/// referred to as "inner value") is also dropped.
83///
84/// Shared references in Rust disallow mutation by default, and `Arc` is no
85/// exception: you cannot generally obtain a mutable reference to something
86/// inside an `Arc`. If you need to mutate through an `Arc`, use
87/// [`Mutex`][mutex], [`RwLock`][rwlock], or one of the [`Atomic`][atomic]
88/// types.
89///
90/// **Note**: This type is only available on platforms that support atomic
91/// loads and stores of pointers, which includes all platforms that support
92/// the `std` crate but not all those which only support [`alloc`](crate).
93/// This may be detected at compile time using `#[cfg(target_has_atomic = "ptr")]`.
94///
95/// ## Thread Safety
96///
97/// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
98/// counting. This means that it is thread-safe. The disadvantage is that
99/// atomic operations are more expensive than ordinary memory accesses. If you
100/// are not sharing reference-counted allocations between threads, consider using
101/// [`Rc<T>`] for lower overhead. [`Rc<T>`] is a safe default, because the
102/// compiler will catch any attempt to send an [`Rc<T>`] between threads.
103/// However, a library might choose `Arc<T>` in order to give library consumers
104/// more flexibility.
105///
106/// `Arc<T>` will implement [`Send`] and [`Sync`] as long as the `T` implements
107/// [`Send`] and [`Sync`]. Why can't you put a non-thread-safe type `T` in an
108/// `Arc<T>` to make it thread-safe? This may be a bit counter-intuitive at
109/// first: after all, isn't the point of `Arc<T>` thread safety? The key is
110/// this: `Arc<T>` makes it thread safe to have multiple ownership of the same
111/// data, but it doesn't add thread safety to its data. Consider
112/// <code>Arc<[RefCell\<T>]></code>. [`RefCell<T>`] isn't [`Sync`], and if `Arc<T>` was always
113/// [`Send`], <code>Arc<[RefCell\<T>]></code> would be as well. But then we'd have a problem:
114/// [`RefCell<T>`] is not thread safe; it keeps track of the borrowing count using
115/// non-atomic operations.
116///
117/// In the end, this means that you may need to pair `Arc<T>` with some sort of
118/// [`std::sync`] type, usually [`Mutex<T>`][mutex].
119///
120/// ## Breaking cycles with `Weak`
121///
122/// The [`downgrade`][downgrade] method can be used to create a non-owning
123/// [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
124/// to an `Arc`, but this will return [`None`] if the value stored in the allocation has
125/// already been dropped. In other words, `Weak` pointers do not keep the value
126/// inside the allocation alive; however, they *do* keep the allocation
127/// (the backing store for the value) alive.
128///
129/// A cycle between `Arc` pointers will never be deallocated. For this reason,
130/// [`Weak`] is used to break cycles. For example, a tree could have
131/// strong `Arc` pointers from parent nodes to children, and [`Weak`]
132/// pointers from children back to their parents.
133///
134/// # Cloning references
135///
136/// Creating a new reference from an existing reference-counted pointer is done using the
137/// `Clone` trait implemented for [`Arc<T>`][Arc] and [`Weak<T>`][Weak].
138///
139/// ```
140/// use std::sync::Arc;
141/// let foo = Arc::new(vec![1.0, 2.0, 3.0]);
142/// // The two syntaxes below are equivalent.
143/// let a = foo.clone();
144/// let b = Arc::clone(&foo);
145/// // a, b, and foo are all Arcs that point to the same memory location
146/// ```
147///
148/// ## `Deref` behavior
149///
150/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
151/// so you can call `T`'s methods on a value of type `Arc<T>`. To avoid name
152/// clashes with `T`'s methods, the methods of `Arc<T>` itself are associated
153/// functions, called using [fully qualified syntax]:
154///
155/// ```
156/// use std::sync::Arc;
157///
158/// let my_arc = Arc::new(());
159/// let my_weak = Arc::downgrade(&my_arc);
160/// ```
161///
162/// `Arc<T>`'s implementations of traits like `Clone` may also be called using
163/// fully qualified syntax. Some people prefer to use fully qualified syntax,
164/// while others prefer using method-call syntax.
165///
166/// ```
167/// use std::sync::Arc;
168///
169/// let arc = Arc::new(());
170/// // Method-call syntax
171/// let arc2 = arc.clone();
172/// // Fully qualified syntax
173/// let arc3 = Arc::clone(&arc);
174/// ```
175///
176/// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
177/// already been dropped.
178///
179/// [`Rc<T>`]: crate::rc::Rc
180/// [clone]: Clone::clone
181/// [mutex]: ../../std/sync/struct.Mutex.html
182/// [rwlock]: ../../std/sync/struct.RwLock.html
183/// [atomic]: core::sync::atomic
184/// [downgrade]: Arc::downgrade
185/// [upgrade]: Weak::upgrade
186/// [RefCell\<T>]: core::cell::RefCell
187/// [`RefCell<T>`]: core::cell::RefCell
188/// [`std::sync`]: ../../std/sync/index.html
189/// [`Arc::clone(&from)`]: Arc::clone
190/// [fully qualified syntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name
191///
192/// # Examples
193///
194/// Sharing some immutable data between threads:
195///
196/// ```
197/// use std::sync::Arc;
198/// use std::thread;
199///
200/// let five = Arc::new(5);
201///
202/// for _ in 0..10 {
203/// let five = Arc::clone(&five);
204///
205/// thread::spawn(move || {
206/// println!("{five:?}");
207/// });
208/// }
209/// ```
210///
211/// Sharing a mutable [`AtomicUsize`]:
212///
213/// [`AtomicUsize`]: core::sync::atomic::AtomicUsize "sync::atomic::AtomicUsize"
214///
215/// ```
216/// use std::sync::Arc;
217/// use std::sync::atomic::{AtomicUsize, Ordering};
218/// use std::thread;
219///
220/// let val = Arc::new(AtomicUsize::new(5));
221///
222/// for _ in 0..10 {
223/// let val = Arc::clone(&val);
224///
225/// thread::spawn(move || {
226/// let v = val.fetch_add(1, Ordering::Relaxed);
227/// println!("{v:?}");
228/// });
229/// }
230/// ```
231///
232/// See the [`rc` documentation][rc_examples] for more examples of reference
233/// counting in general.
234///
235/// [rc_examples]: crate::rc#examples
236#[doc(search_unbox)]
237#[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
238#[stable(feature = "rust1", since = "1.0.0")]
239#[rustc_insignificant_dtor]
240pub struct Arc<
241 T: ?Sized,
242 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
243> {
244 ptr: NonNull<ArcInner<T>>,
245 phantom: PhantomData<ArcInner<T>>,
246 alloc: A,
247}
248
249#[stable(feature = "rust1", since = "1.0.0")]
250unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Send> Send for Arc<T, A> {}
251#[stable(feature = "rust1", since = "1.0.0")]
252unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Sync> Sync for Arc<T, A> {}
253
254#[stable(feature = "catch_unwind", since = "1.9.0")]
255impl<T: RefUnwindSafe + ?Sized, A: Allocator + UnwindSafe> UnwindSafe for Arc<T, A> {}
256
257#[unstable(feature = "coerce_unsized", issue = "18598")]
258impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Arc<U, A>> for Arc<T, A> {}
259
260#[unstable(feature = "dispatch_from_dyn", issue = "none")]
261impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T> {}
262
263impl<T: ?Sized> Arc<T> {
264 unsafe fn from_inner(ptr: NonNull<ArcInner<T>>) -> Self {
265 unsafe { Self::from_inner_in(ptr, Global) }
266 }
267
268 unsafe fn from_ptr(ptr: *mut ArcInner<T>) -> Self {
269 unsafe { Self::from_ptr_in(ptr, Global) }
270 }
271}
272
273impl<T: ?Sized, A: Allocator> Arc<T, A> {
274 #[inline]
275 fn into_inner_with_allocator(this: Self) -> (NonNull<ArcInner<T>>, A) {
276 let this = mem::ManuallyDrop::new(this);
277 (this.ptr, unsafe { ptr::read(&this.alloc) })
278 }
279
280 #[inline]
281 unsafe fn from_inner_in(ptr: NonNull<ArcInner<T>>, alloc: A) -> Self {
282 Self { ptr, phantom: PhantomData, alloc }
283 }
284
285 #[inline]
286 unsafe fn from_ptr_in(ptr: *mut ArcInner<T>, alloc: A) -> Self {
287 unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) }
288 }
289}
290
291/// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
292/// managed allocation.
293///
294/// The allocation is accessed by calling [`upgrade`] on the `Weak`
295/// pointer, which returns an <code>[Option]<[Arc]\<T>></code>.
296///
297/// Since a `Weak` reference does not count towards ownership, it will not
298/// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
299/// guarantees about the value still being present. Thus it may return [`None`]
300/// when [`upgrade`]d. Note however that a `Weak` reference *does* prevent the allocation
301/// itself (the backing store) from being deallocated.
302///
303/// A `Weak` pointer is useful for keeping a temporary reference to the allocation
304/// managed by [`Arc`] without preventing its inner value from being dropped. It is also used to
305/// prevent circular references between [`Arc`] pointers, since mutual owning references
306/// would never allow either [`Arc`] to be dropped. For example, a tree could
307/// have strong [`Arc`] pointers from parent nodes to children, and `Weak`
308/// pointers from children back to their parents.
309///
310/// The typical way to obtain a `Weak` pointer is to call [`Arc::downgrade`].
311///
312/// [`upgrade`]: Weak::upgrade
313#[stable(feature = "arc_weak", since = "1.4.0")]
314#[cfg_attr(not(test), rustc_diagnostic_item = "ArcWeak")]
315pub struct Weak<
316 T: ?Sized,
317 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
318> {
319 // This is a `NonNull` to allow optimizing the size of this type in enums,
320 // but it is not necessarily a valid pointer.
321 // `Weak::new` sets this to `usize::MAX` so that it doesn’t need
322 // to allocate space on the heap. That's not a value a real pointer
323 // will ever have because RcInner has alignment at least 2.
324 // This is only possible when `T: Sized`; unsized `T` never dangle.
325 ptr: NonNull<ArcInner<T>>,
326 alloc: A,
327}
328
329#[stable(feature = "arc_weak", since = "1.4.0")]
330unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Send> Send for Weak<T, A> {}
331#[stable(feature = "arc_weak", since = "1.4.0")]
332unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Sync> Sync for Weak<T, A> {}
333
334#[unstable(feature = "coerce_unsized", issue = "18598")]
335impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Weak<U, A>> for Weak<T, A> {}
336#[unstable(feature = "dispatch_from_dyn", issue = "none")]
337impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Weak<U>> for Weak<T> {}
338
339#[stable(feature = "arc_weak", since = "1.4.0")]
340impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
341 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
342 write!(f, "(Weak)")
343 }
344}
345
346// This is repr(C) to future-proof against possible field-reordering, which
347// would interfere with otherwise safe [into|from]_raw() of transmutable
348// inner types.
349#[repr(C)]
350struct ArcInner<T: ?Sized> {
351 strong: atomic::AtomicUsize,
352
353 // the value usize::MAX acts as a sentinel for temporarily "locking" the
354 // ability to upgrade weak pointers or downgrade strong ones; this is used
355 // to avoid races in `make_mut` and `get_mut`.
356 weak: atomic::AtomicUsize,
357
358 data: T,
359}
360
361/// Calculate layout for `ArcInner<T>` using the inner value's layout
362fn arcinner_layout_for_value_layout(layout: Layout) -> Layout {
363 // Calculate layout using the given value layout.
364 // Previously, layout was calculated on the expression
365 // `&*(ptr as *const ArcInner<T>)`, but this created a misaligned
366 // reference (see #54908).
367 Layout::new::<ArcInner<()>>().extend(layout).unwrap().0.pad_to_align()
368}
369
370unsafe impl<T: ?Sized + Sync + Send> Send for ArcInner<T> {}
371unsafe impl<T: ?Sized + Sync + Send> Sync for ArcInner<T> {}
372
373impl<T> Arc<T> {
374 /// Constructs a new `Arc<T>`.
375 ///
376 /// # Examples
377 ///
378 /// ```
379 /// use std::sync::Arc;
380 ///
381 /// let five = Arc::new(5);
382 /// ```
383 #[cfg(not(no_global_oom_handling))]
384 #[inline]
385 #[stable(feature = "rust1", since = "1.0.0")]
386 pub fn new(data: T) -> Arc<T> {
387 // Start the weak pointer count as 1 which is the weak pointer that's
388 // held by all the strong pointers (kinda), see std/rc.rs for more info
389 let x: Box<_> = Box::new(ArcInner {
390 strong: atomic::AtomicUsize::new(1),
391 weak: atomic::AtomicUsize::new(1),
392 data,
393 });
394 unsafe { Self::from_inner(Box::leak(x).into()) }
395 }
396
397 /// Constructs a new `Arc<T>` while giving you a `Weak<T>` to the allocation,
398 /// to allow you to construct a `T` which holds a weak pointer to itself.
399 ///
400 /// Generally, a structure circularly referencing itself, either directly or
401 /// indirectly, should not hold a strong reference to itself to prevent a memory leak.
402 /// Using this function, you get access to the weak pointer during the
403 /// initialization of `T`, before the `Arc<T>` is created, such that you can
404 /// clone and store it inside the `T`.
405 ///
406 /// `new_cyclic` first allocates the managed allocation for the `Arc<T>`,
407 /// then calls your closure, giving it a `Weak<T>` to this allocation,
408 /// and only afterwards completes the construction of the `Arc<T>` by placing
409 /// the `T` returned from your closure into the allocation.
410 ///
411 /// Since the new `Arc<T>` is not fully-constructed until `Arc<T>::new_cyclic`
412 /// returns, calling [`upgrade`] on the weak reference inside your closure will
413 /// fail and result in a `None` value.
414 ///
415 /// # Panics
416 ///
417 /// If `data_fn` panics, the panic is propagated to the caller, and the
418 /// temporary [`Weak<T>`] is dropped normally.
419 ///
420 /// # Example
421 ///
422 /// ```
423 /// # #![allow(dead_code)]
424 /// use std::sync::{Arc, Weak};
425 ///
426 /// struct Gadget {
427 /// me: Weak<Gadget>,
428 /// }
429 ///
430 /// impl Gadget {
431 /// /// Constructs a reference counted Gadget.
432 /// fn new() -> Arc<Self> {
433 /// // `me` is a `Weak<Gadget>` pointing at the new allocation of the
434 /// // `Arc` we're constructing.
435 /// Arc::new_cyclic(|me| {
436 /// // Create the actual struct here.
437 /// Gadget { me: me.clone() }
438 /// })
439 /// }
440 ///
441 /// /// Returns a reference counted pointer to Self.
442 /// fn me(&self) -> Arc<Self> {
443 /// self.me.upgrade().unwrap()
444 /// }
445 /// }
446 /// ```
447 /// [`upgrade`]: Weak::upgrade
448 #[cfg(not(no_global_oom_handling))]
449 #[inline]
450 #[stable(feature = "arc_new_cyclic", since = "1.60.0")]
451 pub fn new_cyclic<F>(data_fn: F) -> Arc<T>
452 where
453 F: FnOnce(&Weak<T>) -> T,
454 {
455 Self::new_cyclic_in(data_fn, Global)
456 }
457
458 /// Constructs a new `Arc` with uninitialized contents.
459 ///
460 /// # Examples
461 ///
462 /// ```
463 /// #![feature(get_mut_unchecked)]
464 ///
465 /// use std::sync::Arc;
466 ///
467 /// let mut five = Arc::<u32>::new_uninit();
468 ///
469 /// // Deferred initialization:
470 /// Arc::get_mut(&mut five).unwrap().write(5);
471 ///
472 /// let five = unsafe { five.assume_init() };
473 ///
474 /// assert_eq!(*five, 5)
475 /// ```
476 #[cfg(not(no_global_oom_handling))]
477 #[inline]
478 #[stable(feature = "new_uninit", since = "1.82.0")]
479 #[must_use]
480 pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
481 unsafe {
482 Arc::from_ptr(Arc::allocate_for_layout(
483 Layout::new::<T>(),
484 |layout| Global.allocate(layout),
485 <*mut u8>::cast,
486 ))
487 }
488 }
489
490 /// Constructs a new `Arc` with uninitialized contents, with the memory
491 /// being filled with `0` bytes.
492 ///
493 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
494 /// of this method.
495 ///
496 /// # Examples
497 ///
498 /// ```
499 /// #![feature(new_zeroed_alloc)]
500 ///
501 /// use std::sync::Arc;
502 ///
503 /// let zero = Arc::<u32>::new_zeroed();
504 /// let zero = unsafe { zero.assume_init() };
505 ///
506 /// assert_eq!(*zero, 0)
507 /// ```
508 ///
509 /// [zeroed]: mem::MaybeUninit::zeroed
510 #[cfg(not(no_global_oom_handling))]
511 #[inline]
512 #[unstable(feature = "new_zeroed_alloc", issue = "129396")]
513 #[must_use]
514 pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
515 unsafe {
516 Arc::from_ptr(Arc::allocate_for_layout(
517 Layout::new::<T>(),
518 |layout| Global.allocate_zeroed(layout),
519 <*mut u8>::cast,
520 ))
521 }
522 }
523
524 /// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
525 /// `data` will be pinned in memory and unable to be moved.
526 #[cfg(not(no_global_oom_handling))]
527 #[stable(feature = "pin", since = "1.33.0")]
528 #[must_use]
529 pub fn pin(data: T) -> Pin<Arc<T>> {
530 unsafe { Pin::new_unchecked(Arc::new(data)) }
531 }
532
533 /// Constructs a new `Pin<Arc<T>>`, return an error if allocation fails.
534 #[unstable(feature = "allocator_api", issue = "32838")]
535 #[inline]
536 pub fn try_pin(data: T) -> Result<Pin<Arc<T>>, AllocError> {
537 unsafe { Ok(Pin::new_unchecked(Arc::try_new(data)?)) }
538 }
539
540 /// Constructs a new `Arc<T>`, returning an error if allocation fails.
541 ///
542 /// # Examples
543 ///
544 /// ```
545 /// #![feature(allocator_api)]
546 /// use std::sync::Arc;
547 ///
548 /// let five = Arc::try_new(5)?;
549 /// # Ok::<(), std::alloc::AllocError>(())
550 /// ```
551 #[unstable(feature = "allocator_api", issue = "32838")]
552 #[inline]
553 pub fn try_new(data: T) -> Result<Arc<T>, AllocError> {
554 // Start the weak pointer count as 1 which is the weak pointer that's
555 // held by all the strong pointers (kinda), see std/rc.rs for more info
556 let x: Box<_> = Box::try_new(ArcInner {
557 strong: atomic::AtomicUsize::new(1),
558 weak: atomic::AtomicUsize::new(1),
559 data,
560 })?;
561 unsafe { Ok(Self::from_inner(Box::leak(x).into())) }
562 }
563
564 /// Constructs a new `Arc` with uninitialized contents, returning an error
565 /// if allocation fails.
566 ///
567 /// # Examples
568 ///
569 /// ```
570 /// #![feature(allocator_api)]
571 /// #![feature(get_mut_unchecked)]
572 ///
573 /// use std::sync::Arc;
574 ///
575 /// let mut five = Arc::<u32>::try_new_uninit()?;
576 ///
577 /// // Deferred initialization:
578 /// Arc::get_mut(&mut five).unwrap().write(5);
579 ///
580 /// let five = unsafe { five.assume_init() };
581 ///
582 /// assert_eq!(*five, 5);
583 /// # Ok::<(), std::alloc::AllocError>(())
584 /// ```
585 #[unstable(feature = "allocator_api", issue = "32838")]
586 // #[unstable(feature = "new_uninit", issue = "63291")]
587 pub fn try_new_uninit() -> Result<Arc<mem::MaybeUninit<T>>, AllocError> {
588 unsafe {
589 Ok(Arc::from_ptr(Arc::try_allocate_for_layout(
590 Layout::new::<T>(),
591 |layout| Global.allocate(layout),
592 <*mut u8>::cast,
593 )?))
594 }
595 }
596
597 /// Constructs a new `Arc` with uninitialized contents, with the memory
598 /// being filled with `0` bytes, returning an error if allocation fails.
599 ///
600 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
601 /// of this method.
602 ///
603 /// # Examples
604 ///
605 /// ```
606 /// #![feature( allocator_api)]
607 ///
608 /// use std::sync::Arc;
609 ///
610 /// let zero = Arc::<u32>::try_new_zeroed()?;
611 /// let zero = unsafe { zero.assume_init() };
612 ///
613 /// assert_eq!(*zero, 0);
614 /// # Ok::<(), std::alloc::AllocError>(())
615 /// ```
616 ///
617 /// [zeroed]: mem::MaybeUninit::zeroed
618 #[unstable(feature = "allocator_api", issue = "32838")]
619 // #[unstable(feature = "new_uninit", issue = "63291")]
620 pub fn try_new_zeroed() -> Result<Arc<mem::MaybeUninit<T>>, AllocError> {
621 unsafe {
622 Ok(Arc::from_ptr(Arc::try_allocate_for_layout(
623 Layout::new::<T>(),
624 |layout| Global.allocate_zeroed(layout),
625 <*mut u8>::cast,
626 )?))
627 }
628 }
629}
630
631impl<T, A: Allocator> Arc<T, A> {
632 /// Constructs a new `Arc<T>` in the provided allocator.
633 ///
634 /// # Examples
635 ///
636 /// ```
637 /// #![feature(allocator_api)]
638 ///
639 /// use std::sync::Arc;
640 /// use std::alloc::System;
641 ///
642 /// let five = Arc::new_in(5, System);
643 /// ```
644 #[inline]
645 #[cfg(not(no_global_oom_handling))]
646 #[unstable(feature = "allocator_api", issue = "32838")]
647 pub fn new_in(data: T, alloc: A) -> Arc<T, A> {
648 // Start the weak pointer count as 1 which is the weak pointer that's
649 // held by all the strong pointers (kinda), see std/rc.rs for more info
650 let x = Box::new_in(
651 ArcInner {
652 strong: atomic::AtomicUsize::new(1),
653 weak: atomic::AtomicUsize::new(1),
654 data,
655 },
656 alloc,
657 );
658 let (ptr, alloc) = Box::into_unique(x);
659 unsafe { Self::from_inner_in(ptr.into(), alloc) }
660 }
661
662 /// Constructs a new `Arc` with uninitialized contents in the provided allocator.
663 ///
664 /// # Examples
665 ///
666 /// ```
667 /// #![feature(get_mut_unchecked)]
668 /// #![feature(allocator_api)]
669 ///
670 /// use std::sync::Arc;
671 /// use std::alloc::System;
672 ///
673 /// let mut five = Arc::<u32, _>::new_uninit_in(System);
674 ///
675 /// let five = unsafe {
676 /// // Deferred initialization:
677 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
678 ///
679 /// five.assume_init()
680 /// };
681 ///
682 /// assert_eq!(*five, 5)
683 /// ```
684 #[cfg(not(no_global_oom_handling))]
685 #[unstable(feature = "allocator_api", issue = "32838")]
686 // #[unstable(feature = "new_uninit", issue = "63291")]
687 #[inline]
688 pub fn new_uninit_in(alloc: A) -> Arc<mem::MaybeUninit<T>, A> {
689 unsafe {
690 Arc::from_ptr_in(
691 Arc::allocate_for_layout(
692 Layout::new::<T>(),
693 |layout| alloc.allocate(layout),
694 <*mut u8>::cast,
695 ),
696 alloc,
697 )
698 }
699 }
700
701 /// Constructs a new `Arc` with uninitialized contents, with the memory
702 /// being filled with `0` bytes, in the provided allocator.
703 ///
704 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
705 /// of this method.
706 ///
707 /// # Examples
708 ///
709 /// ```
710 /// #![feature(allocator_api)]
711 ///
712 /// use std::sync::Arc;
713 /// use std::alloc::System;
714 ///
715 /// let zero = Arc::<u32, _>::new_zeroed_in(System);
716 /// let zero = unsafe { zero.assume_init() };
717 ///
718 /// assert_eq!(*zero, 0)
719 /// ```
720 ///
721 /// [zeroed]: mem::MaybeUninit::zeroed
722 #[cfg(not(no_global_oom_handling))]
723 #[unstable(feature = "allocator_api", issue = "32838")]
724 // #[unstable(feature = "new_uninit", issue = "63291")]
725 #[inline]
726 pub fn new_zeroed_in(alloc: A) -> Arc<mem::MaybeUninit<T>, A> {
727 unsafe {
728 Arc::from_ptr_in(
729 Arc::allocate_for_layout(
730 Layout::new::<T>(),
731 |layout| alloc.allocate_zeroed(layout),
732 <*mut u8>::cast,
733 ),
734 alloc,
735 )
736 }
737 }
738
739 /// Constructs a new `Arc<T, A>` in the given allocator while giving you a `Weak<T, A>` to the allocation,
740 /// to allow you to construct a `T` which holds a weak pointer to itself.
741 ///
742 /// Generally, a structure circularly referencing itself, either directly or
743 /// indirectly, should not hold a strong reference to itself to prevent a memory leak.
744 /// Using this function, you get access to the weak pointer during the
745 /// initialization of `T`, before the `Arc<T, A>` is created, such that you can
746 /// clone and store it inside the `T`.
747 ///
748 /// `new_cyclic_in` first allocates the managed allocation for the `Arc<T, A>`,
749 /// then calls your closure, giving it a `Weak<T, A>` to this allocation,
750 /// and only afterwards completes the construction of the `Arc<T, A>` by placing
751 /// the `T` returned from your closure into the allocation.
752 ///
753 /// Since the new `Arc<T, A>` is not fully-constructed until `Arc<T, A>::new_cyclic_in`
754 /// returns, calling [`upgrade`] on the weak reference inside your closure will
755 /// fail and result in a `None` value.
756 ///
757 /// # Panics
758 ///
759 /// If `data_fn` panics, the panic is propagated to the caller, and the
760 /// temporary [`Weak<T>`] is dropped normally.
761 ///
762 /// # Example
763 ///
764 /// See [`new_cyclic`]
765 ///
766 /// [`new_cyclic`]: Arc::new_cyclic
767 /// [`upgrade`]: Weak::upgrade
768 #[cfg(not(no_global_oom_handling))]
769 #[inline]
770 #[unstable(feature = "allocator_api", issue = "32838")]
771 pub fn new_cyclic_in<F>(data_fn: F, alloc: A) -> Arc<T, A>
772 where
773 F: FnOnce(&Weak<T, A>) -> T,
774 {
775 // Construct the inner in the "uninitialized" state with a single
776 // weak reference.
777 let (uninit_raw_ptr, alloc) = Box::into_raw_with_allocator(Box::new_in(
778 ArcInner {
779 strong: atomic::AtomicUsize::new(0),
780 weak: atomic::AtomicUsize::new(1),
781 data: mem::MaybeUninit::<T>::uninit(),
782 },
783 alloc,
784 ));
785 let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
786 let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast();
787
788 let weak = Weak { ptr: init_ptr, alloc };
789
790 // It's important we don't give up ownership of the weak pointer, or
791 // else the memory might be freed by the time `data_fn` returns. If
792 // we really wanted to pass ownership, we could create an additional
793 // weak pointer for ourselves, but this would result in additional
794 // updates to the weak reference count which might not be necessary
795 // otherwise.
796 let data = data_fn(&weak);
797
798 // Now we can properly initialize the inner value and turn our weak
799 // reference into a strong reference.
800 let strong = unsafe {
801 let inner = init_ptr.as_ptr();
802 ptr::write(&raw mut (*inner).data, data);
803
804 // The above write to the data field must be visible to any threads which
805 // observe a non-zero strong count. Therefore we need at least "Release" ordering
806 // in order to synchronize with the `compare_exchange_weak` in `Weak::upgrade`.
807 //
808 // "Acquire" ordering is not required. When considering the possible behaviors
809 // of `data_fn` we only need to look at what it could do with a reference to a
810 // non-upgradeable `Weak`:
811 // - It can *clone* the `Weak`, increasing the weak reference count.
812 // - It can drop those clones, decreasing the weak reference count (but never to zero).
813 //
814 // These side effects do not impact us in any way, and no other side effects are
815 // possible with safe code alone.
816 let prev_value = (*inner).strong.fetch_add(1, Release);
817 debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
818
819 // Strong references should collectively own a shared weak reference,
820 // so don't run the destructor for our old weak reference.
821 // Calling into_raw_with_allocator has the double effect of giving us back the allocator,
822 // and forgetting the weak reference.
823 let alloc = weak.into_raw_with_allocator().1;
824
825 Arc::from_inner_in(init_ptr, alloc)
826 };
827
828 strong
829 }
830
831 /// Constructs a new `Pin<Arc<T, A>>` in the provided allocator. If `T` does not implement `Unpin`,
832 /// then `data` will be pinned in memory and unable to be moved.
833 #[cfg(not(no_global_oom_handling))]
834 #[unstable(feature = "allocator_api", issue = "32838")]
835 #[inline]
836 pub fn pin_in(data: T, alloc: A) -> Pin<Arc<T, A>>
837 where
838 A: 'static,
839 {
840 unsafe { Pin::new_unchecked(Arc::new_in(data, alloc)) }
841 }
842
843 /// Constructs a new `Pin<Arc<T, A>>` in the provided allocator, return an error if allocation
844 /// fails.
845 #[inline]
846 #[unstable(feature = "allocator_api", issue = "32838")]
847 pub fn try_pin_in(data: T, alloc: A) -> Result<Pin<Arc<T, A>>, AllocError>
848 where
849 A: 'static,
850 {
851 unsafe { Ok(Pin::new_unchecked(Arc::try_new_in(data, alloc)?)) }
852 }
853
854 /// Constructs a new `Arc<T, A>` in the provided allocator, returning an error if allocation fails.
855 ///
856 /// # Examples
857 ///
858 /// ```
859 /// #![feature(allocator_api)]
860 ///
861 /// use std::sync::Arc;
862 /// use std::alloc::System;
863 ///
864 /// let five = Arc::try_new_in(5, System)?;
865 /// # Ok::<(), std::alloc::AllocError>(())
866 /// ```
867 #[inline]
868 #[unstable(feature = "allocator_api", issue = "32838")]
869 #[inline]
870 pub fn try_new_in(data: T, alloc: A) -> Result<Arc<T, A>, AllocError> {
871 // Start the weak pointer count as 1 which is the weak pointer that's
872 // held by all the strong pointers (kinda), see std/rc.rs for more info
873 let x = Box::try_new_in(
874 ArcInner {
875 strong: atomic::AtomicUsize::new(1),
876 weak: atomic::AtomicUsize::new(1),
877 data,
878 },
879 alloc,
880 )?;
881 let (ptr, alloc) = Box::into_unique(x);
882 Ok(unsafe { Self::from_inner_in(ptr.into(), alloc) })
883 }
884
885 /// Constructs a new `Arc` with uninitialized contents, in the provided allocator, returning an
886 /// error if allocation fails.
887 ///
888 /// # Examples
889 ///
890 /// ```
891 /// #![feature(allocator_api)]
892 /// #![feature(get_mut_unchecked)]
893 ///
894 /// use std::sync::Arc;
895 /// use std::alloc::System;
896 ///
897 /// let mut five = Arc::<u32, _>::try_new_uninit_in(System)?;
898 ///
899 /// let five = unsafe {
900 /// // Deferred initialization:
901 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
902 ///
903 /// five.assume_init()
904 /// };
905 ///
906 /// assert_eq!(*five, 5);
907 /// # Ok::<(), std::alloc::AllocError>(())
908 /// ```
909 #[unstable(feature = "allocator_api", issue = "32838")]
910 // #[unstable(feature = "new_uninit", issue = "63291")]
911 #[inline]
912 pub fn try_new_uninit_in(alloc: A) -> Result<Arc<mem::MaybeUninit<T>, A>, AllocError> {
913 unsafe {
914 Ok(Arc::from_ptr_in(
915 Arc::try_allocate_for_layout(
916 Layout::new::<T>(),
917 |layout| alloc.allocate(layout),
918 <*mut u8>::cast,
919 )?,
920 alloc,
921 ))
922 }
923 }
924
925 /// Constructs a new `Arc` with uninitialized contents, with the memory
926 /// being filled with `0` bytes, in the provided allocator, returning an error if allocation
927 /// fails.
928 ///
929 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
930 /// of this method.
931 ///
932 /// # Examples
933 ///
934 /// ```
935 /// #![feature(allocator_api)]
936 ///
937 /// use std::sync::Arc;
938 /// use std::alloc::System;
939 ///
940 /// let zero = Arc::<u32, _>::try_new_zeroed_in(System)?;
941 /// let zero = unsafe { zero.assume_init() };
942 ///
943 /// assert_eq!(*zero, 0);
944 /// # Ok::<(), std::alloc::AllocError>(())
945 /// ```
946 ///
947 /// [zeroed]: mem::MaybeUninit::zeroed
948 #[unstable(feature = "allocator_api", issue = "32838")]
949 // #[unstable(feature = "new_uninit", issue = "63291")]
950 #[inline]
951 pub fn try_new_zeroed_in(alloc: A) -> Result<Arc<mem::MaybeUninit<T>, A>, AllocError> {
952 unsafe {
953 Ok(Arc::from_ptr_in(
954 Arc::try_allocate_for_layout(
955 Layout::new::<T>(),
956 |layout| alloc.allocate_zeroed(layout),
957 <*mut u8>::cast,
958 )?,
959 alloc,
960 ))
961 }
962 }
963 /// Returns the inner value, if the `Arc` has exactly one strong reference.
964 ///
965 /// Otherwise, an [`Err`] is returned with the same `Arc` that was
966 /// passed in.
967 ///
968 /// This will succeed even if there are outstanding weak references.
969 ///
970 /// It is strongly recommended to use [`Arc::into_inner`] instead if you don't
971 /// keep the `Arc` in the [`Err`] case.
972 /// Immediately dropping the [`Err`]-value, as the expression
973 /// `Arc::try_unwrap(this).ok()` does, can cause the strong count to
974 /// drop to zero and the inner value of the `Arc` to be dropped.
975 /// For instance, if two threads execute such an expression in parallel,
976 /// there is a race condition without the possibility of unsafety:
977 /// The threads could first both check whether they own the last instance
978 /// in `Arc::try_unwrap`, determine that they both do not, and then both
979 /// discard and drop their instance in the call to [`ok`][`Result::ok`].
980 /// In this scenario, the value inside the `Arc` is safely destroyed
981 /// by exactly one of the threads, but neither thread will ever be able
982 /// to use the value.
983 ///
984 /// # Examples
985 ///
986 /// ```
987 /// use std::sync::Arc;
988 ///
989 /// let x = Arc::new(3);
990 /// assert_eq!(Arc::try_unwrap(x), Ok(3));
991 ///
992 /// let x = Arc::new(4);
993 /// let _y = Arc::clone(&x);
994 /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
995 /// ```
996 #[inline]
997 #[stable(feature = "arc_unique", since = "1.4.0")]
998 pub fn try_unwrap(this: Self) -> Result<T, Self> {
999 if this.inner().strong.compare_exchange(1, 0, Relaxed, Relaxed).is_err() {
1000 return Err(this);
1001 }
1002
1003 acquire!(this.inner().strong);
1004
1005 let this = ManuallyDrop::new(this);
1006 let elem: T = unsafe { ptr::read(&this.ptr.as_ref().data) };
1007 let alloc: A = unsafe { ptr::read(&this.alloc) }; // copy the allocator
1008
1009 // Make a weak pointer to clean up the implicit strong-weak reference
1010 let _weak = Weak { ptr: this.ptr, alloc };
1011
1012 Ok(elem)
1013 }
1014
1015 /// Returns the inner value, if the `Arc` has exactly one strong reference.
1016 ///
1017 /// Otherwise, [`None`] is returned and the `Arc` is dropped.
1018 ///
1019 /// This will succeed even if there are outstanding weak references.
1020 ///
1021 /// If `Arc::into_inner` is called on every clone of this `Arc`,
1022 /// it is guaranteed that exactly one of the calls returns the inner value.
1023 /// This means in particular that the inner value is not dropped.
1024 ///
1025 /// [`Arc::try_unwrap`] is conceptually similar to `Arc::into_inner`, but it
1026 /// is meant for different use-cases. If used as a direct replacement
1027 /// for `Arc::into_inner` anyway, such as with the expression
1028 /// <code>[Arc::try_unwrap]\(this).[ok][Result::ok]()</code>, then it does
1029 /// **not** give the same guarantee as described in the previous paragraph.
1030 /// For more information, see the examples below and read the documentation
1031 /// of [`Arc::try_unwrap`].
1032 ///
1033 /// # Examples
1034 ///
1035 /// Minimal example demonstrating the guarantee that `Arc::into_inner` gives.
1036 /// ```
1037 /// use std::sync::Arc;
1038 ///
1039 /// let x = Arc::new(3);
1040 /// let y = Arc::clone(&x);
1041 ///
1042 /// // Two threads calling `Arc::into_inner` on both clones of an `Arc`:
1043 /// let x_thread = std::thread::spawn(|| Arc::into_inner(x));
1044 /// let y_thread = std::thread::spawn(|| Arc::into_inner(y));
1045 ///
1046 /// let x_inner_value = x_thread.join().unwrap();
1047 /// let y_inner_value = y_thread.join().unwrap();
1048 ///
1049 /// // One of the threads is guaranteed to receive the inner value:
1050 /// assert!(matches!(
1051 /// (x_inner_value, y_inner_value),
1052 /// (None, Some(3)) | (Some(3), None)
1053 /// ));
1054 /// // The result could also be `(None, None)` if the threads called
1055 /// // `Arc::try_unwrap(x).ok()` and `Arc::try_unwrap(y).ok()` instead.
1056 /// ```
1057 ///
1058 /// A more practical example demonstrating the need for `Arc::into_inner`:
1059 /// ```
1060 /// use std::sync::Arc;
1061 ///
1062 /// // Definition of a simple singly linked list using `Arc`:
1063 /// #[derive(Clone)]
1064 /// struct LinkedList<T>(Option<Arc<Node<T>>>);
1065 /// struct Node<T>(T, Option<Arc<Node<T>>>);
1066 ///
1067 /// // Dropping a long `LinkedList<T>` relying on the destructor of `Arc`
1068 /// // can cause a stack overflow. To prevent this, we can provide a
1069 /// // manual `Drop` implementation that does the destruction in a loop:
1070 /// impl<T> Drop for LinkedList<T> {
1071 /// fn drop(&mut self) {
1072 /// let mut link = self.0.take();
1073 /// while let Some(arc_node) = link.take() {
1074 /// if let Some(Node(_value, next)) = Arc::into_inner(arc_node) {
1075 /// link = next;
1076 /// }
1077 /// }
1078 /// }
1079 /// }
1080 ///
1081 /// // Implementation of `new` and `push` omitted
1082 /// impl<T> LinkedList<T> {
1083 /// /* ... */
1084 /// # fn new() -> Self {
1085 /// # LinkedList(None)
1086 /// # }
1087 /// # fn push(&mut self, x: T) {
1088 /// # self.0 = Some(Arc::new(Node(x, self.0.take())));
1089 /// # }
1090 /// }
1091 ///
1092 /// // The following code could have still caused a stack overflow
1093 /// // despite the manual `Drop` impl if that `Drop` impl had used
1094 /// // `Arc::try_unwrap(arc).ok()` instead of `Arc::into_inner(arc)`.
1095 ///
1096 /// // Create a long list and clone it
1097 /// let mut x = LinkedList::new();
1098 /// let size = 100000;
1099 /// # let size = if cfg!(miri) { 100 } else { size };
1100 /// for i in 0..size {
1101 /// x.push(i); // Adds i to the front of x
1102 /// }
1103 /// let y = x.clone();
1104 ///
1105 /// // Drop the clones in parallel
1106 /// let x_thread = std::thread::spawn(|| drop(x));
1107 /// let y_thread = std::thread::spawn(|| drop(y));
1108 /// x_thread.join().unwrap();
1109 /// y_thread.join().unwrap();
1110 /// ```
1111 #[inline]
1112 #[stable(feature = "arc_into_inner", since = "1.70.0")]
1113 pub fn into_inner(this: Self) -> Option<T> {
1114 // Make sure that the ordinary `Drop` implementation isn’t called as well
1115 let mut this = mem::ManuallyDrop::new(this);
1116
1117 // Following the implementation of `drop` and `drop_slow`
1118 if this.inner().strong.fetch_sub(1, Release) != 1 {
1119 return None;
1120 }
1121
1122 acquire!(this.inner().strong);
1123
1124 // SAFETY: This mirrors the line
1125 //
1126 // unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) };
1127 //
1128 // in `drop_slow`. Instead of dropping the value behind the pointer,
1129 // it is read and eventually returned; `ptr::read` has the same
1130 // safety conditions as `ptr::drop_in_place`.
1131
1132 let inner = unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) };
1133 let alloc = unsafe { ptr::read(&this.alloc) };
1134
1135 drop(Weak { ptr: this.ptr, alloc });
1136
1137 Some(inner)
1138 }
1139}
1140
1141impl<T> Arc<[T]> {
1142 /// Constructs a new atomically reference-counted slice with uninitialized contents.
1143 ///
1144 /// # Examples
1145 ///
1146 /// ```
1147 /// #![feature(get_mut_unchecked)]
1148 ///
1149 /// use std::sync::Arc;
1150 ///
1151 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
1152 ///
1153 /// // Deferred initialization:
1154 /// let data = Arc::get_mut(&mut values).unwrap();
1155 /// data[0].write(1);
1156 /// data[1].write(2);
1157 /// data[2].write(3);
1158 ///
1159 /// let values = unsafe { values.assume_init() };
1160 ///
1161 /// assert_eq!(*values, [1, 2, 3])
1162 /// ```
1163 #[cfg(not(no_global_oom_handling))]
1164 #[inline]
1165 #[stable(feature = "new_uninit", since = "1.82.0")]
1166 #[must_use]
1167 pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
1168 unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) }
1169 }
1170
1171 /// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
1172 /// filled with `0` bytes.
1173 ///
1174 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1175 /// incorrect usage of this method.
1176 ///
1177 /// # Examples
1178 ///
1179 /// ```
1180 /// #![feature(new_zeroed_alloc)]
1181 ///
1182 /// use std::sync::Arc;
1183 ///
1184 /// let values = Arc::<[u32]>::new_zeroed_slice(3);
1185 /// let values = unsafe { values.assume_init() };
1186 ///
1187 /// assert_eq!(*values, [0, 0, 0])
1188 /// ```
1189 ///
1190 /// [zeroed]: mem::MaybeUninit::zeroed
1191 #[cfg(not(no_global_oom_handling))]
1192 #[inline]
1193 #[unstable(feature = "new_zeroed_alloc", issue = "129396")]
1194 #[must_use]
1195 pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
1196 unsafe {
1197 Arc::from_ptr(Arc::allocate_for_layout(
1198 Layout::array::<T>(len).unwrap(),
1199 |layout| Global.allocate_zeroed(layout),
1200 |mem| {
1201 ptr::slice_from_raw_parts_mut(mem as *mut T, len)
1202 as *mut ArcInner<[mem::MaybeUninit<T>]>
1203 },
1204 ))
1205 }
1206 }
1207
1208 /// Converts the reference-counted slice into a reference-counted array.
1209 ///
1210 /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
1211 ///
1212 /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1213 #[unstable(feature = "slice_as_array", issue = "133508")]
1214 #[inline]
1215 #[must_use]
1216 pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N]>> {
1217 if self.len() == N {
1218 let ptr = Self::into_raw(self) as *const [T; N];
1219
1220 // SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
1221 let me = unsafe { Arc::from_raw(ptr) };
1222 Some(me)
1223 } else {
1224 None
1225 }
1226 }
1227}
1228
1229impl<T, A: Allocator> Arc<[T], A> {
1230 /// Constructs a new atomically reference-counted slice with uninitialized contents in the
1231 /// provided allocator.
1232 ///
1233 /// # Examples
1234 ///
1235 /// ```
1236 /// #![feature(get_mut_unchecked)]
1237 /// #![feature(allocator_api)]
1238 ///
1239 /// use std::sync::Arc;
1240 /// use std::alloc::System;
1241 ///
1242 /// let mut values = Arc::<[u32], _>::new_uninit_slice_in(3, System);
1243 ///
1244 /// let values = unsafe {
1245 /// // Deferred initialization:
1246 /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
1247 /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
1248 /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
1249 ///
1250 /// values.assume_init()
1251 /// };
1252 ///
1253 /// assert_eq!(*values, [1, 2, 3])
1254 /// ```
1255 #[cfg(not(no_global_oom_handling))]
1256 #[unstable(feature = "allocator_api", issue = "32838")]
1257 #[inline]
1258 pub fn new_uninit_slice_in(len: usize, alloc: A) -> Arc<[mem::MaybeUninit<T>], A> {
1259 unsafe { Arc::from_ptr_in(Arc::allocate_for_slice_in(len, &alloc), alloc) }
1260 }
1261
1262 /// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
1263 /// filled with `0` bytes, in the provided allocator.
1264 ///
1265 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1266 /// incorrect usage of this method.
1267 ///
1268 /// # Examples
1269 ///
1270 /// ```
1271 /// #![feature(allocator_api)]
1272 ///
1273 /// use std::sync::Arc;
1274 /// use std::alloc::System;
1275 ///
1276 /// let values = Arc::<[u32], _>::new_zeroed_slice_in(3, System);
1277 /// let values = unsafe { values.assume_init() };
1278 ///
1279 /// assert_eq!(*values, [0, 0, 0])
1280 /// ```
1281 ///
1282 /// [zeroed]: mem::MaybeUninit::zeroed
1283 #[cfg(not(no_global_oom_handling))]
1284 #[unstable(feature = "allocator_api", issue = "32838")]
1285 #[inline]
1286 pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Arc<[mem::MaybeUninit<T>], A> {
1287 unsafe {
1288 Arc::from_ptr_in(
1289 Arc::allocate_for_layout(
1290 Layout::array::<T>(len).unwrap(),
1291 |layout| alloc.allocate_zeroed(layout),
1292 |mem| {
1293 ptr::slice_from_raw_parts_mut(mem.cast::<T>(), len)
1294 as *mut ArcInner<[mem::MaybeUninit<T>]>
1295 },
1296 ),
1297 alloc,
1298 )
1299 }
1300 }
1301}
1302
1303impl<T, A: Allocator> Arc<mem::MaybeUninit<T>, A> {
1304 /// Converts to `Arc<T>`.
1305 ///
1306 /// # Safety
1307 ///
1308 /// As with [`MaybeUninit::assume_init`],
1309 /// it is up to the caller to guarantee that the inner value
1310 /// really is in an initialized state.
1311 /// Calling this when the content is not yet fully initialized
1312 /// causes immediate undefined behavior.
1313 ///
1314 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1315 ///
1316 /// # Examples
1317 ///
1318 /// ```
1319 /// #![feature(get_mut_unchecked)]
1320 ///
1321 /// use std::sync::Arc;
1322 ///
1323 /// let mut five = Arc::<u32>::new_uninit();
1324 ///
1325 /// // Deferred initialization:
1326 /// Arc::get_mut(&mut five).unwrap().write(5);
1327 ///
1328 /// let five = unsafe { five.assume_init() };
1329 ///
1330 /// assert_eq!(*five, 5)
1331 /// ```
1332 #[stable(feature = "new_uninit", since = "1.82.0")]
1333 #[must_use = "`self` will be dropped if the result is not used"]
1334 #[inline]
1335 pub unsafe fn assume_init(self) -> Arc<T, A> {
1336 let (ptr, alloc) = Arc::into_inner_with_allocator(self);
1337 unsafe { Arc::from_inner_in(ptr.cast(), alloc) }
1338 }
1339}
1340
1341impl<T, A: Allocator> Arc<[mem::MaybeUninit<T>], A> {
1342 /// Converts to `Arc<[T]>`.
1343 ///
1344 /// # Safety
1345 ///
1346 /// As with [`MaybeUninit::assume_init`],
1347 /// it is up to the caller to guarantee that the inner value
1348 /// really is in an initialized state.
1349 /// Calling this when the content is not yet fully initialized
1350 /// causes immediate undefined behavior.
1351 ///
1352 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1353 ///
1354 /// # Examples
1355 ///
1356 /// ```
1357 /// #![feature(get_mut_unchecked)]
1358 ///
1359 /// use std::sync::Arc;
1360 ///
1361 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
1362 ///
1363 /// // Deferred initialization:
1364 /// let data = Arc::get_mut(&mut values).unwrap();
1365 /// data[0].write(1);
1366 /// data[1].write(2);
1367 /// data[2].write(3);
1368 ///
1369 /// let values = unsafe { values.assume_init() };
1370 ///
1371 /// assert_eq!(*values, [1, 2, 3])
1372 /// ```
1373 #[stable(feature = "new_uninit", since = "1.82.0")]
1374 #[must_use = "`self` will be dropped if the result is not used"]
1375 #[inline]
1376 pub unsafe fn assume_init(self) -> Arc<[T], A> {
1377 let (ptr, alloc) = Arc::into_inner_with_allocator(self);
1378 unsafe { Arc::from_ptr_in(ptr.as_ptr() as _, alloc) }
1379 }
1380}
1381
1382impl<T: ?Sized> Arc<T> {
1383 /// Constructs an `Arc<T>` from a raw pointer.
1384 ///
1385 /// The raw pointer must have been previously returned by a call to
1386 /// [`Arc<U>::into_raw`][into_raw] with the following requirements:
1387 ///
1388 /// * If `U` is sized, it must have the same size and alignment as `T`. This
1389 /// is trivially true if `U` is `T`.
1390 /// * If `U` is unsized, its data pointer must have the same size and
1391 /// alignment as `T`. This is trivially true if `Arc<U>` was constructed
1392 /// through `Arc<T>` and then converted to `Arc<U>` through an [unsized
1393 /// coercion].
1394 ///
1395 /// Note that if `U` or `U`'s data pointer is not `T` but has the same size
1396 /// and alignment, this is basically like transmuting references of
1397 /// different types. See [`mem::transmute`][transmute] for more information
1398 /// on what restrictions apply in this case.
1399 ///
1400 /// The raw pointer must point to a block of memory allocated by the global allocator.
1401 ///
1402 /// The user of `from_raw` has to make sure a specific value of `T` is only
1403 /// dropped once.
1404 ///
1405 /// This function is unsafe because improper use may lead to memory unsafety,
1406 /// even if the returned `Arc<T>` is never accessed.
1407 ///
1408 /// [into_raw]: Arc::into_raw
1409 /// [transmute]: core::mem::transmute
1410 /// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
1411 ///
1412 /// # Examples
1413 ///
1414 /// ```
1415 /// use std::sync::Arc;
1416 ///
1417 /// let x = Arc::new("hello".to_owned());
1418 /// let x_ptr = Arc::into_raw(x);
1419 ///
1420 /// unsafe {
1421 /// // Convert back to an `Arc` to prevent leak.
1422 /// let x = Arc::from_raw(x_ptr);
1423 /// assert_eq!(&*x, "hello");
1424 ///
1425 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
1426 /// }
1427 ///
1428 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1429 /// ```
1430 ///
1431 /// Convert a slice back into its original array:
1432 ///
1433 /// ```
1434 /// use std::sync::Arc;
1435 ///
1436 /// let x: Arc<[u32]> = Arc::new([1, 2, 3]);
1437 /// let x_ptr: *const [u32] = Arc::into_raw(x);
1438 ///
1439 /// unsafe {
1440 /// let x: Arc<[u32; 3]> = Arc::from_raw(x_ptr.cast::<[u32; 3]>());
1441 /// assert_eq!(&*x, &[1, 2, 3]);
1442 /// }
1443 /// ```
1444 #[inline]
1445 #[stable(feature = "rc_raw", since = "1.17.0")]
1446 pub unsafe fn from_raw(ptr: *const T) -> Self {
1447 unsafe { Arc::from_raw_in(ptr, Global) }
1448 }
1449
1450 /// Increments the strong reference count on the `Arc<T>` associated with the
1451 /// provided pointer by one.
1452 ///
1453 /// # Safety
1454 ///
1455 /// The pointer must have been obtained through `Arc::into_raw`, and the
1456 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1457 /// least 1) for the duration of this method, and `ptr` must point to a block of memory
1458 /// allocated by the global allocator.
1459 ///
1460 /// # Examples
1461 ///
1462 /// ```
1463 /// use std::sync::Arc;
1464 ///
1465 /// let five = Arc::new(5);
1466 ///
1467 /// unsafe {
1468 /// let ptr = Arc::into_raw(five);
1469 /// Arc::increment_strong_count(ptr);
1470 ///
1471 /// // This assertion is deterministic because we haven't shared
1472 /// // the `Arc` between threads.
1473 /// let five = Arc::from_raw(ptr);
1474 /// assert_eq!(2, Arc::strong_count(&five));
1475 /// # // Prevent leaks for Miri.
1476 /// # Arc::decrement_strong_count(ptr);
1477 /// }
1478 /// ```
1479 #[inline]
1480 #[stable(feature = "arc_mutate_strong_count", since = "1.51.0")]
1481 pub unsafe fn increment_strong_count(ptr: *const T) {
1482 unsafe { Arc::increment_strong_count_in(ptr, Global) }
1483 }
1484
1485 /// Decrements the strong reference count on the `Arc<T>` associated with the
1486 /// provided pointer by one.
1487 ///
1488 /// # Safety
1489 ///
1490 /// The pointer must have been obtained through `Arc::into_raw`, and the
1491 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1492 /// least 1) when invoking this method, and `ptr` must point to a block of memory
1493 /// allocated by the global allocator. This method can be used to release the final
1494 /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
1495 /// released.
1496 ///
1497 /// # Examples
1498 ///
1499 /// ```
1500 /// use std::sync::Arc;
1501 ///
1502 /// let five = Arc::new(5);
1503 ///
1504 /// unsafe {
1505 /// let ptr = Arc::into_raw(five);
1506 /// Arc::increment_strong_count(ptr);
1507 ///
1508 /// // Those assertions are deterministic because we haven't shared
1509 /// // the `Arc` between threads.
1510 /// let five = Arc::from_raw(ptr);
1511 /// assert_eq!(2, Arc::strong_count(&five));
1512 /// Arc::decrement_strong_count(ptr);
1513 /// assert_eq!(1, Arc::strong_count(&five));
1514 /// }
1515 /// ```
1516 #[inline]
1517 #[stable(feature = "arc_mutate_strong_count", since = "1.51.0")]
1518 pub unsafe fn decrement_strong_count(ptr: *const T) {
1519 unsafe { Arc::decrement_strong_count_in(ptr, Global) }
1520 }
1521}
1522
1523impl<T: ?Sized, A: Allocator> Arc<T, A> {
1524 /// Returns a reference to the underlying allocator.
1525 ///
1526 /// Note: this is an associated function, which means that you have
1527 /// to call it as `Arc::allocator(&a)` instead of `a.allocator()`. This
1528 /// is so that there is no conflict with a method on the inner type.
1529 #[inline]
1530 #[unstable(feature = "allocator_api", issue = "32838")]
1531 pub fn allocator(this: &Self) -> &A {
1532 &this.alloc
1533 }
1534
1535 /// Consumes the `Arc`, returning the wrapped pointer.
1536 ///
1537 /// To avoid a memory leak the pointer must be converted back to an `Arc` using
1538 /// [`Arc::from_raw`].
1539 ///
1540 /// # Examples
1541 ///
1542 /// ```
1543 /// use std::sync::Arc;
1544 ///
1545 /// let x = Arc::new("hello".to_owned());
1546 /// let x_ptr = Arc::into_raw(x);
1547 /// assert_eq!(unsafe { &*x_ptr }, "hello");
1548 /// # // Prevent leaks for Miri.
1549 /// # drop(unsafe { Arc::from_raw(x_ptr) });
1550 /// ```
1551 #[must_use = "losing the pointer will leak memory"]
1552 #[stable(feature = "rc_raw", since = "1.17.0")]
1553 #[rustc_never_returns_null_ptr]
1554 pub fn into_raw(this: Self) -> *const T {
1555 let this = ManuallyDrop::new(this);
1556 Self::as_ptr(&*this)
1557 }
1558
1559 /// Consumes the `Arc`, returning the wrapped pointer and allocator.
1560 ///
1561 /// To avoid a memory leak the pointer must be converted back to an `Arc` using
1562 /// [`Arc::from_raw_in`].
1563 ///
1564 /// # Examples
1565 ///
1566 /// ```
1567 /// #![feature(allocator_api)]
1568 /// use std::sync::Arc;
1569 /// use std::alloc::System;
1570 ///
1571 /// let x = Arc::new_in("hello".to_owned(), System);
1572 /// let (ptr, alloc) = Arc::into_raw_with_allocator(x);
1573 /// assert_eq!(unsafe { &*ptr }, "hello");
1574 /// let x = unsafe { Arc::from_raw_in(ptr, alloc) };
1575 /// assert_eq!(&*x, "hello");
1576 /// ```
1577 #[must_use = "losing the pointer will leak memory"]
1578 #[unstable(feature = "allocator_api", issue = "32838")]
1579 pub fn into_raw_with_allocator(this: Self) -> (*const T, A) {
1580 let this = mem::ManuallyDrop::new(this);
1581 let ptr = Self::as_ptr(&this);
1582 // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
1583 let alloc = unsafe { ptr::read(&this.alloc) };
1584 (ptr, alloc)
1585 }
1586
1587 /// Provides a raw pointer to the data.
1588 ///
1589 /// The counts are not affected in any way and the `Arc` is not consumed. The pointer is valid for
1590 /// as long as there are strong counts in the `Arc`.
1591 ///
1592 /// # Examples
1593 ///
1594 /// ```
1595 /// use std::sync::Arc;
1596 ///
1597 /// let x = Arc::new("hello".to_owned());
1598 /// let y = Arc::clone(&x);
1599 /// let x_ptr = Arc::as_ptr(&x);
1600 /// assert_eq!(x_ptr, Arc::as_ptr(&y));
1601 /// assert_eq!(unsafe { &*x_ptr }, "hello");
1602 /// ```
1603 #[must_use]
1604 #[stable(feature = "rc_as_ptr", since = "1.45.0")]
1605 #[rustc_never_returns_null_ptr]
1606 pub fn as_ptr(this: &Self) -> *const T {
1607 let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
1608
1609 // SAFETY: This cannot go through Deref::deref or RcInnerPtr::inner because
1610 // this is required to retain raw/mut provenance such that e.g. `get_mut` can
1611 // write through the pointer after the Rc is recovered through `from_raw`.
1612 unsafe { &raw mut (*ptr).data }
1613 }
1614
1615 /// Constructs an `Arc<T, A>` from a raw pointer.
1616 ///
1617 /// The raw pointer must have been previously returned by a call to [`Arc<U,
1618 /// A>::into_raw`][into_raw] with the following requirements:
1619 ///
1620 /// * If `U` is sized, it must have the same size and alignment as `T`. This
1621 /// is trivially true if `U` is `T`.
1622 /// * If `U` is unsized, its data pointer must have the same size and
1623 /// alignment as `T`. This is trivially true if `Arc<U>` was constructed
1624 /// through `Arc<T>` and then converted to `Arc<U>` through an [unsized
1625 /// coercion].
1626 ///
1627 /// Note that if `U` or `U`'s data pointer is not `T` but has the same size
1628 /// and alignment, this is basically like transmuting references of
1629 /// different types. See [`mem::transmute`][transmute] for more information
1630 /// on what restrictions apply in this case.
1631 ///
1632 /// The raw pointer must point to a block of memory allocated by `alloc`
1633 ///
1634 /// The user of `from_raw` has to make sure a specific value of `T` is only
1635 /// dropped once.
1636 ///
1637 /// This function is unsafe because improper use may lead to memory unsafety,
1638 /// even if the returned `Arc<T>` is never accessed.
1639 ///
1640 /// [into_raw]: Arc::into_raw
1641 /// [transmute]: core::mem::transmute
1642 /// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions
1643 ///
1644 /// # Examples
1645 ///
1646 /// ```
1647 /// #![feature(allocator_api)]
1648 ///
1649 /// use std::sync::Arc;
1650 /// use std::alloc::System;
1651 ///
1652 /// let x = Arc::new_in("hello".to_owned(), System);
1653 /// let x_ptr = Arc::into_raw(x);
1654 ///
1655 /// unsafe {
1656 /// // Convert back to an `Arc` to prevent leak.
1657 /// let x = Arc::from_raw_in(x_ptr, System);
1658 /// assert_eq!(&*x, "hello");
1659 ///
1660 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
1661 /// }
1662 ///
1663 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1664 /// ```
1665 ///
1666 /// Convert a slice back into its original array:
1667 ///
1668 /// ```
1669 /// #![feature(allocator_api)]
1670 ///
1671 /// use std::sync::Arc;
1672 /// use std::alloc::System;
1673 ///
1674 /// let x: Arc<[u32], _> = Arc::new_in([1, 2, 3], System);
1675 /// let x_ptr: *const [u32] = Arc::into_raw(x);
1676 ///
1677 /// unsafe {
1678 /// let x: Arc<[u32; 3], _> = Arc::from_raw_in(x_ptr.cast::<[u32; 3]>(), System);
1679 /// assert_eq!(&*x, &[1, 2, 3]);
1680 /// }
1681 /// ```
1682 #[inline]
1683 #[unstable(feature = "allocator_api", issue = "32838")]
1684 pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {
1685 unsafe {
1686 let offset = data_offset(ptr);
1687
1688 // Reverse the offset to find the original ArcInner.
1689 let arc_ptr = ptr.byte_sub(offset) as *mut ArcInner<T>;
1690
1691 Self::from_ptr_in(arc_ptr, alloc)
1692 }
1693 }
1694
1695 /// Creates a new [`Weak`] pointer to this allocation.
1696 ///
1697 /// # Examples
1698 ///
1699 /// ```
1700 /// use std::sync::Arc;
1701 ///
1702 /// let five = Arc::new(5);
1703 ///
1704 /// let weak_five = Arc::downgrade(&five);
1705 /// ```
1706 #[must_use = "this returns a new `Weak` pointer, \
1707 without modifying the original `Arc`"]
1708 #[stable(feature = "arc_weak", since = "1.4.0")]
1709 pub fn downgrade(this: &Self) -> Weak<T, A>
1710 where
1711 A: Clone,
1712 {
1713 // This Relaxed is OK because we're checking the value in the CAS
1714 // below.
1715 let mut cur = this.inner().weak.load(Relaxed);
1716
1717 loop {
1718 // check if the weak counter is currently "locked"; if so, spin.
1719 if cur == usize::MAX {
1720 hint::spin_loop();
1721 cur = this.inner().weak.load(Relaxed);
1722 continue;
1723 }
1724
1725 // We can't allow the refcount to increase much past `MAX_REFCOUNT`.
1726 assert!(cur <= MAX_REFCOUNT, "{}", INTERNAL_OVERFLOW_ERROR);
1727
1728 // NOTE: this code currently ignores the possibility of overflow
1729 // into usize::MAX; in general both Rc and Arc need to be adjusted
1730 // to deal with overflow.
1731
1732 // Unlike with Clone(), we need this to be an Acquire read to
1733 // synchronize with the write coming from `is_unique`, so that the
1734 // events prior to that write happen before this read.
1735 match this.inner().weak.compare_exchange_weak(cur, cur + 1, Acquire, Relaxed) {
1736 Ok(_) => {
1737 // Make sure we do not create a dangling Weak
1738 debug_assert!(!is_dangling(this.ptr.as_ptr()));
1739 return Weak { ptr: this.ptr, alloc: this.alloc.clone() };
1740 }
1741 Err(old) => cur = old,
1742 }
1743 }
1744 }
1745
1746 /// Gets the number of [`Weak`] pointers to this allocation.
1747 ///
1748 /// # Safety
1749 ///
1750 /// This method by itself is safe, but using it correctly requires extra care.
1751 /// Another thread can change the weak count at any time,
1752 /// including potentially between calling this method and acting on the result.
1753 ///
1754 /// # Examples
1755 ///
1756 /// ```
1757 /// use std::sync::Arc;
1758 ///
1759 /// let five = Arc::new(5);
1760 /// let _weak_five = Arc::downgrade(&five);
1761 ///
1762 /// // This assertion is deterministic because we haven't shared
1763 /// // the `Arc` or `Weak` between threads.
1764 /// assert_eq!(1, Arc::weak_count(&five));
1765 /// ```
1766 #[inline]
1767 #[must_use]
1768 #[stable(feature = "arc_counts", since = "1.15.0")]
1769 pub fn weak_count(this: &Self) -> usize {
1770 let cnt = this.inner().weak.load(Relaxed);
1771 // If the weak count is currently locked, the value of the
1772 // count was 0 just before taking the lock.
1773 if cnt == usize::MAX { 0 } else { cnt - 1 }
1774 }
1775
1776 /// Gets the number of strong (`Arc`) pointers to this allocation.
1777 ///
1778 /// # Safety
1779 ///
1780 /// This method by itself is safe, but using it correctly requires extra care.
1781 /// Another thread can change the strong count at any time,
1782 /// including potentially between calling this method and acting on the result.
1783 ///
1784 /// # Examples
1785 ///
1786 /// ```
1787 /// use std::sync::Arc;
1788 ///
1789 /// let five = Arc::new(5);
1790 /// let _also_five = Arc::clone(&five);
1791 ///
1792 /// // This assertion is deterministic because we haven't shared
1793 /// // the `Arc` between threads.
1794 /// assert_eq!(2, Arc::strong_count(&five));
1795 /// ```
1796 #[inline]
1797 #[must_use]
1798 #[stable(feature = "arc_counts", since = "1.15.0")]
1799 pub fn strong_count(this: &Self) -> usize {
1800 this.inner().strong.load(Relaxed)
1801 }
1802
1803 /// Increments the strong reference count on the `Arc<T>` associated with the
1804 /// provided pointer by one.
1805 ///
1806 /// # Safety
1807 ///
1808 /// The pointer must have been obtained through `Arc::into_raw`, and the
1809 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1810 /// least 1) for the duration of this method,, and `ptr` must point to a block of memory
1811 /// allocated by `alloc`.
1812 ///
1813 /// # Examples
1814 ///
1815 /// ```
1816 /// #![feature(allocator_api)]
1817 ///
1818 /// use std::sync::Arc;
1819 /// use std::alloc::System;
1820 ///
1821 /// let five = Arc::new_in(5, System);
1822 ///
1823 /// unsafe {
1824 /// let ptr = Arc::into_raw(five);
1825 /// Arc::increment_strong_count_in(ptr, System);
1826 ///
1827 /// // This assertion is deterministic because we haven't shared
1828 /// // the `Arc` between threads.
1829 /// let five = Arc::from_raw_in(ptr, System);
1830 /// assert_eq!(2, Arc::strong_count(&five));
1831 /// # // Prevent leaks for Miri.
1832 /// # Arc::decrement_strong_count_in(ptr, System);
1833 /// }
1834 /// ```
1835 #[inline]
1836 #[unstable(feature = "allocator_api", issue = "32838")]
1837 pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
1838 where
1839 A: Clone,
1840 {
1841 // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
1842 let arc = unsafe { mem::ManuallyDrop::new(Arc::from_raw_in(ptr, alloc)) };
1843 // Now increase refcount, but don't drop new refcount either
1844 let _arc_clone: mem::ManuallyDrop<_> = arc.clone();
1845 }
1846
1847 /// Decrements the strong reference count on the `Arc<T>` associated with the
1848 /// provided pointer by one.
1849 ///
1850 /// # Safety
1851 ///
1852 /// The pointer must have been obtained through `Arc::into_raw`, the
1853 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1854 /// least 1) when invoking this method, and `ptr` must point to a block of memory
1855 /// allocated by `alloc`. This method can be used to release the final
1856 /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
1857 /// released.
1858 ///
1859 /// # Examples
1860 ///
1861 /// ```
1862 /// #![feature(allocator_api)]
1863 ///
1864 /// use std::sync::Arc;
1865 /// use std::alloc::System;
1866 ///
1867 /// let five = Arc::new_in(5, System);
1868 ///
1869 /// unsafe {
1870 /// let ptr = Arc::into_raw(five);
1871 /// Arc::increment_strong_count_in(ptr, System);
1872 ///
1873 /// // Those assertions are deterministic because we haven't shared
1874 /// // the `Arc` between threads.
1875 /// let five = Arc::from_raw_in(ptr, System);
1876 /// assert_eq!(2, Arc::strong_count(&five));
1877 /// Arc::decrement_strong_count_in(ptr, System);
1878 /// assert_eq!(1, Arc::strong_count(&five));
1879 /// }
1880 /// ```
1881 #[inline]
1882 #[unstable(feature = "allocator_api", issue = "32838")]
1883 pub unsafe fn decrement_strong_count_in(ptr: *const T, alloc: A) {
1884 unsafe { drop(Arc::from_raw_in(ptr, alloc)) };
1885 }
1886
1887 #[inline]
1888 fn inner(&self) -> &ArcInner<T> {
1889 // This unsafety is ok because while this arc is alive we're guaranteed
1890 // that the inner pointer is valid. Furthermore, we know that the
1891 // `ArcInner` structure itself is `Sync` because the inner data is
1892 // `Sync` as well, so we're ok loaning out an immutable pointer to these
1893 // contents.
1894 unsafe { self.ptr.as_ref() }
1895 }
1896
1897 // Non-inlined part of `drop`.
1898 #[inline(never)]
1899 unsafe fn drop_slow(&mut self) {
1900 // Drop the weak ref collectively held by all strong references when this
1901 // variable goes out of scope. This ensures that the memory is deallocated
1902 // even if the destructor of `T` panics.
1903 // Take a reference to `self.alloc` instead of cloning because 1. it'll last long
1904 // enough, and 2. you should be able to drop `Arc`s with unclonable allocators
1905 let _weak = Weak { ptr: self.ptr, alloc: &self.alloc };
1906
1907 // Destroy the data at this time, even though we must not free the box
1908 // allocation itself (there might still be weak pointers lying around).
1909 // We cannot use `get_mut_unchecked` here, because `self.alloc` is borrowed.
1910 unsafe { ptr::drop_in_place(&mut (*self.ptr.as_ptr()).data) };
1911 }
1912
1913 /// Returns `true` if the two `Arc`s point to the same allocation in a vein similar to
1914 /// [`ptr::eq`]. This function ignores the metadata of `dyn Trait` pointers.
1915 ///
1916 /// # Examples
1917 ///
1918 /// ```
1919 /// use std::sync::Arc;
1920 ///
1921 /// let five = Arc::new(5);
1922 /// let same_five = Arc::clone(&five);
1923 /// let other_five = Arc::new(5);
1924 ///
1925 /// assert!(Arc::ptr_eq(&five, &same_five));
1926 /// assert!(!Arc::ptr_eq(&five, &other_five));
1927 /// ```
1928 ///
1929 /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
1930 #[inline]
1931 #[must_use]
1932 #[stable(feature = "ptr_eq", since = "1.17.0")]
1933 pub fn ptr_eq(this: &Self, other: &Self) -> bool {
1934 ptr::addr_eq(this.ptr.as_ptr(), other.ptr.as_ptr())
1935 }
1936}
1937
1938impl<T: ?Sized> Arc<T> {
1939 /// Allocates an `ArcInner<T>` with sufficient space for
1940 /// a possibly-unsized inner value where the value has the layout provided.
1941 ///
1942 /// The function `mem_to_arcinner` is called with the data pointer
1943 /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
1944 #[cfg(not(no_global_oom_handling))]
1945 unsafe fn allocate_for_layout(
1946 value_layout: Layout,
1947 allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
1948 mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
1949 ) -> *mut ArcInner<T> {
1950 let layout = arcinner_layout_for_value_layout(value_layout);
1951
1952 let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
1953
1954 unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) }
1955 }
1956
1957 /// Allocates an `ArcInner<T>` with sufficient space for
1958 /// a possibly-unsized inner value where the value has the layout provided,
1959 /// returning an error if allocation fails.
1960 ///
1961 /// The function `mem_to_arcinner` is called with the data pointer
1962 /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
1963 unsafe fn try_allocate_for_layout(
1964 value_layout: Layout,
1965 allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
1966 mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
1967 ) -> Result<*mut ArcInner<T>, AllocError> {
1968 let layout = arcinner_layout_for_value_layout(value_layout);
1969
1970 let ptr = allocate(layout)?;
1971
1972 let inner = unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) };
1973
1974 Ok(inner)
1975 }
1976
1977 unsafe fn initialize_arcinner(
1978 ptr: NonNull<[u8]>,
1979 layout: Layout,
1980 mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
1981 ) -> *mut ArcInner<T> {
1982 let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr());
1983 debug_assert_eq!(unsafe { Layout::for_value_raw(inner) }, layout);
1984
1985 unsafe {
1986 (&raw mut (*inner).strong).write(atomic::AtomicUsize::new(1));
1987 (&raw mut (*inner).weak).write(atomic::AtomicUsize::new(1));
1988 }
1989
1990 inner
1991 }
1992}
1993
1994impl<T: ?Sized, A: Allocator> Arc<T, A> {
1995 /// Allocates an `ArcInner<T>` with sufficient space for an unsized inner value.
1996 #[inline]
1997 #[cfg(not(no_global_oom_handling))]
1998 unsafe fn allocate_for_ptr_in(ptr: *const T, alloc: &A) -> *mut ArcInner<T> {
1999 // Allocate for the `ArcInner<T>` using the given value.
2000 unsafe {
2001 Arc::allocate_for_layout(
2002 Layout::for_value_raw(ptr),
2003 |layout| alloc.allocate(layout),
2004 |mem| mem.with_metadata_of(ptr as *const ArcInner<T>),
2005 )
2006 }
2007 }
2008
2009 #[cfg(not(no_global_oom_handling))]
2010 fn from_box_in(src: Box<T, A>) -> Arc<T, A> {
2011 unsafe {
2012 let value_size = size_of_val(&*src);
2013 let ptr = Self::allocate_for_ptr_in(&*src, Box::allocator(&src));
2014
2015 // Copy value as bytes
2016 ptr::copy_nonoverlapping(
2017 (&raw const *src) as *const u8,
2018 (&raw mut (*ptr).data) as *mut u8,
2019 value_size,
2020 );
2021
2022 // Free the allocation without dropping its contents
2023 let (bptr, alloc) = Box::into_raw_with_allocator(src);
2024 let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
2025 drop(src);
2026
2027 Self::from_ptr_in(ptr, alloc)
2028 }
2029 }
2030}
2031
2032impl<T> Arc<[T]> {
2033 /// Allocates an `ArcInner<[T]>` with the given length.
2034 #[cfg(not(no_global_oom_handling))]
2035 unsafe fn allocate_for_slice(len: usize) -> *mut ArcInner<[T]> {
2036 unsafe {
2037 Self::allocate_for_layout(
2038 Layout::array::<T>(len).unwrap(),
2039 |layout| Global.allocate(layout),
2040 |mem| ptr::slice_from_raw_parts_mut(mem.cast::<T>(), len) as *mut ArcInner<[T]>,
2041 )
2042 }
2043 }
2044
2045 /// Copy elements from slice into newly allocated `Arc<[T]>`
2046 ///
2047 /// Unsafe because the caller must either take ownership or bind `T: Copy`.
2048 #[cfg(not(no_global_oom_handling))]
2049 unsafe fn copy_from_slice(v: &[T]) -> Arc<[T]> {
2050 unsafe {
2051 let ptr = Self::allocate_for_slice(v.len());
2052
2053 ptr::copy_nonoverlapping(v.as_ptr(), (&raw mut (*ptr).data) as *mut T, v.len());
2054
2055 Self::from_ptr(ptr)
2056 }
2057 }
2058
2059 /// Constructs an `Arc<[T]>` from an iterator known to be of a certain size.
2060 ///
2061 /// Behavior is undefined should the size be wrong.
2062 #[cfg(not(no_global_oom_handling))]
2063 unsafe fn from_iter_exact(iter: impl Iterator<Item = T>, len: usize) -> Arc<[T]> {
2064 // Panic guard while cloning T elements.
2065 // In the event of a panic, elements that have been written
2066 // into the new ArcInner will be dropped, then the memory freed.
2067 struct Guard<T> {
2068 mem: NonNull<u8>,
2069 elems: *mut T,
2070 layout: Layout,
2071 n_elems: usize,
2072 }
2073
2074 impl<T> Drop for Guard<T> {
2075 fn drop(&mut self) {
2076 unsafe {
2077 let slice = from_raw_parts_mut(self.elems, self.n_elems);
2078 ptr::drop_in_place(slice);
2079
2080 Global.deallocate(self.mem, self.layout);
2081 }
2082 }
2083 }
2084
2085 unsafe {
2086 let ptr = Self::allocate_for_slice(len);
2087
2088 let mem = ptr as *mut _ as *mut u8;
2089 let layout = Layout::for_value_raw(ptr);
2090
2091 // Pointer to first element
2092 let elems = (&raw mut (*ptr).data) as *mut T;
2093
2094 let mut guard = Guard { mem: NonNull::new_unchecked(mem), elems, layout, n_elems: 0 };
2095
2096 for (i, item) in iter.enumerate() {
2097 ptr::write(elems.add(i), item);
2098 guard.n_elems += 1;
2099 }
2100
2101 // All clear. Forget the guard so it doesn't free the new ArcInner.
2102 mem::forget(guard);
2103
2104 Self::from_ptr(ptr)
2105 }
2106 }
2107}
2108
2109impl<T, A: Allocator> Arc<[T], A> {
2110 /// Allocates an `ArcInner<[T]>` with the given length.
2111 #[inline]
2112 #[cfg(not(no_global_oom_handling))]
2113 unsafe fn allocate_for_slice_in(len: usize, alloc: &A) -> *mut ArcInner<[T]> {
2114 unsafe {
2115 Arc::allocate_for_layout(
2116 Layout::array::<T>(len).unwrap(),
2117 |layout| alloc.allocate(layout),
2118 |mem| ptr::slice_from_raw_parts_mut(mem.cast::<T>(), len) as *mut ArcInner<[T]>,
2119 )
2120 }
2121 }
2122}
2123
2124/// Specialization trait used for `From<&[T]>`.
2125#[cfg(not(no_global_oom_handling))]
2126trait ArcFromSlice<T> {
2127 fn from_slice(slice: &[T]) -> Self;
2128}
2129
2130#[cfg(not(no_global_oom_handling))]
2131impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
2132 #[inline]
2133 default fn from_slice(v: &[T]) -> Self {
2134 unsafe { Self::from_iter_exact(v.iter().cloned(), v.len()) }
2135 }
2136}
2137
2138#[cfg(not(no_global_oom_handling))]
2139impl<T: Copy> ArcFromSlice<T> for Arc<[T]> {
2140 #[inline]
2141 fn from_slice(v: &[T]) -> Self {
2142 unsafe { Arc::copy_from_slice(v) }
2143 }
2144}
2145
2146#[stable(feature = "rust1", since = "1.0.0")]
2147impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
2148 /// Makes a clone of the `Arc` pointer.
2149 ///
2150 /// This creates another pointer to the same allocation, increasing the
2151 /// strong reference count.
2152 ///
2153 /// # Examples
2154 ///
2155 /// ```
2156 /// use std::sync::Arc;
2157 ///
2158 /// let five = Arc::new(5);
2159 ///
2160 /// let _ = Arc::clone(&five);
2161 /// ```
2162 #[inline]
2163 fn clone(&self) -> Arc<T, A> {
2164 // Using a relaxed ordering is alright here, as knowledge of the
2165 // original reference prevents other threads from erroneously deleting
2166 // the object.
2167 //
2168 // As explained in the [Boost documentation][1], Increasing the
2169 // reference counter can always be done with memory_order_relaxed: New
2170 // references to an object can only be formed from an existing
2171 // reference, and passing an existing reference from one thread to
2172 // another must already provide any required synchronization.
2173 //
2174 // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
2175 let old_size = self.inner().strong.fetch_add(1, Relaxed);
2176
2177 // However we need to guard against massive refcounts in case someone is `mem::forget`ing
2178 // Arcs. If we don't do this the count can overflow and users will use-after free. This
2179 // branch will never be taken in any realistic program. We abort because such a program is
2180 // incredibly degenerate, and we don't care to support it.
2181 //
2182 // This check is not 100% water-proof: we error when the refcount grows beyond `isize::MAX`.
2183 // But we do that check *after* having done the increment, so there is a chance here that
2184 // the worst already happened and we actually do overflow the `usize` counter. However, that
2185 // requires the counter to grow from `isize::MAX` to `usize::MAX` between the increment
2186 // above and the `abort` below, which seems exceedingly unlikely.
2187 //
2188 // This is a global invariant, and also applies when using a compare-exchange loop to increment
2189 // counters in other methods.
2190 // Otherwise, the counter could be brought to an almost-overflow using a compare-exchange loop,
2191 // and then overflow using a few `fetch_add`s.
2192 if old_size > MAX_REFCOUNT {
2193 abort();
2194 }
2195
2196 unsafe { Self::from_inner_in(self.ptr, self.alloc.clone()) }
2197 }
2198}
2199
2200#[stable(feature = "rust1", since = "1.0.0")]
2201impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
2202 type Target = T;
2203
2204 #[inline]
2205 fn deref(&self) -> &T {
2206 &self.inner().data
2207 }
2208}
2209
2210#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2211unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Arc<T, A> {}
2212
2213#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2214unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Weak<T, A> {}
2215
2216#[unstable(feature = "deref_pure_trait", issue = "87121")]
2217unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
2218
2219#[unstable(feature = "legacy_receiver_trait", issue = "none")]
2220impl<T: ?Sized> LegacyReceiver for Arc<T> {}
2221
2222#[cfg(not(no_global_oom_handling))]
2223impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Arc<T, A> {
2224 /// Makes a mutable reference into the given `Arc`.
2225 ///
2226 /// If there are other `Arc` pointers to the same allocation, then `make_mut` will
2227 /// [`clone`] the inner value to a new allocation to ensure unique ownership. This is also
2228 /// referred to as clone-on-write.
2229 ///
2230 /// However, if there are no other `Arc` pointers to this allocation, but some [`Weak`]
2231 /// pointers, then the [`Weak`] pointers will be dissociated and the inner value will not
2232 /// be cloned.
2233 ///
2234 /// See also [`get_mut`], which will fail rather than cloning the inner value
2235 /// or dissociating [`Weak`] pointers.
2236 ///
2237 /// [`clone`]: Clone::clone
2238 /// [`get_mut`]: Arc::get_mut
2239 ///
2240 /// # Examples
2241 ///
2242 /// ```
2243 /// use std::sync::Arc;
2244 ///
2245 /// let mut data = Arc::new(5);
2246 ///
2247 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
2248 /// let mut other_data = Arc::clone(&data); // Won't clone inner data
2249 /// *Arc::make_mut(&mut data) += 1; // Clones inner data
2250 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
2251 /// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
2252 ///
2253 /// // Now `data` and `other_data` point to different allocations.
2254 /// assert_eq!(*data, 8);
2255 /// assert_eq!(*other_data, 12);
2256 /// ```
2257 ///
2258 /// [`Weak`] pointers will be dissociated:
2259 ///
2260 /// ```
2261 /// use std::sync::Arc;
2262 ///
2263 /// let mut data = Arc::new(75);
2264 /// let weak = Arc::downgrade(&data);
2265 ///
2266 /// assert!(75 == *data);
2267 /// assert!(75 == *weak.upgrade().unwrap());
2268 ///
2269 /// *Arc::make_mut(&mut data) += 1;
2270 ///
2271 /// assert!(76 == *data);
2272 /// assert!(weak.upgrade().is_none());
2273 /// ```
2274 #[inline]
2275 #[stable(feature = "arc_unique", since = "1.4.0")]
2276 pub fn make_mut(this: &mut Self) -> &mut T {
2277 let size_of_val = mem::size_of_val::<T>(&**this);
2278
2279 // Note that we hold both a strong reference and a weak reference.
2280 // Thus, releasing our strong reference only will not, by itself, cause
2281 // the memory to be deallocated.
2282 //
2283 // Use Acquire to ensure that we see any writes to `weak` that happen
2284 // before release writes (i.e., decrements) to `strong`. Since we hold a
2285 // weak count, there's no chance the ArcInner itself could be
2286 // deallocated.
2287 if this.inner().strong.compare_exchange(1, 0, Acquire, Relaxed).is_err() {
2288 // Another strong pointer exists, so we must clone.
2289
2290 let this_data_ref: &T = &**this;
2291 // `in_progress` drops the allocation if we panic before finishing initializing it.
2292 let mut in_progress: UniqueArcUninit<T, A> =
2293 UniqueArcUninit::new(this_data_ref, this.alloc.clone());
2294
2295 let initialized_clone = unsafe {
2296 // Clone. If the clone panics, `in_progress` will be dropped and clean up.
2297 this_data_ref.clone_to_uninit(in_progress.data_ptr().cast());
2298 // Cast type of pointer, now that it is initialized.
2299 in_progress.into_arc()
2300 };
2301 *this = initialized_clone;
2302 } else if this.inner().weak.load(Relaxed) != 1 {
2303 // Relaxed suffices in the above because this is fundamentally an
2304 // optimization: we are always racing with weak pointers being
2305 // dropped. Worst case, we end up allocated a new Arc unnecessarily.
2306
2307 // We removed the last strong ref, but there are additional weak
2308 // refs remaining. We'll move the contents to a new Arc, and
2309 // invalidate the other weak refs.
2310
2311 // Note that it is not possible for the read of `weak` to yield
2312 // usize::MAX (i.e., locked), since the weak count can only be
2313 // locked by a thread with a strong reference.
2314
2315 // Materialize our own implicit weak pointer, so that it can clean
2316 // up the ArcInner as needed.
2317 let _weak = Weak { ptr: this.ptr, alloc: this.alloc.clone() };
2318
2319 // Can just steal the data, all that's left is Weaks
2320 //
2321 // We don't need panic-protection like the above branch does, but we might as well
2322 // use the same mechanism.
2323 let mut in_progress: UniqueArcUninit<T, A> =
2324 UniqueArcUninit::new(&**this, this.alloc.clone());
2325 unsafe {
2326 // Initialize `in_progress` with move of **this.
2327 // We have to express this in terms of bytes because `T: ?Sized`; there is no
2328 // operation that just copies a value based on its `size_of_val()`.
2329 ptr::copy_nonoverlapping(
2330 ptr::from_ref(&**this).cast::<u8>(),
2331 in_progress.data_ptr().cast::<u8>(),
2332 size_of_val,
2333 );
2334
2335 ptr::write(this, in_progress.into_arc());
2336 }
2337 } else {
2338 // We were the sole reference of either kind; bump back up the
2339 // strong ref count.
2340 this.inner().strong.store(1, Release);
2341 }
2342
2343 // As with `get_mut()`, the unsafety is ok because our reference was
2344 // either unique to begin with, or became one upon cloning the contents.
2345 unsafe { Self::get_mut_unchecked(this) }
2346 }
2347}
2348
2349impl<T: Clone, A: Allocator> Arc<T, A> {
2350 /// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
2351 /// clone.
2352 ///
2353 /// Assuming `arc_t` is of type `Arc<T>`, this function is functionally equivalent to
2354 /// `(*arc_t).clone()`, but will avoid cloning the inner value where possible.
2355 ///
2356 /// # Examples
2357 ///
2358 /// ```
2359 /// # use std::{ptr, sync::Arc};
2360 /// let inner = String::from("test");
2361 /// let ptr = inner.as_ptr();
2362 ///
2363 /// let arc = Arc::new(inner);
2364 /// let inner = Arc::unwrap_or_clone(arc);
2365 /// // The inner value was not cloned
2366 /// assert!(ptr::eq(ptr, inner.as_ptr()));
2367 ///
2368 /// let arc = Arc::new(inner);
2369 /// let arc2 = arc.clone();
2370 /// let inner = Arc::unwrap_or_clone(arc);
2371 /// // Because there were 2 references, we had to clone the inner value.
2372 /// assert!(!ptr::eq(ptr, inner.as_ptr()));
2373 /// // `arc2` is the last reference, so when we unwrap it we get back
2374 /// // the original `String`.
2375 /// let inner = Arc::unwrap_or_clone(arc2);
2376 /// assert!(ptr::eq(ptr, inner.as_ptr()));
2377 /// ```
2378 #[inline]
2379 #[stable(feature = "arc_unwrap_or_clone", since = "1.76.0")]
2380 pub fn unwrap_or_clone(this: Self) -> T {
2381 Arc::try_unwrap(this).unwrap_or_else(|arc| (*arc).clone())
2382 }
2383}
2384
2385impl<T: ?Sized, A: Allocator> Arc<T, A> {
2386 /// Returns a mutable reference into the given `Arc`, if there are
2387 /// no other `Arc` or [`Weak`] pointers to the same allocation.
2388 ///
2389 /// Returns [`None`] otherwise, because it is not safe to
2390 /// mutate a shared value.
2391 ///
2392 /// See also [`make_mut`][make_mut], which will [`clone`][clone]
2393 /// the inner value when there are other `Arc` pointers.
2394 ///
2395 /// [make_mut]: Arc::make_mut
2396 /// [clone]: Clone::clone
2397 ///
2398 /// # Examples
2399 ///
2400 /// ```
2401 /// use std::sync::Arc;
2402 ///
2403 /// let mut x = Arc::new(3);
2404 /// *Arc::get_mut(&mut x).unwrap() = 4;
2405 /// assert_eq!(*x, 4);
2406 ///
2407 /// let _y = Arc::clone(&x);
2408 /// assert!(Arc::get_mut(&mut x).is_none());
2409 /// ```
2410 #[inline]
2411 #[stable(feature = "arc_unique", since = "1.4.0")]
2412 pub fn get_mut(this: &mut Self) -> Option<&mut T> {
2413 if this.is_unique() {
2414 // This unsafety is ok because we're guaranteed that the pointer
2415 // returned is the *only* pointer that will ever be returned to T. Our
2416 // reference count is guaranteed to be 1 at this point, and we required
2417 // the Arc itself to be `mut`, so we're returning the only possible
2418 // reference to the inner data.
2419 unsafe { Some(Arc::get_mut_unchecked(this)) }
2420 } else {
2421 None
2422 }
2423 }
2424
2425 /// Returns a mutable reference into the given `Arc`,
2426 /// without any check.
2427 ///
2428 /// See also [`get_mut`], which is safe and does appropriate checks.
2429 ///
2430 /// [`get_mut`]: Arc::get_mut
2431 ///
2432 /// # Safety
2433 ///
2434 /// If any other `Arc` or [`Weak`] pointers to the same allocation exist, then
2435 /// they must not be dereferenced or have active borrows for the duration
2436 /// of the returned borrow, and their inner type must be exactly the same as the
2437 /// inner type of this Rc (including lifetimes). This is trivially the case if no
2438 /// such pointers exist, for example immediately after `Arc::new`.
2439 ///
2440 /// # Examples
2441 ///
2442 /// ```
2443 /// #![feature(get_mut_unchecked)]
2444 ///
2445 /// use std::sync::Arc;
2446 ///
2447 /// let mut x = Arc::new(String::new());
2448 /// unsafe {
2449 /// Arc::get_mut_unchecked(&mut x).push_str("foo")
2450 /// }
2451 /// assert_eq!(*x, "foo");
2452 /// ```
2453 /// Other `Arc` pointers to the same allocation must be to the same type.
2454 /// ```no_run
2455 /// #![feature(get_mut_unchecked)]
2456 ///
2457 /// use std::sync::Arc;
2458 ///
2459 /// let x: Arc<str> = Arc::from("Hello, world!");
2460 /// let mut y: Arc<[u8]> = x.clone().into();
2461 /// unsafe {
2462 /// // this is Undefined Behavior, because x's inner type is str, not [u8]
2463 /// Arc::get_mut_unchecked(&mut y).fill(0xff); // 0xff is invalid in UTF-8
2464 /// }
2465 /// println!("{}", &*x); // Invalid UTF-8 in a str
2466 /// ```
2467 /// Other `Arc` pointers to the same allocation must be to the exact same type, including lifetimes.
2468 /// ```no_run
2469 /// #![feature(get_mut_unchecked)]
2470 ///
2471 /// use std::sync::Arc;
2472 ///
2473 /// let x: Arc<&str> = Arc::new("Hello, world!");
2474 /// {
2475 /// let s = String::from("Oh, no!");
2476 /// let mut y: Arc<&str> = x.clone();
2477 /// unsafe {
2478 /// // this is Undefined Behavior, because x's inner type
2479 /// // is &'long str, not &'short str
2480 /// *Arc::get_mut_unchecked(&mut y) = &s;
2481 /// }
2482 /// }
2483 /// println!("{}", &*x); // Use-after-free
2484 /// ```
2485 #[inline]
2486 #[unstable(feature = "get_mut_unchecked", issue = "63292")]
2487 pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
2488 // We are careful to *not* create a reference covering the "count" fields, as
2489 // this would alias with concurrent access to the reference counts (e.g. by `Weak`).
2490 unsafe { &mut (*this.ptr.as_ptr()).data }
2491 }
2492
2493 /// Determine whether this is the unique reference (including weak refs) to
2494 /// the underlying data.
2495 ///
2496 /// Note that this requires locking the weak ref count.
2497 fn is_unique(&mut self) -> bool {
2498 // lock the weak pointer count if we appear to be the sole weak pointer
2499 // holder.
2500 //
2501 // The acquire label here ensures a happens-before relationship with any
2502 // writes to `strong` (in particular in `Weak::upgrade`) prior to decrements
2503 // of the `weak` count (via `Weak::drop`, which uses release). If the upgraded
2504 // weak ref was never dropped, the CAS here will fail so we do not care to synchronize.
2505 if self.inner().weak.compare_exchange(1, usize::MAX, Acquire, Relaxed).is_ok() {
2506 // This needs to be an `Acquire` to synchronize with the decrement of the `strong`
2507 // counter in `drop` -- the only access that happens when any but the last reference
2508 // is being dropped.
2509 let unique = self.inner().strong.load(Acquire) == 1;
2510
2511 // The release write here synchronizes with a read in `downgrade`,
2512 // effectively preventing the above read of `strong` from happening
2513 // after the write.
2514 self.inner().weak.store(1, Release); // release the lock
2515 unique
2516 } else {
2517 false
2518 }
2519 }
2520}
2521
2522#[stable(feature = "rust1", since = "1.0.0")]
2523unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Arc<T, A> {
2524 /// Drops the `Arc`.
2525 ///
2526 /// This will decrement the strong reference count. If the strong reference
2527 /// count reaches zero then the only other references (if any) are
2528 /// [`Weak`], so we `drop` the inner value.
2529 ///
2530 /// # Examples
2531 ///
2532 /// ```
2533 /// use std::sync::Arc;
2534 ///
2535 /// struct Foo;
2536 ///
2537 /// impl Drop for Foo {
2538 /// fn drop(&mut self) {
2539 /// println!("dropped!");
2540 /// }
2541 /// }
2542 ///
2543 /// let foo = Arc::new(Foo);
2544 /// let foo2 = Arc::clone(&foo);
2545 ///
2546 /// drop(foo); // Doesn't print anything
2547 /// drop(foo2); // Prints "dropped!"
2548 /// ```
2549 #[inline]
2550 fn drop(&mut self) {
2551 // Because `fetch_sub` is already atomic, we do not need to synchronize
2552 // with other threads unless we are going to delete the object. This
2553 // same logic applies to the below `fetch_sub` to the `weak` count.
2554 if self.inner().strong.fetch_sub(1, Release) != 1 {
2555 return;
2556 }
2557
2558 // This fence is needed to prevent reordering of use of the data and
2559 // deletion of the data. Because it is marked `Release`, the decreasing
2560 // of the reference count synchronizes with this `Acquire` fence. This
2561 // means that use of the data happens before decreasing the reference
2562 // count, which happens before this fence, which happens before the
2563 // deletion of the data.
2564 //
2565 // As explained in the [Boost documentation][1],
2566 //
2567 // > It is important to enforce any possible access to the object in one
2568 // > thread (through an existing reference) to *happen before* deleting
2569 // > the object in a different thread. This is achieved by a "release"
2570 // > operation after dropping a reference (any access to the object
2571 // > through this reference must obviously happened before), and an
2572 // > "acquire" operation before deleting the object.
2573 //
2574 // In particular, while the contents of an Arc are usually immutable, it's
2575 // possible to have interior writes to something like a Mutex<T>. Since a
2576 // Mutex is not acquired when it is deleted, we can't rely on its
2577 // synchronization logic to make writes in thread A visible to a destructor
2578 // running in thread B.
2579 //
2580 // Also note that the Acquire fence here could probably be replaced with an
2581 // Acquire load, which could improve performance in highly-contended
2582 // situations. See [2].
2583 //
2584 // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
2585 // [2]: (https://github.com/rust-lang/rust/pull/41714)
2586 acquire!(self.inner().strong);
2587
2588 // Make sure we aren't trying to "drop" the shared static for empty slices
2589 // used by Default::default.
2590 debug_assert!(
2591 !ptr::addr_eq(self.ptr.as_ptr(), &STATIC_INNER_SLICE.inner),
2592 "Arcs backed by a static should never reach a strong count of 0. \
2593 Likely decrement_strong_count or from_raw were called too many times.",
2594 );
2595
2596 unsafe {
2597 self.drop_slow();
2598 }
2599 }
2600}
2601
2602impl<A: Allocator> Arc<dyn Any + Send + Sync, A> {
2603 /// Attempts to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
2604 ///
2605 /// # Examples
2606 ///
2607 /// ```
2608 /// use std::any::Any;
2609 /// use std::sync::Arc;
2610 ///
2611 /// fn print_if_string(value: Arc<dyn Any + Send + Sync>) {
2612 /// if let Ok(string) = value.downcast::<String>() {
2613 /// println!("String ({}): {}", string.len(), string);
2614 /// }
2615 /// }
2616 ///
2617 /// let my_string = "Hello World".to_string();
2618 /// print_if_string(Arc::new(my_string));
2619 /// print_if_string(Arc::new(0i8));
2620 /// ```
2621 #[inline]
2622 #[stable(feature = "rc_downcast", since = "1.29.0")]
2623 pub fn downcast<T>(self) -> Result<Arc<T, A>, Self>
2624 where
2625 T: Any + Send + Sync,
2626 {
2627 if (*self).is::<T>() {
2628 unsafe {
2629 let (ptr, alloc) = Arc::into_inner_with_allocator(self);
2630 Ok(Arc::from_inner_in(ptr.cast(), alloc))
2631 }
2632 } else {
2633 Err(self)
2634 }
2635 }
2636
2637 /// Downcasts the `Arc<dyn Any + Send + Sync>` to a concrete type.
2638 ///
2639 /// For a safe alternative see [`downcast`].
2640 ///
2641 /// # Examples
2642 ///
2643 /// ```
2644 /// #![feature(downcast_unchecked)]
2645 ///
2646 /// use std::any::Any;
2647 /// use std::sync::Arc;
2648 ///
2649 /// let x: Arc<dyn Any + Send + Sync> = Arc::new(1_usize);
2650 ///
2651 /// unsafe {
2652 /// assert_eq!(*x.downcast_unchecked::<usize>(), 1);
2653 /// }
2654 /// ```
2655 ///
2656 /// # Safety
2657 ///
2658 /// The contained value must be of type `T`. Calling this method
2659 /// with the incorrect type is *undefined behavior*.
2660 ///
2661 ///
2662 /// [`downcast`]: Self::downcast
2663 #[inline]
2664 #[unstable(feature = "downcast_unchecked", issue = "90850")]
2665 pub unsafe fn downcast_unchecked<T>(self) -> Arc<T, A>
2666 where
2667 T: Any + Send + Sync,
2668 {
2669 unsafe {
2670 let (ptr, alloc) = Arc::into_inner_with_allocator(self);
2671 Arc::from_inner_in(ptr.cast(), alloc)
2672 }
2673 }
2674}
2675
2676impl<T> Weak<T> {
2677 /// Constructs a new `Weak<T>`, without allocating any memory.
2678 /// Calling [`upgrade`] on the return value always gives [`None`].
2679 ///
2680 /// [`upgrade`]: Weak::upgrade
2681 ///
2682 /// # Examples
2683 ///
2684 /// ```
2685 /// use std::sync::Weak;
2686 ///
2687 /// let empty: Weak<i64> = Weak::new();
2688 /// assert!(empty.upgrade().is_none());
2689 /// ```
2690 #[inline]
2691 #[stable(feature = "downgraded_weak", since = "1.10.0")]
2692 #[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
2693 #[must_use]
2694 pub const fn new() -> Weak<T> {
2695 Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc: Global }
2696 }
2697}
2698
2699impl<T, A: Allocator> Weak<T, A> {
2700 /// Constructs a new `Weak<T, A>`, without allocating any memory, technically in the provided
2701 /// allocator.
2702 /// Calling [`upgrade`] on the return value always gives [`None`].
2703 ///
2704 /// [`upgrade`]: Weak::upgrade
2705 ///
2706 /// # Examples
2707 ///
2708 /// ```
2709 /// #![feature(allocator_api)]
2710 ///
2711 /// use std::sync::Weak;
2712 /// use std::alloc::System;
2713 ///
2714 /// let empty: Weak<i64, _> = Weak::new_in(System);
2715 /// assert!(empty.upgrade().is_none());
2716 /// ```
2717 #[inline]
2718 #[unstable(feature = "allocator_api", issue = "32838")]
2719 pub fn new_in(alloc: A) -> Weak<T, A> {
2720 Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc }
2721 }
2722}
2723
2724/// Helper type to allow accessing the reference counts without
2725/// making any assertions about the data field.
2726struct WeakInner<'a> {
2727 weak: &'a atomic::AtomicUsize,
2728 strong: &'a atomic::AtomicUsize,
2729}
2730
2731impl<T: ?Sized> Weak<T> {
2732 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
2733 ///
2734 /// This can be used to safely get a strong reference (by calling [`upgrade`]
2735 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
2736 ///
2737 /// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
2738 /// as these don't own anything; the method still works on them).
2739 ///
2740 /// # Safety
2741 ///
2742 /// The pointer must have originated from the [`into_raw`] and must still own its potential
2743 /// weak reference, and must point to a block of memory allocated by global allocator.
2744 ///
2745 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
2746 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
2747 /// count is not modified by this operation) and therefore it must be paired with a previous
2748 /// call to [`into_raw`].
2749 /// # Examples
2750 ///
2751 /// ```
2752 /// use std::sync::{Arc, Weak};
2753 ///
2754 /// let strong = Arc::new("hello".to_owned());
2755 ///
2756 /// let raw_1 = Arc::downgrade(&strong).into_raw();
2757 /// let raw_2 = Arc::downgrade(&strong).into_raw();
2758 ///
2759 /// assert_eq!(2, Arc::weak_count(&strong));
2760 ///
2761 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
2762 /// assert_eq!(1, Arc::weak_count(&strong));
2763 ///
2764 /// drop(strong);
2765 ///
2766 /// // Decrement the last weak count.
2767 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
2768 /// ```
2769 ///
2770 /// [`new`]: Weak::new
2771 /// [`into_raw`]: Weak::into_raw
2772 /// [`upgrade`]: Weak::upgrade
2773 #[inline]
2774 #[stable(feature = "weak_into_raw", since = "1.45.0")]
2775 pub unsafe fn from_raw(ptr: *const T) -> Self {
2776 unsafe { Weak::from_raw_in(ptr, Global) }
2777 }
2778}
2779
2780impl<T: ?Sized, A: Allocator> Weak<T, A> {
2781 /// Returns a reference to the underlying allocator.
2782 #[inline]
2783 #[unstable(feature = "allocator_api", issue = "32838")]
2784 pub fn allocator(&self) -> &A {
2785 &self.alloc
2786 }
2787
2788 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
2789 ///
2790 /// The pointer is valid only if there are some strong references. The pointer may be dangling,
2791 /// unaligned or even [`null`] otherwise.
2792 ///
2793 /// # Examples
2794 ///
2795 /// ```
2796 /// use std::sync::Arc;
2797 /// use std::ptr;
2798 ///
2799 /// let strong = Arc::new("hello".to_owned());
2800 /// let weak = Arc::downgrade(&strong);
2801 /// // Both point to the same object
2802 /// assert!(ptr::eq(&*strong, weak.as_ptr()));
2803 /// // The strong here keeps it alive, so we can still access the object.
2804 /// assert_eq!("hello", unsafe { &*weak.as_ptr() });
2805 ///
2806 /// drop(strong);
2807 /// // But not any more. We can do weak.as_ptr(), but accessing the pointer would lead to
2808 /// // undefined behavior.
2809 /// // assert_eq!("hello", unsafe { &*weak.as_ptr() });
2810 /// ```
2811 ///
2812 /// [`null`]: core::ptr::null "ptr::null"
2813 #[must_use]
2814 #[stable(feature = "weak_into_raw", since = "1.45.0")]
2815 pub fn as_ptr(&self) -> *const T {
2816 let ptr: *mut ArcInner<T> = NonNull::as_ptr(self.ptr);
2817
2818 if is_dangling(ptr) {
2819 // If the pointer is dangling, we return the sentinel directly. This cannot be
2820 // a valid payload address, as the payload is at least as aligned as ArcInner (usize).
2821 ptr as *const T
2822 } else {
2823 // SAFETY: if is_dangling returns false, then the pointer is dereferenceable.
2824 // The payload may be dropped at this point, and we have to maintain provenance,
2825 // so use raw pointer manipulation.
2826 unsafe { &raw mut (*ptr).data }
2827 }
2828 }
2829
2830 /// Consumes the `Weak<T>` and turns it into a raw pointer.
2831 ///
2832 /// This converts the weak pointer into a raw pointer, while still preserving the ownership of
2833 /// one weak reference (the weak count is not modified by this operation). It can be turned
2834 /// back into the `Weak<T>` with [`from_raw`].
2835 ///
2836 /// The same restrictions of accessing the target of the pointer as with
2837 /// [`as_ptr`] apply.
2838 ///
2839 /// # Examples
2840 ///
2841 /// ```
2842 /// use std::sync::{Arc, Weak};
2843 ///
2844 /// let strong = Arc::new("hello".to_owned());
2845 /// let weak = Arc::downgrade(&strong);
2846 /// let raw = weak.into_raw();
2847 ///
2848 /// assert_eq!(1, Arc::weak_count(&strong));
2849 /// assert_eq!("hello", unsafe { &*raw });
2850 ///
2851 /// drop(unsafe { Weak::from_raw(raw) });
2852 /// assert_eq!(0, Arc::weak_count(&strong));
2853 /// ```
2854 ///
2855 /// [`from_raw`]: Weak::from_raw
2856 /// [`as_ptr`]: Weak::as_ptr
2857 #[must_use = "losing the pointer will leak memory"]
2858 #[stable(feature = "weak_into_raw", since = "1.45.0")]
2859 pub fn into_raw(self) -> *const T {
2860 ManuallyDrop::new(self).as_ptr()
2861 }
2862
2863 /// Consumes the `Weak<T>`, returning the wrapped pointer and allocator.
2864 ///
2865 /// This converts the weak pointer into a raw pointer, while still preserving the ownership of
2866 /// one weak reference (the weak count is not modified by this operation). It can be turned
2867 /// back into the `Weak<T>` with [`from_raw_in`].
2868 ///
2869 /// The same restrictions of accessing the target of the pointer as with
2870 /// [`as_ptr`] apply.
2871 ///
2872 /// # Examples
2873 ///
2874 /// ```
2875 /// #![feature(allocator_api)]
2876 /// use std::sync::{Arc, Weak};
2877 /// use std::alloc::System;
2878 ///
2879 /// let strong = Arc::new_in("hello".to_owned(), System);
2880 /// let weak = Arc::downgrade(&strong);
2881 /// let (raw, alloc) = weak.into_raw_with_allocator();
2882 ///
2883 /// assert_eq!(1, Arc::weak_count(&strong));
2884 /// assert_eq!("hello", unsafe { &*raw });
2885 ///
2886 /// drop(unsafe { Weak::from_raw_in(raw, alloc) });
2887 /// assert_eq!(0, Arc::weak_count(&strong));
2888 /// ```
2889 ///
2890 /// [`from_raw_in`]: Weak::from_raw_in
2891 /// [`as_ptr`]: Weak::as_ptr
2892 #[must_use = "losing the pointer will leak memory"]
2893 #[unstable(feature = "allocator_api", issue = "32838")]
2894 pub fn into_raw_with_allocator(self) -> (*const T, A) {
2895 let this = mem::ManuallyDrop::new(self);
2896 let result = this.as_ptr();
2897 // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
2898 let alloc = unsafe { ptr::read(&this.alloc) };
2899 (result, alloc)
2900 }
2901
2902 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>` in the provided
2903 /// allocator.
2904 ///
2905 /// This can be used to safely get a strong reference (by calling [`upgrade`]
2906 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
2907 ///
2908 /// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
2909 /// as these don't own anything; the method still works on them).
2910 ///
2911 /// # Safety
2912 ///
2913 /// The pointer must have originated from the [`into_raw`] and must still own its potential
2914 /// weak reference, and must point to a block of memory allocated by `alloc`.
2915 ///
2916 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
2917 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
2918 /// count is not modified by this operation) and therefore it must be paired with a previous
2919 /// call to [`into_raw`].
2920 /// # Examples
2921 ///
2922 /// ```
2923 /// use std::sync::{Arc, Weak};
2924 ///
2925 /// let strong = Arc::new("hello".to_owned());
2926 ///
2927 /// let raw_1 = Arc::downgrade(&strong).into_raw();
2928 /// let raw_2 = Arc::downgrade(&strong).into_raw();
2929 ///
2930 /// assert_eq!(2, Arc::weak_count(&strong));
2931 ///
2932 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
2933 /// assert_eq!(1, Arc::weak_count(&strong));
2934 ///
2935 /// drop(strong);
2936 ///
2937 /// // Decrement the last weak count.
2938 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
2939 /// ```
2940 ///
2941 /// [`new`]: Weak::new
2942 /// [`into_raw`]: Weak::into_raw
2943 /// [`upgrade`]: Weak::upgrade
2944 #[inline]
2945 #[unstable(feature = "allocator_api", issue = "32838")]
2946 pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {
2947 // See Weak::as_ptr for context on how the input pointer is derived.
2948
2949 let ptr = if is_dangling(ptr) {
2950 // This is a dangling Weak.
2951 ptr as *mut ArcInner<T>
2952 } else {
2953 // Otherwise, we're guaranteed the pointer came from a nondangling Weak.
2954 // SAFETY: data_offset is safe to call, as ptr references a real (potentially dropped) T.
2955 let offset = unsafe { data_offset(ptr) };
2956 // Thus, we reverse the offset to get the whole RcInner.
2957 // SAFETY: the pointer originated from a Weak, so this offset is safe.
2958 unsafe { ptr.byte_sub(offset) as *mut ArcInner<T> }
2959 };
2960
2961 // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
2962 Weak { ptr: unsafe { NonNull::new_unchecked(ptr) }, alloc }
2963 }
2964}
2965
2966impl<T: ?Sized, A: Allocator> Weak<T, A> {
2967 /// Attempts to upgrade the `Weak` pointer to an [`Arc`], delaying
2968 /// dropping of the inner value if successful.
2969 ///
2970 /// Returns [`None`] if the inner value has since been dropped.
2971 ///
2972 /// # Examples
2973 ///
2974 /// ```
2975 /// use std::sync::Arc;
2976 ///
2977 /// let five = Arc::new(5);
2978 ///
2979 /// let weak_five = Arc::downgrade(&five);
2980 ///
2981 /// let strong_five: Option<Arc<_>> = weak_five.upgrade();
2982 /// assert!(strong_five.is_some());
2983 ///
2984 /// // Destroy all strong pointers.
2985 /// drop(strong_five);
2986 /// drop(five);
2987 ///
2988 /// assert!(weak_five.upgrade().is_none());
2989 /// ```
2990 #[must_use = "this returns a new `Arc`, \
2991 without modifying the original weak pointer"]
2992 #[stable(feature = "arc_weak", since = "1.4.0")]
2993 pub fn upgrade(&self) -> Option<Arc<T, A>>
2994 where
2995 A: Clone,
2996 {
2997 #[inline]
2998 fn checked_increment(n: usize) -> Option<usize> {
2999 // Any write of 0 we can observe leaves the field in permanently zero state.
3000 if n == 0 {
3001 return None;
3002 }
3003 // See comments in `Arc::clone` for why we do this (for `mem::forget`).
3004 assert!(n <= MAX_REFCOUNT, "{}", INTERNAL_OVERFLOW_ERROR);
3005 Some(n + 1)
3006 }
3007
3008 // We use a CAS loop to increment the strong count instead of a
3009 // fetch_add as this function should never take the reference count
3010 // from zero to one.
3011 //
3012 // Relaxed is fine for the failure case because we don't have any expectations about the new state.
3013 // Acquire is necessary for the success case to synchronise with `Arc::new_cyclic`, when the inner
3014 // value can be initialized after `Weak` references have already been created. In that case, we
3015 // expect to observe the fully initialized value.
3016 if self.inner()?.strong.fetch_update(Acquire, Relaxed, checked_increment).is_ok() {
3017 // SAFETY: pointer is not null, verified in checked_increment
3018 unsafe { Some(Arc::from_inner_in(self.ptr, self.alloc.clone())) }
3019 } else {
3020 None
3021 }
3022 }
3023
3024 /// Gets the number of strong (`Arc`) pointers pointing to this allocation.
3025 ///
3026 /// If `self` was created using [`Weak::new`], this will return 0.
3027 #[must_use]
3028 #[stable(feature = "weak_counts", since = "1.41.0")]
3029 pub fn strong_count(&self) -> usize {
3030 if let Some(inner) = self.inner() { inner.strong.load(Relaxed) } else { 0 }
3031 }
3032
3033 /// Gets an approximation of the number of `Weak` pointers pointing to this
3034 /// allocation.
3035 ///
3036 /// If `self` was created using [`Weak::new`], or if there are no remaining
3037 /// strong pointers, this will return 0.
3038 ///
3039 /// # Accuracy
3040 ///
3041 /// Due to implementation details, the returned value can be off by 1 in
3042 /// either direction when other threads are manipulating any `Arc`s or
3043 /// `Weak`s pointing to the same allocation.
3044 #[must_use]
3045 #[stable(feature = "weak_counts", since = "1.41.0")]
3046 pub fn weak_count(&self) -> usize {
3047 if let Some(inner) = self.inner() {
3048 let weak = inner.weak.load(Acquire);
3049 let strong = inner.strong.load(Relaxed);
3050 if strong == 0 {
3051 0
3052 } else {
3053 // Since we observed that there was at least one strong pointer
3054 // after reading the weak count, we know that the implicit weak
3055 // reference (present whenever any strong references are alive)
3056 // was still around when we observed the weak count, and can
3057 // therefore safely subtract it.
3058 weak - 1
3059 }
3060 } else {
3061 0
3062 }
3063 }
3064
3065 /// Returns `None` when the pointer is dangling and there is no allocated `ArcInner`,
3066 /// (i.e., when this `Weak` was created by `Weak::new`).
3067 #[inline]
3068 fn inner(&self) -> Option<WeakInner<'_>> {
3069 let ptr = self.ptr.as_ptr();
3070 if is_dangling(ptr) {
3071 None
3072 } else {
3073 // We are careful to *not* create a reference covering the "data" field, as
3074 // the field may be mutated concurrently (for example, if the last `Arc`
3075 // is dropped, the data field will be dropped in-place).
3076 Some(unsafe { WeakInner { strong: &(*ptr).strong, weak: &(*ptr).weak } })
3077 }
3078 }
3079
3080 /// Returns `true` if the two `Weak`s point to the same allocation similar to [`ptr::eq`], or if
3081 /// both don't point to any allocation (because they were created with `Weak::new()`). However,
3082 /// this function ignores the metadata of `dyn Trait` pointers.
3083 ///
3084 /// # Notes
3085 ///
3086 /// Since this compares pointers it means that `Weak::new()` will equal each
3087 /// other, even though they don't point to any allocation.
3088 ///
3089 /// # Examples
3090 ///
3091 /// ```
3092 /// use std::sync::Arc;
3093 ///
3094 /// let first_rc = Arc::new(5);
3095 /// let first = Arc::downgrade(&first_rc);
3096 /// let second = Arc::downgrade(&first_rc);
3097 ///
3098 /// assert!(first.ptr_eq(&second));
3099 ///
3100 /// let third_rc = Arc::new(5);
3101 /// let third = Arc::downgrade(&third_rc);
3102 ///
3103 /// assert!(!first.ptr_eq(&third));
3104 /// ```
3105 ///
3106 /// Comparing `Weak::new`.
3107 ///
3108 /// ```
3109 /// use std::sync::{Arc, Weak};
3110 ///
3111 /// let first = Weak::new();
3112 /// let second = Weak::new();
3113 /// assert!(first.ptr_eq(&second));
3114 ///
3115 /// let third_rc = Arc::new(());
3116 /// let third = Arc::downgrade(&third_rc);
3117 /// assert!(!first.ptr_eq(&third));
3118 /// ```
3119 ///
3120 /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
3121 #[inline]
3122 #[must_use]
3123 #[stable(feature = "weak_ptr_eq", since = "1.39.0")]
3124 pub fn ptr_eq(&self, other: &Self) -> bool {
3125 ptr::addr_eq(self.ptr.as_ptr(), other.ptr.as_ptr())
3126 }
3127}
3128
3129#[stable(feature = "arc_weak", since = "1.4.0")]
3130impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
3131 /// Makes a clone of the `Weak` pointer that points to the same allocation.
3132 ///
3133 /// # Examples
3134 ///
3135 /// ```
3136 /// use std::sync::{Arc, Weak};
3137 ///
3138 /// let weak_five = Arc::downgrade(&Arc::new(5));
3139 ///
3140 /// let _ = Weak::clone(&weak_five);
3141 /// ```
3142 #[inline]
3143 fn clone(&self) -> Weak<T, A> {
3144 if let Some(inner) = self.inner() {
3145 // See comments in Arc::clone() for why this is relaxed. This can use a
3146 // fetch_add (ignoring the lock) because the weak count is only locked
3147 // where are *no other* weak pointers in existence. (So we can't be
3148 // running this code in that case).
3149 let old_size = inner.weak.fetch_add(1, Relaxed);
3150
3151 // See comments in Arc::clone() for why we do this (for mem::forget).
3152 if old_size > MAX_REFCOUNT {
3153 abort();
3154 }
3155 }
3156
3157 Weak { ptr: self.ptr, alloc: self.alloc.clone() }
3158 }
3159}
3160
3161#[stable(feature = "downgraded_weak", since = "1.10.0")]
3162impl<T> Default for Weak<T> {
3163 /// Constructs a new `Weak<T>`, without allocating memory.
3164 /// Calling [`upgrade`] on the return value always
3165 /// gives [`None`].
3166 ///
3167 /// [`upgrade`]: Weak::upgrade
3168 ///
3169 /// # Examples
3170 ///
3171 /// ```
3172 /// use std::sync::Weak;
3173 ///
3174 /// let empty: Weak<i64> = Default::default();
3175 /// assert!(empty.upgrade().is_none());
3176 /// ```
3177 fn default() -> Weak<T> {
3178 Weak::new()
3179 }
3180}
3181
3182#[stable(feature = "arc_weak", since = "1.4.0")]
3183unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Weak<T, A> {
3184 /// Drops the `Weak` pointer.
3185 ///
3186 /// # Examples
3187 ///
3188 /// ```
3189 /// use std::sync::{Arc, Weak};
3190 ///
3191 /// struct Foo;
3192 ///
3193 /// impl Drop for Foo {
3194 /// fn drop(&mut self) {
3195 /// println!("dropped!");
3196 /// }
3197 /// }
3198 ///
3199 /// let foo = Arc::new(Foo);
3200 /// let weak_foo = Arc::downgrade(&foo);
3201 /// let other_weak_foo = Weak::clone(&weak_foo);
3202 ///
3203 /// drop(weak_foo); // Doesn't print anything
3204 /// drop(foo); // Prints "dropped!"
3205 ///
3206 /// assert!(other_weak_foo.upgrade().is_none());
3207 /// ```
3208 fn drop(&mut self) {
3209 // If we find out that we were the last weak pointer, then its time to
3210 // deallocate the data entirely. See the discussion in Arc::drop() about
3211 // the memory orderings
3212 //
3213 // It's not necessary to check for the locked state here, because the
3214 // weak count can only be locked if there was precisely one weak ref,
3215 // meaning that drop could only subsequently run ON that remaining weak
3216 // ref, which can only happen after the lock is released.
3217 let inner = if let Some(inner) = self.inner() { inner } else { return };
3218
3219 if inner.weak.fetch_sub(1, Release) == 1 {
3220 acquire!(inner.weak);
3221
3222 // Make sure we aren't trying to "deallocate" the shared static for empty slices
3223 // used by Default::default.
3224 debug_assert!(
3225 !ptr::addr_eq(self.ptr.as_ptr(), &STATIC_INNER_SLICE.inner),
3226 "Arc/Weaks backed by a static should never be deallocated. \
3227 Likely decrement_strong_count or from_raw were called too many times.",
3228 );
3229
3230 unsafe {
3231 self.alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()))
3232 }
3233 }
3234 }
3235}
3236
3237#[stable(feature = "rust1", since = "1.0.0")]
3238trait ArcEqIdent<T: ?Sized + PartialEq, A: Allocator> {
3239 fn eq(&self, other: &Arc<T, A>) -> bool;
3240 fn ne(&self, other: &Arc<T, A>) -> bool;
3241}
3242
3243#[stable(feature = "rust1", since = "1.0.0")]
3244impl<T: ?Sized + PartialEq, A: Allocator> ArcEqIdent<T, A> for Arc<T, A> {
3245 #[inline]
3246 default fn eq(&self, other: &Arc<T, A>) -> bool {
3247 **self == **other
3248 }
3249 #[inline]
3250 default fn ne(&self, other: &Arc<T, A>) -> bool {
3251 **self != **other
3252 }
3253}
3254
3255/// We're doing this specialization here, and not as a more general optimization on `&T`, because it
3256/// would otherwise add a cost to all equality checks on refs. We assume that `Arc`s are used to
3257/// store large values, that are slow to clone, but also heavy to check for equality, causing this
3258/// cost to pay off more easily. It's also more likely to have two `Arc` clones, that point to
3259/// the same value, than two `&T`s.
3260///
3261/// We can only do this when `T: Eq` as a `PartialEq` might be deliberately irreflexive.
3262#[stable(feature = "rust1", since = "1.0.0")]
3263impl<T: ?Sized + crate::rc::MarkerEq, A: Allocator> ArcEqIdent<T, A> for Arc<T, A> {
3264 #[inline]
3265 fn eq(&self, other: &Arc<T, A>) -> bool {
3266 Arc::ptr_eq(self, other) || **self == **other
3267 }
3268
3269 #[inline]
3270 fn ne(&self, other: &Arc<T, A>) -> bool {
3271 !Arc::ptr_eq(self, other) && **self != **other
3272 }
3273}
3274
3275#[stable(feature = "rust1", since = "1.0.0")]
3276impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Arc<T, A> {
3277 /// Equality for two `Arc`s.
3278 ///
3279 /// Two `Arc`s are equal if their inner values are equal, even if they are
3280 /// stored in different allocation.
3281 ///
3282 /// If `T` also implements `Eq` (implying reflexivity of equality),
3283 /// two `Arc`s that point to the same allocation are always equal.
3284 ///
3285 /// # Examples
3286 ///
3287 /// ```
3288 /// use std::sync::Arc;
3289 ///
3290 /// let five = Arc::new(5);
3291 ///
3292 /// assert!(five == Arc::new(5));
3293 /// ```
3294 #[inline]
3295 fn eq(&self, other: &Arc<T, A>) -> bool {
3296 ArcEqIdent::eq(self, other)
3297 }
3298
3299 /// Inequality for two `Arc`s.
3300 ///
3301 /// Two `Arc`s are not equal if their inner values are not equal.
3302 ///
3303 /// If `T` also implements `Eq` (implying reflexivity of equality),
3304 /// two `Arc`s that point to the same value are always equal.
3305 ///
3306 /// # Examples
3307 ///
3308 /// ```
3309 /// use std::sync::Arc;
3310 ///
3311 /// let five = Arc::new(5);
3312 ///
3313 /// assert!(five != Arc::new(6));
3314 /// ```
3315 #[inline]
3316 fn ne(&self, other: &Arc<T, A>) -> bool {
3317 ArcEqIdent::ne(self, other)
3318 }
3319}
3320
3321#[stable(feature = "rust1", since = "1.0.0")]
3322impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for Arc<T, A> {
3323 /// Partial comparison for two `Arc`s.
3324 ///
3325 /// The two are compared by calling `partial_cmp()` on their inner values.
3326 ///
3327 /// # Examples
3328 ///
3329 /// ```
3330 /// use std::sync::Arc;
3331 /// use std::cmp::Ordering;
3332 ///
3333 /// let five = Arc::new(5);
3334 ///
3335 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6)));
3336 /// ```
3337 fn partial_cmp(&self, other: &Arc<T, A>) -> Option<Ordering> {
3338 (**self).partial_cmp(&**other)
3339 }
3340
3341 /// Less-than comparison for two `Arc`s.
3342 ///
3343 /// The two are compared by calling `<` on their inner values.
3344 ///
3345 /// # Examples
3346 ///
3347 /// ```
3348 /// use std::sync::Arc;
3349 ///
3350 /// let five = Arc::new(5);
3351 ///
3352 /// assert!(five < Arc::new(6));
3353 /// ```
3354 fn lt(&self, other: &Arc<T, A>) -> bool {
3355 *(*self) < *(*other)
3356 }
3357
3358 /// 'Less than or equal to' comparison for two `Arc`s.
3359 ///
3360 /// The two are compared by calling `<=` on their inner values.
3361 ///
3362 /// # Examples
3363 ///
3364 /// ```
3365 /// use std::sync::Arc;
3366 ///
3367 /// let five = Arc::new(5);
3368 ///
3369 /// assert!(five <= Arc::new(5));
3370 /// ```
3371 fn le(&self, other: &Arc<T, A>) -> bool {
3372 *(*self) <= *(*other)
3373 }
3374
3375 /// Greater-than comparison for two `Arc`s.
3376 ///
3377 /// The two are compared by calling `>` on their inner values.
3378 ///
3379 /// # Examples
3380 ///
3381 /// ```
3382 /// use std::sync::Arc;
3383 ///
3384 /// let five = Arc::new(5);
3385 ///
3386 /// assert!(five > Arc::new(4));
3387 /// ```
3388 fn gt(&self, other: &Arc<T, A>) -> bool {
3389 *(*self) > *(*other)
3390 }
3391
3392 /// 'Greater than or equal to' comparison for two `Arc`s.
3393 ///
3394 /// The two are compared by calling `>=` on their inner values.
3395 ///
3396 /// # Examples
3397 ///
3398 /// ```
3399 /// use std::sync::Arc;
3400 ///
3401 /// let five = Arc::new(5);
3402 ///
3403 /// assert!(five >= Arc::new(5));
3404 /// ```
3405 fn ge(&self, other: &Arc<T, A>) -> bool {
3406 *(*self) >= *(*other)
3407 }
3408}
3409#[stable(feature = "rust1", since = "1.0.0")]
3410impl<T: ?Sized + Ord, A: Allocator> Ord for Arc<T, A> {
3411 /// Comparison for two `Arc`s.
3412 ///
3413 /// The two are compared by calling `cmp()` on their inner values.
3414 ///
3415 /// # Examples
3416 ///
3417 /// ```
3418 /// use std::sync::Arc;
3419 /// use std::cmp::Ordering;
3420 ///
3421 /// let five = Arc::new(5);
3422 ///
3423 /// assert_eq!(Ordering::Less, five.cmp(&Arc::new(6)));
3424 /// ```
3425 fn cmp(&self, other: &Arc<T, A>) -> Ordering {
3426 (**self).cmp(&**other)
3427 }
3428}
3429#[stable(feature = "rust1", since = "1.0.0")]
3430impl<T: ?Sized + Eq, A: Allocator> Eq for Arc<T, A> {}
3431
3432#[stable(feature = "rust1", since = "1.0.0")]
3433impl<T: ?Sized + fmt::Display, A: Allocator> fmt::Display for Arc<T, A> {
3434 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3435 fmt::Display::fmt(&**self, f)
3436 }
3437}
3438
3439#[stable(feature = "rust1", since = "1.0.0")]
3440impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Arc<T, A> {
3441 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3442 fmt::Debug::fmt(&**self, f)
3443 }
3444}
3445
3446#[stable(feature = "rust1", since = "1.0.0")]
3447impl<T: ?Sized, A: Allocator> fmt::Pointer for Arc<T, A> {
3448 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3449 fmt::Pointer::fmt(&(&raw const **self), f)
3450 }
3451}
3452
3453#[cfg(not(no_global_oom_handling))]
3454#[stable(feature = "rust1", since = "1.0.0")]
3455impl<T: Default> Default for Arc<T> {
3456 /// Creates a new `Arc<T>`, with the `Default` value for `T`.
3457 ///
3458 /// # Examples
3459 ///
3460 /// ```
3461 /// use std::sync::Arc;
3462 ///
3463 /// let x: Arc<i32> = Default::default();
3464 /// assert_eq!(*x, 0);
3465 /// ```
3466 fn default() -> Arc<T> {
3467 unsafe {
3468 Self::from_inner(
3469 Box::leak(Box::write(
3470 Box::new_uninit(),
3471 ArcInner {
3472 strong: atomic::AtomicUsize::new(1),
3473 weak: atomic::AtomicUsize::new(1),
3474 data: T::default(),
3475 },
3476 ))
3477 .into(),
3478 )
3479 }
3480 }
3481}
3482
3483/// Struct to hold the static `ArcInner` used for empty `Arc<str/CStr/[T]>` as
3484/// returned by `Default::default`.
3485///
3486/// Layout notes:
3487/// * `repr(align(16))` so we can use it for `[T]` with `align_of::<T>() <= 16`.
3488/// * `repr(C)` so `inner` is at offset 0 (and thus guaranteed to actually be aligned to 16).
3489/// * `[u8; 1]` (to be initialized with 0) so it can be used for `Arc<CStr>`.
3490#[repr(C, align(16))]
3491struct SliceArcInnerForStatic {
3492 inner: ArcInner<[u8; 1]>,
3493}
3494#[cfg(not(no_global_oom_handling))]
3495const MAX_STATIC_INNER_SLICE_ALIGNMENT: usize = 16;
3496
3497static STATIC_INNER_SLICE: SliceArcInnerForStatic = SliceArcInnerForStatic {
3498 inner: ArcInner {
3499 strong: atomic::AtomicUsize::new(1),
3500 weak: atomic::AtomicUsize::new(1),
3501 data: [0],
3502 },
3503};
3504
3505#[cfg(not(no_global_oom_handling))]
3506#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
3507impl Default for Arc<str> {
3508 /// Creates an empty str inside an Arc
3509 ///
3510 /// This may or may not share an allocation with other Arcs.
3511 #[inline]
3512 fn default() -> Self {
3513 let arc: Arc<[u8]> = Default::default();
3514 debug_assert!(core::str::from_utf8(&*arc).is_ok());
3515 let (ptr, alloc) = Arc::into_inner_with_allocator(arc);
3516 unsafe { Arc::from_ptr_in(ptr.as_ptr() as *mut ArcInner<str>, alloc) }
3517 }
3518}
3519
3520#[cfg(not(no_global_oom_handling))]
3521#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
3522impl Default for Arc<core::ffi::CStr> {
3523 /// Creates an empty CStr inside an Arc
3524 ///
3525 /// This may or may not share an allocation with other Arcs.
3526 #[inline]
3527 fn default() -> Self {
3528 use core::ffi::CStr;
3529 let inner: NonNull<ArcInner<[u8]>> = NonNull::from(&STATIC_INNER_SLICE.inner);
3530 let inner: NonNull<ArcInner<CStr>> =
3531 NonNull::new(inner.as_ptr() as *mut ArcInner<CStr>).unwrap();
3532 // `this` semantically is the Arc "owned" by the static, so make sure not to drop it.
3533 let this: mem::ManuallyDrop<Arc<CStr>> =
3534 unsafe { mem::ManuallyDrop::new(Arc::from_inner(inner)) };
3535 (*this).clone()
3536 }
3537}
3538
3539#[cfg(not(no_global_oom_handling))]
3540#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
3541impl<T> Default for Arc<[T]> {
3542 /// Creates an empty `[T]` inside an Arc
3543 ///
3544 /// This may or may not share an allocation with other Arcs.
3545 #[inline]
3546 fn default() -> Self {
3547 if mem::align_of::<T>() <= MAX_STATIC_INNER_SLICE_ALIGNMENT {
3548 // We take a reference to the whole struct instead of the ArcInner<[u8; 1]> inside it so
3549 // we don't shrink the range of bytes the ptr is allowed to access under Stacked Borrows.
3550 // (Miri complains on 32-bit targets with Arc<[Align16]> otherwise.)
3551 // (Note that NonNull::from(&STATIC_INNER_SLICE.inner) is fine under Tree Borrows.)
3552 let inner: NonNull<SliceArcInnerForStatic> = NonNull::from(&STATIC_INNER_SLICE);
3553 let inner: NonNull<ArcInner<[T; 0]>> = inner.cast();
3554 // `this` semantically is the Arc "owned" by the static, so make sure not to drop it.
3555 let this: mem::ManuallyDrop<Arc<[T; 0]>> =
3556 unsafe { mem::ManuallyDrop::new(Arc::from_inner(inner)) };
3557 return (*this).clone();
3558 }
3559
3560 // If T's alignment is too large for the static, make a new unique allocation.
3561 let arr: [T; 0] = [];
3562 Arc::from(arr)
3563 }
3564}
3565
3566#[stable(feature = "rust1", since = "1.0.0")]
3567impl<T: ?Sized + Hash, A: Allocator> Hash for Arc<T, A> {
3568 fn hash<H: Hasher>(&self, state: &mut H) {
3569 (**self).hash(state)
3570 }
3571}
3572
3573#[cfg(not(no_global_oom_handling))]
3574#[stable(feature = "from_for_ptrs", since = "1.6.0")]
3575impl<T> From<T> for Arc<T> {
3576 /// Converts a `T` into an `Arc<T>`
3577 ///
3578 /// The conversion moves the value into a
3579 /// newly allocated `Arc`. It is equivalent to
3580 /// calling `Arc::new(t)`.
3581 ///
3582 /// # Example
3583 /// ```rust
3584 /// # use std::sync::Arc;
3585 /// let x = 5;
3586 /// let arc = Arc::new(5);
3587 ///
3588 /// assert_eq!(Arc::from(x), arc);
3589 /// ```
3590 fn from(t: T) -> Self {
3591 Arc::new(t)
3592 }
3593}
3594
3595#[cfg(not(no_global_oom_handling))]
3596#[stable(feature = "shared_from_array", since = "1.74.0")]
3597impl<T, const N: usize> From<[T; N]> for Arc<[T]> {
3598 /// Converts a [`[T; N]`](prim@array) into an `Arc<[T]>`.
3599 ///
3600 /// The conversion moves the array into a newly allocated `Arc`.
3601 ///
3602 /// # Example
3603 ///
3604 /// ```
3605 /// # use std::sync::Arc;
3606 /// let original: [i32; 3] = [1, 2, 3];
3607 /// let shared: Arc<[i32]> = Arc::from(original);
3608 /// assert_eq!(&[1, 2, 3], &shared[..]);
3609 /// ```
3610 #[inline]
3611 fn from(v: [T; N]) -> Arc<[T]> {
3612 Arc::<[T; N]>::from(v)
3613 }
3614}
3615
3616#[cfg(not(no_global_oom_handling))]
3617#[stable(feature = "shared_from_slice", since = "1.21.0")]
3618impl<T: Clone> From<&[T]> for Arc<[T]> {
3619 /// Allocates a reference-counted slice and fills it by cloning `v`'s items.
3620 ///
3621 /// # Example
3622 ///
3623 /// ```
3624 /// # use std::sync::Arc;
3625 /// let original: &[i32] = &[1, 2, 3];
3626 /// let shared: Arc<[i32]> = Arc::from(original);
3627 /// assert_eq!(&[1, 2, 3], &shared[..]);
3628 /// ```
3629 #[inline]
3630 fn from(v: &[T]) -> Arc<[T]> {
3631 <Self as ArcFromSlice<T>>::from_slice(v)
3632 }
3633}
3634
3635#[cfg(not(no_global_oom_handling))]
3636#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
3637impl<T: Clone> From<&mut [T]> for Arc<[T]> {
3638 /// Allocates a reference-counted slice and fills it by cloning `v`'s items.
3639 ///
3640 /// # Example
3641 ///
3642 /// ```
3643 /// # use std::sync::Arc;
3644 /// let mut original = [1, 2, 3];
3645 /// let original: &mut [i32] = &mut original;
3646 /// let shared: Arc<[i32]> = Arc::from(original);
3647 /// assert_eq!(&[1, 2, 3], &shared[..]);
3648 /// ```
3649 #[inline]
3650 fn from(v: &mut [T]) -> Arc<[T]> {
3651 Arc::from(&*v)
3652 }
3653}
3654
3655#[cfg(not(no_global_oom_handling))]
3656#[stable(feature = "shared_from_slice", since = "1.21.0")]
3657impl From<&str> for Arc<str> {
3658 /// Allocates a reference-counted `str` and copies `v` into it.
3659 ///
3660 /// # Example
3661 ///
3662 /// ```
3663 /// # use std::sync::Arc;
3664 /// let shared: Arc<str> = Arc::from("eggplant");
3665 /// assert_eq!("eggplant", &shared[..]);
3666 /// ```
3667 #[inline]
3668 fn from(v: &str) -> Arc<str> {
3669 let arc = Arc::<[u8]>::from(v.as_bytes());
3670 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const str) }
3671 }
3672}
3673
3674#[cfg(not(no_global_oom_handling))]
3675#[stable(feature = "shared_from_mut_slice", since = "1.84.0")]
3676impl From<&mut str> for Arc<str> {
3677 /// Allocates a reference-counted `str` and copies `v` into it.
3678 ///
3679 /// # Example
3680 ///
3681 /// ```
3682 /// # use std::sync::Arc;
3683 /// let mut original = String::from("eggplant");
3684 /// let original: &mut str = &mut original;
3685 /// let shared: Arc<str> = Arc::from(original);
3686 /// assert_eq!("eggplant", &shared[..]);
3687 /// ```
3688 #[inline]
3689 fn from(v: &mut str) -> Arc<str> {
3690 Arc::from(&*v)
3691 }
3692}
3693
3694#[cfg(not(no_global_oom_handling))]
3695#[stable(feature = "shared_from_slice", since = "1.21.0")]
3696impl From<String> for Arc<str> {
3697 /// Allocates a reference-counted `str` and copies `v` into it.
3698 ///
3699 /// # Example
3700 ///
3701 /// ```
3702 /// # use std::sync::Arc;
3703 /// let unique: String = "eggplant".to_owned();
3704 /// let shared: Arc<str> = Arc::from(unique);
3705 /// assert_eq!("eggplant", &shared[..]);
3706 /// ```
3707 #[inline]
3708 fn from(v: String) -> Arc<str> {
3709 Arc::from(&v[..])
3710 }
3711}
3712
3713#[cfg(not(no_global_oom_handling))]
3714#[stable(feature = "shared_from_slice", since = "1.21.0")]
3715impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Arc<T, A> {
3716 /// Move a boxed object to a new, reference-counted allocation.
3717 ///
3718 /// # Example
3719 ///
3720 /// ```
3721 /// # use std::sync::Arc;
3722 /// let unique: Box<str> = Box::from("eggplant");
3723 /// let shared: Arc<str> = Arc::from(unique);
3724 /// assert_eq!("eggplant", &shared[..]);
3725 /// ```
3726 #[inline]
3727 fn from(v: Box<T, A>) -> Arc<T, A> {
3728 Arc::from_box_in(v)
3729 }
3730}
3731
3732#[cfg(not(no_global_oom_handling))]
3733#[stable(feature = "shared_from_slice", since = "1.21.0")]
3734impl<T, A: Allocator + Clone> From<Vec<T, A>> for Arc<[T], A> {
3735 /// Allocates a reference-counted slice and moves `v`'s items into it.
3736 ///
3737 /// # Example
3738 ///
3739 /// ```
3740 /// # use std::sync::Arc;
3741 /// let unique: Vec<i32> = vec![1, 2, 3];
3742 /// let shared: Arc<[i32]> = Arc::from(unique);
3743 /// assert_eq!(&[1, 2, 3], &shared[..]);
3744 /// ```
3745 #[inline]
3746 fn from(v: Vec<T, A>) -> Arc<[T], A> {
3747 unsafe {
3748 let (vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
3749
3750 let rc_ptr = Self::allocate_for_slice_in(len, &alloc);
3751 ptr::copy_nonoverlapping(vec_ptr, (&raw mut (*rc_ptr).data) as *mut T, len);
3752
3753 // Create a `Vec<T, &A>` with length 0, to deallocate the buffer
3754 // without dropping its contents or the allocator
3755 let _ = Vec::from_raw_parts_in(vec_ptr, 0, cap, &alloc);
3756
3757 Self::from_ptr_in(rc_ptr, alloc)
3758 }
3759 }
3760}
3761
3762#[stable(feature = "shared_from_cow", since = "1.45.0")]
3763impl<'a, B> From<Cow<'a, B>> for Arc<B>
3764where
3765 B: ToOwned + ?Sized,
3766 Arc<B>: From<&'a B> + From<B::Owned>,
3767{
3768 /// Creates an atomically reference-counted pointer from a clone-on-write
3769 /// pointer by copying its content.
3770 ///
3771 /// # Example
3772 ///
3773 /// ```rust
3774 /// # use std::sync::Arc;
3775 /// # use std::borrow::Cow;
3776 /// let cow: Cow<'_, str> = Cow::Borrowed("eggplant");
3777 /// let shared: Arc<str> = Arc::from(cow);
3778 /// assert_eq!("eggplant", &shared[..]);
3779 /// ```
3780 #[inline]
3781 fn from(cow: Cow<'a, B>) -> Arc<B> {
3782 match cow {
3783 Cow::Borrowed(s) => Arc::from(s),
3784 Cow::Owned(s) => Arc::from(s),
3785 }
3786 }
3787}
3788
3789#[stable(feature = "shared_from_str", since = "1.62.0")]
3790impl From<Arc<str>> for Arc<[u8]> {
3791 /// Converts an atomically reference-counted string slice into a byte slice.
3792 ///
3793 /// # Example
3794 ///
3795 /// ```
3796 /// # use std::sync::Arc;
3797 /// let string: Arc<str> = Arc::from("eggplant");
3798 /// let bytes: Arc<[u8]> = Arc::from(string);
3799 /// assert_eq!("eggplant".as_bytes(), bytes.as_ref());
3800 /// ```
3801 #[inline]
3802 fn from(rc: Arc<str>) -> Self {
3803 // SAFETY: `str` has the same layout as `[u8]`.
3804 unsafe { Arc::from_raw(Arc::into_raw(rc) as *const [u8]) }
3805 }
3806}
3807
3808#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
3809impl<T, A: Allocator, const N: usize> TryFrom<Arc<[T], A>> for Arc<[T; N], A> {
3810 type Error = Arc<[T], A>;
3811
3812 fn try_from(boxed_slice: Arc<[T], A>) -> Result<Self, Self::Error> {
3813 if boxed_slice.len() == N {
3814 let (ptr, alloc) = Arc::into_inner_with_allocator(boxed_slice);
3815 Ok(unsafe { Arc::from_inner_in(ptr.cast(), alloc) })
3816 } else {
3817 Err(boxed_slice)
3818 }
3819 }
3820}
3821
3822#[cfg(not(no_global_oom_handling))]
3823#[stable(feature = "shared_from_iter", since = "1.37.0")]
3824impl<T> FromIterator<T> for Arc<[T]> {
3825 /// Takes each element in the `Iterator` and collects it into an `Arc<[T]>`.
3826 ///
3827 /// # Performance characteristics
3828 ///
3829 /// ## The general case
3830 ///
3831 /// In the general case, collecting into `Arc<[T]>` is done by first
3832 /// collecting into a `Vec<T>`. That is, when writing the following:
3833 ///
3834 /// ```rust
3835 /// # use std::sync::Arc;
3836 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0).collect();
3837 /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
3838 /// ```
3839 ///
3840 /// this behaves as if we wrote:
3841 ///
3842 /// ```rust
3843 /// # use std::sync::Arc;
3844 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0)
3845 /// .collect::<Vec<_>>() // The first set of allocations happens here.
3846 /// .into(); // A second allocation for `Arc<[T]>` happens here.
3847 /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
3848 /// ```
3849 ///
3850 /// This will allocate as many times as needed for constructing the `Vec<T>`
3851 /// and then it will allocate once for turning the `Vec<T>` into the `Arc<[T]>`.
3852 ///
3853 /// ## Iterators of known length
3854 ///
3855 /// When your `Iterator` implements `TrustedLen` and is of an exact size,
3856 /// a single allocation will be made for the `Arc<[T]>`. For example:
3857 ///
3858 /// ```rust
3859 /// # use std::sync::Arc;
3860 /// let evens: Arc<[u8]> = (0..10).collect(); // Just a single allocation happens here.
3861 /// # assert_eq!(&*evens, &*(0..10).collect::<Vec<_>>());
3862 /// ```
3863 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3864 ToArcSlice::to_arc_slice(iter.into_iter())
3865 }
3866}
3867
3868#[cfg(not(no_global_oom_handling))]
3869/// Specialization trait used for collecting into `Arc<[T]>`.
3870trait ToArcSlice<T>: Iterator<Item = T> + Sized {
3871 fn to_arc_slice(self) -> Arc<[T]>;
3872}
3873
3874#[cfg(not(no_global_oom_handling))]
3875impl<T, I: Iterator<Item = T>> ToArcSlice<T> for I {
3876 default fn to_arc_slice(self) -> Arc<[T]> {
3877 self.collect::<Vec<T>>().into()
3878 }
3879}
3880
3881#[cfg(not(no_global_oom_handling))]
3882impl<T, I: iter::TrustedLen<Item = T>> ToArcSlice<T> for I {
3883 fn to_arc_slice(self) -> Arc<[T]> {
3884 // This is the case for a `TrustedLen` iterator.
3885 let (low, high) = self.size_hint();
3886 if let Some(high) = high {
3887 debug_assert_eq!(
3888 low,
3889 high,
3890 "TrustedLen iterator's size hint is not exact: {:?}",
3891 (low, high)
3892 );
3893
3894 unsafe {
3895 // SAFETY: We need to ensure that the iterator has an exact length and we have.
3896 Arc::from_iter_exact(self, low)
3897 }
3898 } else {
3899 // TrustedLen contract guarantees that `upper_bound == None` implies an iterator
3900 // length exceeding `usize::MAX`.
3901 // The default implementation would collect into a vec which would panic.
3902 // Thus we panic here immediately without invoking `Vec` code.
3903 panic!("capacity overflow");
3904 }
3905 }
3906}
3907
3908#[stable(feature = "rust1", since = "1.0.0")]
3909impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for Arc<T, A> {
3910 fn borrow(&self) -> &T {
3911 &**self
3912 }
3913}
3914
3915#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
3916impl<T: ?Sized, A: Allocator> AsRef<T> for Arc<T, A> {
3917 fn as_ref(&self) -> &T {
3918 &**self
3919 }
3920}
3921
3922#[stable(feature = "pin", since = "1.33.0")]
3923impl<T: ?Sized, A: Allocator> Unpin for Arc<T, A> {}
3924
3925/// Gets the offset within an `ArcInner` for the payload behind a pointer.
3926///
3927/// # Safety
3928///
3929/// The pointer must point to (and have valid metadata for) a previously
3930/// valid instance of T, but the T is allowed to be dropped.
3931unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
3932 // Align the unsized value to the end of the ArcInner.
3933 // Because RcInner is repr(C), it will always be the last field in memory.
3934 // SAFETY: since the only unsized types possible are slices, trait objects,
3935 // and extern types, the input safety requirement is currently enough to
3936 // satisfy the requirements of align_of_val_raw; this is an implementation
3937 // detail of the language that must not be relied upon outside of std.
3938 unsafe { data_offset_align(align_of_val_raw(ptr)) }
3939}
3940
3941#[inline]
3942fn data_offset_align(align: usize) -> usize {
3943 let layout = Layout::new::<ArcInner<()>>();
3944 layout.size() + layout.padding_needed_for(align)
3945}
3946
3947/// A unique owning pointer to an [`ArcInner`] **that does not imply the contents are initialized,**
3948/// but will deallocate it (without dropping the value) when dropped.
3949///
3950/// This is a helper for [`Arc::make_mut()`] to ensure correct cleanup on panic.
3951#[cfg(not(no_global_oom_handling))]
3952struct UniqueArcUninit<T: ?Sized, A: Allocator> {
3953 ptr: NonNull<ArcInner<T>>,
3954 layout_for_value: Layout,
3955 alloc: Option<A>,
3956}
3957
3958#[cfg(not(no_global_oom_handling))]
3959impl<T: ?Sized, A: Allocator> UniqueArcUninit<T, A> {
3960 /// Allocates an ArcInner with layout suitable to contain `for_value` or a clone of it.
3961 fn new(for_value: &T, alloc: A) -> UniqueArcUninit<T, A> {
3962 let layout = Layout::for_value(for_value);
3963 let ptr = unsafe {
3964 Arc::allocate_for_layout(
3965 layout,
3966 |layout_for_arcinner| alloc.allocate(layout_for_arcinner),
3967 |mem| mem.with_metadata_of(ptr::from_ref(for_value) as *const ArcInner<T>),
3968 )
3969 };
3970 Self { ptr: NonNull::new(ptr).unwrap(), layout_for_value: layout, alloc: Some(alloc) }
3971 }
3972
3973 /// Returns the pointer to be written into to initialize the [`Arc`].
3974 fn data_ptr(&mut self) -> *mut T {
3975 let offset = data_offset_align(self.layout_for_value.align());
3976 unsafe { self.ptr.as_ptr().byte_add(offset) as *mut T }
3977 }
3978
3979 /// Upgrade this into a normal [`Arc`].
3980 ///
3981 /// # Safety
3982 ///
3983 /// The data must have been initialized (by writing to [`Self::data_ptr()`]).
3984 unsafe fn into_arc(self) -> Arc<T, A> {
3985 let mut this = ManuallyDrop::new(self);
3986 let ptr = this.ptr.as_ptr();
3987 let alloc = this.alloc.take().unwrap();
3988
3989 // SAFETY: The pointer is valid as per `UniqueArcUninit::new`, and the caller is responsible
3990 // for having initialized the data.
3991 unsafe { Arc::from_ptr_in(ptr, alloc) }
3992 }
3993}
3994
3995#[cfg(not(no_global_oom_handling))]
3996impl<T: ?Sized, A: Allocator> Drop for UniqueArcUninit<T, A> {
3997 fn drop(&mut self) {
3998 // SAFETY:
3999 // * new() produced a pointer safe to deallocate.
4000 // * We own the pointer unless into_arc() was called, which forgets us.
4001 unsafe {
4002 self.alloc.take().unwrap().deallocate(
4003 self.ptr.cast(),
4004 arcinner_layout_for_value_layout(self.layout_for_value),
4005 );
4006 }
4007 }
4008}
4009
4010#[stable(feature = "arc_error", since = "1.52.0")]
4011impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4012 #[allow(deprecated, deprecated_in_future)]
4013 fn description(&self) -> &str {
4014 core::error::Error::description(&**self)
4015 }
4016
4017 #[allow(deprecated)]
4018 fn cause(&self) -> Option<&dyn core::error::Error> {
4019 core::error::Error::cause(&**self)
4020 }
4021
4022 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
4023 core::error::Error::source(&**self)
4024 }
4025
4026 fn provide<'a>(&'a self, req: &mut core::error::Request<'a>) {
4027 core::error::Error::provide(&**self, req);
4028 }
4029}