1 #![stable(feature = "rust1", since = "1.0.0")]
3 //! Thread-safe reference-counting pointers.
5 //! See the [`Arc<T>`][Arc] documentation for more details.
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")]`.
13 use core
::cmp
::Ordering
;
15 use core
::hash
::{Hash, Hasher}
;
17 use core
::intrinsics
::abort
;
18 #[cfg(not(no_global_oom_handling))]
20 use core
::marker
::{PhantomData, Unsize}
;
21 #[cfg(not(no_global_oom_handling))]
22 use core
::mem
::size_of_val
;
23 use core
::mem
::{self, align_of_val_raw}
;
24 use core
::ops
::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}
;
25 use core
::panic
::{RefUnwindSafe, UnwindSafe}
;
27 use core
::ptr
::{self, NonNull}
;
28 #[cfg(not(no_global_oom_handling))]
29 use core
::slice
::from_raw_parts_mut
;
30 use core
::sync
::atomic
;
31 use core
::sync
::atomic
::Ordering
::{Acquire, Relaxed, Release}
;
33 #[cfg(not(no_global_oom_handling))]
34 use crate::alloc
::handle_alloc_error
;
35 #[cfg(not(no_global_oom_handling))]
36 use crate::alloc
::WriteCloneIntoRaw
;
37 use crate::alloc
::{AllocError, Allocator, Global, Layout}
;
38 use crate::borrow
::{Cow, ToOwned}
;
39 use crate::boxed
::Box
;
40 use crate::rc
::is_dangling
;
41 #[cfg(not(no_global_oom_handling))]
42 use crate::string
::String
;
43 #[cfg(not(no_global_oom_handling))]
49 /// A soft limit on the amount of references that may be made to an `Arc`.
51 /// Going above this limit will abort your program (although not
52 /// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
53 /// Trying to go above it might call a `panic` (if not actually going above it).
55 /// This is a global invariant, and also applies when using a compare-exchange loop.
57 /// See comment in `Arc::clone`.
58 const MAX_REFCOUNT
: usize = (isize::MAX
) as usize;
60 /// The error in case either counter reaches above `MAX_REFCOUNT`, and we can `panic` safely.
61 const INTERNAL_OVERFLOW_ERROR
: &str = "Arc counter overflow";
63 #[cfg(not(sanitize = "thread"))]
64 macro_rules
! acquire
{
66 atomic
::fence(Acquire
)
70 // ThreadSanitizer does not support memory fences. To avoid false positive
71 // reports in Arc / Weak implementation use atomic loads for synchronization
73 #[cfg(sanitize = "thread")]
74 macro_rules
! acquire
{
80 /// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
81 /// Reference Counted'.
83 /// The type `Arc<T>` provides shared ownership of a value of type `T`,
84 /// allocated in the heap. Invoking [`clone`][clone] on `Arc` produces
85 /// a new `Arc` instance, which points to the same allocation on the heap as the
86 /// source `Arc`, while increasing a reference count. When the last `Arc`
87 /// pointer to a given allocation is destroyed, the value stored in that allocation (often
88 /// referred to as "inner value") is also dropped.
90 /// Shared references in Rust disallow mutation by default, and `Arc` is no
91 /// exception: you cannot generally obtain a mutable reference to something
92 /// inside an `Arc`. If you need to mutate through an `Arc`, use
93 /// [`Mutex`][mutex], [`RwLock`][rwlock], or one of the [`Atomic`][atomic]
96 /// **Note**: This type is only available on platforms that support atomic
97 /// loads and stores of pointers, which includes all platforms that support
98 /// the `std` crate but not all those which only support [`alloc`](crate).
99 /// This may be detected at compile time using `#[cfg(target_has_atomic = "ptr")]`.
103 /// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
104 /// counting. This means that it is thread-safe. The disadvantage is that
105 /// atomic operations are more expensive than ordinary memory accesses. If you
106 /// are not sharing reference-counted allocations between threads, consider using
107 /// [`Rc<T>`] for lower overhead. [`Rc<T>`] is a safe default, because the
108 /// compiler will catch any attempt to send an [`Rc<T>`] between threads.
109 /// However, a library might choose `Arc<T>` in order to give library consumers
110 /// more flexibility.
112 /// `Arc<T>` will implement [`Send`] and [`Sync`] as long as the `T` implements
113 /// [`Send`] and [`Sync`]. Why can't you put a non-thread-safe type `T` in an
114 /// `Arc<T>` to make it thread-safe? This may be a bit counter-intuitive at
115 /// first: after all, isn't the point of `Arc<T>` thread safety? The key is
116 /// this: `Arc<T>` makes it thread safe to have multiple ownership of the same
117 /// data, but it doesn't add thread safety to its data. Consider
118 /// <code>Arc<[RefCell\<T>]></code>. [`RefCell<T>`] isn't [`Sync`], and if `Arc<T>` was always
119 /// [`Send`], <code>Arc<[RefCell\<T>]></code> would be as well. But then we'd have a problem:
120 /// [`RefCell<T>`] is not thread safe; it keeps track of the borrowing count using
121 /// non-atomic operations.
123 /// In the end, this means that you may need to pair `Arc<T>` with some sort of
124 /// [`std::sync`] type, usually [`Mutex<T>`][mutex].
126 /// ## Breaking cycles with `Weak`
128 /// The [`downgrade`][downgrade] method can be used to create a non-owning
129 /// [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
130 /// to an `Arc`, but this will return [`None`] if the value stored in the allocation has
131 /// already been dropped. In other words, `Weak` pointers do not keep the value
132 /// inside the allocation alive; however, they *do* keep the allocation
133 /// (the backing store for the value) alive.
135 /// A cycle between `Arc` pointers will never be deallocated. For this reason,
136 /// [`Weak`] is used to break cycles. For example, a tree could have
137 /// strong `Arc` pointers from parent nodes to children, and [`Weak`]
138 /// pointers from children back to their parents.
140 /// # Cloning references
142 /// Creating a new reference from an existing reference-counted pointer is done using the
143 /// `Clone` trait implemented for [`Arc<T>`][Arc] and [`Weak<T>`][Weak].
146 /// use std::sync::Arc;
147 /// let foo = Arc::new(vec![1.0, 2.0, 3.0]);
148 /// // The two syntaxes below are equivalent.
149 /// let a = foo.clone();
150 /// let b = Arc::clone(&foo);
151 /// // a, b, and foo are all Arcs that point to the same memory location
154 /// ## `Deref` behavior
156 /// `Arc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
157 /// so you can call `T`'s methods on a value of type `Arc<T>`. To avoid name
158 /// clashes with `T`'s methods, the methods of `Arc<T>` itself are associated
159 /// functions, called using [fully qualified syntax]:
162 /// use std::sync::Arc;
164 /// let my_arc = Arc::new(());
165 /// let my_weak = Arc::downgrade(&my_arc);
168 /// `Arc<T>`'s implementations of traits like `Clone` may also be called using
169 /// fully qualified syntax. Some people prefer to use fully qualified syntax,
170 /// while others prefer using method-call syntax.
173 /// use std::sync::Arc;
175 /// let arc = Arc::new(());
176 /// // Method-call syntax
177 /// let arc2 = arc.clone();
178 /// // Fully qualified syntax
179 /// let arc3 = Arc::clone(&arc);
182 /// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
183 /// already been dropped.
185 /// [`Rc<T>`]: crate::rc::Rc
186 /// [clone]: Clone::clone
187 /// [mutex]: ../../std/sync/struct.Mutex.html
188 /// [rwlock]: ../../std/sync/struct.RwLock.html
189 /// [atomic]: core::sync::atomic
190 /// [downgrade]: Arc::downgrade
191 /// [upgrade]: Weak::upgrade
192 /// [RefCell\<T>]: core::cell::RefCell
193 /// [`RefCell<T>`]: core::cell::RefCell
194 /// [`std::sync`]: ../../std/sync/index.html
195 /// [`Arc::clone(&from)`]: Arc::clone
196 /// [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
200 /// Sharing some immutable data between threads:
202 // Note that we **do not** run these tests here. The windows builders get super
203 // unhappy if a thread outlives the main thread and then exits at the same time
204 // (something deadlocks) so we just avoid this entirely by not running these
207 /// use std::sync::Arc;
210 /// let five = Arc::new(5);
213 /// let five = Arc::clone(&five);
215 /// thread::spawn(move || {
216 /// println!("{five:?}");
221 /// Sharing a mutable [`AtomicUsize`]:
223 /// [`AtomicUsize`]: core::sync::atomic::AtomicUsize "sync::atomic::AtomicUsize"
226 /// use std::sync::Arc;
227 /// use std::sync::atomic::{AtomicUsize, Ordering};
230 /// let val = Arc::new(AtomicUsize::new(5));
233 /// let val = Arc::clone(&val);
235 /// thread::spawn(move || {
236 /// let v = val.fetch_add(1, Ordering::SeqCst);
237 /// println!("{v:?}");
242 /// See the [`rc` documentation][rc_examples] for more examples of reference
243 /// counting in general.
245 /// [rc_examples]: crate::rc#examples
246 #[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
247 #[stable(feature = "rust1", since = "1.0.0")]
250 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
252 ptr
: NonNull
<ArcInner
<T
>>,
253 phantom
: PhantomData
<ArcInner
<T
>>,
257 #[stable(feature = "rust1", since = "1.0.0")]
258 unsafe impl<T
: ?Sized
+ Sync
+ Send
, A
: Allocator
+ Send
> Send
for Arc
<T
, A
> {}
259 #[stable(feature = "rust1", since = "1.0.0")]
260 unsafe impl<T
: ?Sized
+ Sync
+ Send
, A
: Allocator
+ Sync
> Sync
for Arc
<T
, A
> {}
262 #[stable(feature = "catch_unwind", since = "1.9.0")]
263 impl<T
: RefUnwindSafe
+ ?Sized
, A
: Allocator
+ UnwindSafe
> UnwindSafe
for Arc
<T
, A
> {}
265 #[unstable(feature = "coerce_unsized", issue = "18598")]
266 impl<T
: ?Sized
+ Unsize
<U
>, U
: ?Sized
, A
: Allocator
> CoerceUnsized
<Arc
<U
, A
>> for Arc
<T
, A
> {}
268 #[unstable(feature = "dispatch_from_dyn", issue = "none")]
269 impl<T
: ?Sized
+ Unsize
<U
>, U
: ?Sized
> DispatchFromDyn
<Arc
<U
>> for Arc
<T
> {}
271 impl<T
: ?Sized
> Arc
<T
> {
272 unsafe fn from_inner(ptr
: NonNull
<ArcInner
<T
>>) -> Self {
273 unsafe { Self::from_inner_in(ptr, Global) }
276 unsafe fn from_ptr(ptr
: *mut ArcInner
<T
>) -> Self {
277 unsafe { Self::from_ptr_in(ptr, Global) }
281 impl<T
: ?Sized
, A
: Allocator
> Arc
<T
, A
> {
283 unsafe fn from_inner_in(ptr
: NonNull
<ArcInner
<T
>>, alloc
: A
) -> Self {
284 Self { ptr, phantom: PhantomData, alloc }
288 unsafe fn from_ptr_in(ptr
: *mut ArcInner
<T
>, alloc
: A
) -> Self {
289 unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) }
293 /// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
294 /// managed allocation. The allocation is accessed by calling [`upgrade`] on the `Weak`
295 /// pointer, which returns an <code>[Option]<[Arc]\<T>></code>.
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.
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.
310 /// The typical way to obtain a `Weak` pointer is to call [`Arc::downgrade`].
312 /// [`upgrade`]: Weak::upgrade
313 #[stable(feature = "arc_weak", since = "1.4.0")]
316 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
318 // This is a `NonNull` to allow optimizing the size of this type in enums,
319 // but it is not necessarily a valid pointer.
320 // `Weak::new` sets this to `usize::MAX` so that it doesn’t need
321 // to allocate space on the heap. That's not a value a real pointer
322 // will ever have because RcBox has alignment at least 2.
323 // This is only possible when `T: Sized`; unsized `T` never dangle.
324 ptr
: NonNull
<ArcInner
<T
>>,
328 #[stable(feature = "arc_weak", since = "1.4.0")]
329 unsafe impl<T
: ?Sized
+ Sync
+ Send
, A
: Allocator
+ Send
> Send
for Weak
<T
, A
> {}
330 #[stable(feature = "arc_weak", since = "1.4.0")]
331 unsafe impl<T
: ?Sized
+ Sync
+ Send
, A
: Allocator
+ Sync
> Sync
for Weak
<T
, A
> {}
333 #[unstable(feature = "coerce_unsized", issue = "18598")]
334 impl<T
: ?Sized
+ Unsize
<U
>, U
: ?Sized
, A
: Allocator
> CoerceUnsized
<Weak
<U
, A
>> for Weak
<T
, A
> {}
335 #[unstable(feature = "dispatch_from_dyn", issue = "none")]
336 impl<T
: ?Sized
+ Unsize
<U
>, U
: ?Sized
> DispatchFromDyn
<Weak
<U
>> for Weak
<T
> {}
338 #[stable(feature = "arc_weak", since = "1.4.0")]
339 impl<T
: ?Sized
> fmt
::Debug
for Weak
<T
> {
340 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
345 // This is repr(C) to future-proof against possible field-reordering, which
346 // would interfere with otherwise safe [into|from]_raw() of transmutable
349 struct ArcInner
<T
: ?Sized
> {
350 strong
: atomic
::AtomicUsize
,
352 // the value usize::MAX acts as a sentinel for temporarily "locking" the
353 // ability to upgrade weak pointers or downgrade strong ones; this is used
354 // to avoid races in `make_mut` and `get_mut`.
355 weak
: atomic
::AtomicUsize
,
360 /// Calculate layout for `ArcInner<T>` using the inner value's layout
361 fn arcinner_layout_for_value_layout(layout
: Layout
) -> Layout
{
362 // Calculate layout using the given value layout.
363 // Previously, layout was calculated on the expression
364 // `&*(ptr as *const ArcInner<T>)`, but this created a misaligned
365 // reference (see #54908).
366 Layout
::new
::<ArcInner
<()>>().extend(layout
).unwrap().0.pad_to_align()
369 unsafe impl<T
: ?Sized
+ Sync
+ Send
> Send
for ArcInner
<T
> {}
370 unsafe impl<T
: ?Sized
+ Sync
+ Send
> Sync
for ArcInner
<T
> {}
373 /// Constructs a new `Arc<T>`.
378 /// use std::sync::Arc;
380 /// let five = Arc::new(5);
382 #[cfg(not(no_global_oom_handling))]
384 #[stable(feature = "rust1", since = "1.0.0")]
385 pub fn new(data
: T
) -> Arc
<T
> {
386 // Start the weak pointer count as 1 which is the weak pointer that's
387 // held by all the strong pointers (kinda), see std/rc.rs for more info
388 let x
: Box
<_
> = Box
::new(ArcInner
{
389 strong
: atomic
::AtomicUsize
::new(1),
390 weak
: atomic
::AtomicUsize
::new(1),
393 unsafe { Self::from_inner(Box::leak(x).into()) }
396 /// Constructs a new `Arc<T>` while giving you a `Weak<T>` to the allocation,
397 /// to allow you to construct a `T` which holds a weak pointer to itself.
399 /// Generally, a structure circularly referencing itself, either directly or
400 /// indirectly, should not hold a strong reference to itself to prevent a memory leak.
401 /// Using this function, you get access to the weak pointer during the
402 /// initialization of `T`, before the `Arc<T>` is created, such that you can
403 /// clone and store it inside the `T`.
405 /// `new_cyclic` first allocates the managed allocation for the `Arc<T>`,
406 /// then calls your closure, giving it a `Weak<T>` to this allocation,
407 /// and only afterwards completes the construction of the `Arc<T>` by placing
408 /// the `T` returned from your closure into the allocation.
410 /// Since the new `Arc<T>` is not fully-constructed until `Arc<T>::new_cyclic`
411 /// returns, calling [`upgrade`] on the weak reference inside your closure will
412 /// fail and result in a `None` value.
416 /// If `data_fn` panics, the panic is propagated to the caller, and the
417 /// temporary [`Weak<T>`] is dropped normally.
422 /// # #![allow(dead_code)]
423 /// use std::sync::{Arc, Weak};
426 /// me: Weak<Gadget>,
430 /// /// Construct a reference counted Gadget.
431 /// fn new() -> Arc<Self> {
432 /// // `me` is a `Weak<Gadget>` pointing at the new allocation of the
433 /// // `Arc` we're constructing.
434 /// Arc::new_cyclic(|me| {
435 /// // Create the actual struct here.
436 /// Gadget { me: me.clone() }
440 /// /// Return a reference counted pointer to Self.
441 /// fn me(&self) -> Arc<Self> {
442 /// self.me.upgrade().unwrap()
446 /// [`upgrade`]: Weak::upgrade
447 #[cfg(not(no_global_oom_handling))]
449 #[stable(feature = "arc_new_cyclic", since = "1.60.0")]
450 pub fn new_cyclic
<F
>(data_fn
: F
) -> Arc
<T
>
452 F
: FnOnce(&Weak
<T
>) -> T
,
454 // Construct the inner in the "uninitialized" state with a single
456 let uninit_ptr
: NonNull
<_
> = Box
::leak(Box
::new(ArcInner
{
457 strong
: atomic
::AtomicUsize
::new(0),
458 weak
: atomic
::AtomicUsize
::new(1),
459 data
: mem
::MaybeUninit
::<T
>::uninit(),
462 let init_ptr
: NonNull
<ArcInner
<T
>> = uninit_ptr
.cast();
464 let weak
= Weak { ptr: init_ptr, alloc: Global }
;
466 // It's important we don't give up ownership of the weak pointer, or
467 // else the memory might be freed by the time `data_fn` returns. If
468 // we really wanted to pass ownership, we could create an additional
469 // weak pointer for ourselves, but this would result in additional
470 // updates to the weak reference count which might not be necessary
472 let data
= data_fn(&weak
);
474 // Now we can properly initialize the inner value and turn our weak
475 // reference into a strong reference.
476 let strong
= unsafe {
477 let inner
= init_ptr
.as_ptr();
478 ptr
::write(ptr
::addr_of_mut
!((*inner
).data
), data
);
480 // The above write to the data field must be visible to any threads which
481 // observe a non-zero strong count. Therefore we need at least "Release" ordering
482 // in order to synchronize with the `compare_exchange_weak` in `Weak::upgrade`.
484 // "Acquire" ordering is not required. When considering the possible behaviours
485 // of `data_fn` we only need to look at what it could do with a reference to a
486 // non-upgradeable `Weak`:
487 // - It can *clone* the `Weak`, increasing the weak reference count.
488 // - It can drop those clones, decreasing the weak reference count (but never to zero).
490 // These side effects do not impact us in any way, and no other side effects are
491 // possible with safe code alone.
492 let prev_value
= (*inner
).strong
.fetch_add(1, Release
);
493 debug_assert_eq
!(prev_value
, 0, "No prior strong references should exist");
495 Arc
::from_inner(init_ptr
)
498 // Strong references should collectively own a shared weak reference,
499 // so don't run the destructor for our old weak reference.
504 /// Constructs a new `Arc` with uninitialized contents.
509 /// #![feature(new_uninit)]
510 /// #![feature(get_mut_unchecked)]
512 /// use std::sync::Arc;
514 /// let mut five = Arc::<u32>::new_uninit();
516 /// // Deferred initialization:
517 /// Arc::get_mut(&mut five).unwrap().write(5);
519 /// let five = unsafe { five.assume_init() };
521 /// assert_eq!(*five, 5)
523 #[cfg(not(no_global_oom_handling))]
525 #[unstable(feature = "new_uninit", issue = "63291")]
527 pub fn new_uninit() -> Arc
<mem
::MaybeUninit
<T
>> {
529 Arc
::from_ptr(Arc
::allocate_for_layout(
531 |layout
| Global
.allocate(layout
),
537 /// Constructs a new `Arc` with uninitialized contents, with the memory
538 /// being filled with `0` bytes.
540 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
546 /// #![feature(new_uninit)]
548 /// use std::sync::Arc;
550 /// let zero = Arc::<u32>::new_zeroed();
551 /// let zero = unsafe { zero.assume_init() };
553 /// assert_eq!(*zero, 0)
556 /// [zeroed]: mem::MaybeUninit::zeroed
557 #[cfg(not(no_global_oom_handling))]
559 #[unstable(feature = "new_uninit", issue = "63291")]
561 pub fn new_zeroed() -> Arc
<mem
::MaybeUninit
<T
>> {
563 Arc
::from_ptr(Arc
::allocate_for_layout(
565 |layout
| Global
.allocate_zeroed(layout
),
571 /// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
572 /// `data` will be pinned in memory and unable to be moved.
573 #[cfg(not(no_global_oom_handling))]
574 #[stable(feature = "pin", since = "1.33.0")]
576 pub fn pin(data
: T
) -> Pin
<Arc
<T
>> {
577 unsafe { Pin::new_unchecked(Arc::new(data)) }
580 /// Constructs a new `Pin<Arc<T>>`, return an error if allocation fails.
581 #[unstable(feature = "allocator_api", issue = "32838")]
583 pub fn try_pin(data
: T
) -> Result
<Pin
<Arc
<T
>>, AllocError
> {
584 unsafe { Ok(Pin::new_unchecked(Arc::try_new(data)?)) }
587 /// Constructs a new `Arc<T>`, returning an error if allocation fails.
592 /// #![feature(allocator_api)]
593 /// use std::sync::Arc;
595 /// let five = Arc::try_new(5)?;
596 /// # Ok::<(), std::alloc::AllocError>(())
598 #[unstable(feature = "allocator_api", issue = "32838")]
600 pub fn try_new(data
: T
) -> Result
<Arc
<T
>, AllocError
> {
601 // Start the weak pointer count as 1 which is the weak pointer that's
602 // held by all the strong pointers (kinda), see std/rc.rs for more info
603 let x
: Box
<_
> = Box
::try_new(ArcInner
{
604 strong
: atomic
::AtomicUsize
::new(1),
605 weak
: atomic
::AtomicUsize
::new(1),
608 unsafe { Ok(Self::from_inner(Box::leak(x).into())) }
611 /// Constructs a new `Arc` with uninitialized contents, returning an error
612 /// if allocation fails.
617 /// #![feature(new_uninit, allocator_api)]
618 /// #![feature(get_mut_unchecked)]
620 /// use std::sync::Arc;
622 /// let mut five = Arc::<u32>::try_new_uninit()?;
624 /// // Deferred initialization:
625 /// Arc::get_mut(&mut five).unwrap().write(5);
627 /// let five = unsafe { five.assume_init() };
629 /// assert_eq!(*five, 5);
630 /// # Ok::<(), std::alloc::AllocError>(())
632 #[unstable(feature = "allocator_api", issue = "32838")]
633 // #[unstable(feature = "new_uninit", issue = "63291")]
634 pub fn try_new_uninit() -> Result
<Arc
<mem
::MaybeUninit
<T
>>, AllocError
> {
636 Ok(Arc
::from_ptr(Arc
::try_allocate_for_layout(
638 |layout
| Global
.allocate(layout
),
644 /// Constructs a new `Arc` with uninitialized contents, with the memory
645 /// being filled with `0` bytes, returning an error if allocation fails.
647 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
653 /// #![feature(new_uninit, allocator_api)]
655 /// use std::sync::Arc;
657 /// let zero = Arc::<u32>::try_new_zeroed()?;
658 /// let zero = unsafe { zero.assume_init() };
660 /// assert_eq!(*zero, 0);
661 /// # Ok::<(), std::alloc::AllocError>(())
664 /// [zeroed]: mem::MaybeUninit::zeroed
665 #[unstable(feature = "allocator_api", issue = "32838")]
666 // #[unstable(feature = "new_uninit", issue = "63291")]
667 pub fn try_new_zeroed() -> Result
<Arc
<mem
::MaybeUninit
<T
>>, AllocError
> {
669 Ok(Arc
::from_ptr(Arc
::try_allocate_for_layout(
671 |layout
| Global
.allocate_zeroed(layout
),
678 impl<T
, A
: Allocator
> Arc
<T
, A
> {
679 /// Returns a reference to the underlying allocator.
681 /// Note: this is an associated function, which means that you have
682 /// to call it as `Arc::allocator(&a)` instead of `a.allocator()`. This
683 /// is so that there is no conflict with a method on the inner type.
685 #[unstable(feature = "allocator_api", issue = "32838")]
686 pub fn allocator(this
: &Self) -> &A
{
689 /// Constructs a new `Arc<T>` in the provided allocator.
694 /// #![feature(allocator_api)]
696 /// use std::sync::Arc;
697 /// use std::alloc::System;
699 /// let five = Arc::new_in(5, System);
702 #[cfg(not(no_global_oom_handling))]
703 #[unstable(feature = "allocator_api", issue = "32838")]
704 pub fn new_in(data
: T
, alloc
: A
) -> Arc
<T
, A
> {
705 // Start the weak pointer count as 1 which is the weak pointer that's
706 // held by all the strong pointers (kinda), see std/rc.rs for more info
709 strong
: atomic
::AtomicUsize
::new(1),
710 weak
: atomic
::AtomicUsize
::new(1),
715 let (ptr
, alloc
) = Box
::into_unique(x
);
716 unsafe { Self::from_inner_in(ptr.into(), alloc) }
719 /// Constructs a new `Arc` with uninitialized contents in the provided allocator.
724 /// #![feature(new_uninit)]
725 /// #![feature(get_mut_unchecked)]
726 /// #![feature(allocator_api)]
728 /// use std::sync::Arc;
729 /// use std::alloc::System;
731 /// let mut five = Arc::<u32, _>::new_uninit_in(System);
733 /// let five = unsafe {
734 /// // Deferred initialization:
735 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
737 /// five.assume_init()
740 /// assert_eq!(*five, 5)
742 #[cfg(not(no_global_oom_handling))]
743 #[unstable(feature = "allocator_api", issue = "32838")]
744 // #[unstable(feature = "new_uninit", issue = "63291")]
746 pub fn new_uninit_in(alloc
: A
) -> Arc
<mem
::MaybeUninit
<T
>, A
> {
749 Arc
::allocate_for_layout(
751 |layout
| alloc
.allocate(layout
),
759 /// Constructs a new `Arc` with uninitialized contents, with the memory
760 /// being filled with `0` bytes, in the provided allocator.
762 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
768 /// #![feature(new_uninit)]
769 /// #![feature(allocator_api)]
771 /// use std::sync::Arc;
772 /// use std::alloc::System;
774 /// let zero = Arc::<u32, _>::new_zeroed_in(System);
775 /// let zero = unsafe { zero.assume_init() };
777 /// assert_eq!(*zero, 0)
780 /// [zeroed]: mem::MaybeUninit::zeroed
781 #[cfg(not(no_global_oom_handling))]
782 #[unstable(feature = "allocator_api", issue = "32838")]
783 // #[unstable(feature = "new_uninit", issue = "63291")]
785 pub fn new_zeroed_in(alloc
: A
) -> Arc
<mem
::MaybeUninit
<T
>, A
> {
788 Arc
::allocate_for_layout(
790 |layout
| alloc
.allocate_zeroed(layout
),
798 /// Constructs a new `Pin<Arc<T, A>>` in the provided allocator. If `T` does not implement `Unpin`,
799 /// then `data` will be pinned in memory and unable to be moved.
800 #[cfg(not(no_global_oom_handling))]
801 #[unstable(feature = "allocator_api", issue = "32838")]
803 pub fn pin_in(data
: T
, alloc
: A
) -> Pin
<Arc
<T
, A
>> {
804 unsafe { Pin::new_unchecked(Arc::new_in(data, alloc)) }
807 /// Constructs a new `Pin<Arc<T, A>>` in the provided allocator, return an error if allocation
810 #[unstable(feature = "allocator_api", issue = "32838")]
811 pub fn try_pin_in(data
: T
, alloc
: A
) -> Result
<Pin
<Arc
<T
, A
>>, AllocError
> {
812 unsafe { Ok(Pin::new_unchecked(Arc::try_new_in(data, alloc)?)) }
815 /// Constructs a new `Arc<T, A>` in the provided allocator, returning an error if allocation fails.
820 /// #![feature(allocator_api)]
822 /// use std::sync::Arc;
823 /// use std::alloc::System;
825 /// let five = Arc::try_new_in(5, System)?;
826 /// # Ok::<(), std::alloc::AllocError>(())
829 #[unstable(feature = "allocator_api", issue = "32838")]
831 pub fn try_new_in(data
: T
, alloc
: A
) -> Result
<Arc
<T
, A
>, AllocError
> {
832 // Start the weak pointer count as 1 which is the weak pointer that's
833 // held by all the strong pointers (kinda), see std/rc.rs for more info
834 let x
= Box
::try_new_in(
836 strong
: atomic
::AtomicUsize
::new(1),
837 weak
: atomic
::AtomicUsize
::new(1),
842 let (ptr
, alloc
) = Box
::into_unique(x
);
843 Ok(unsafe { Self::from_inner_in(ptr.into(), alloc) }
)
846 /// Constructs a new `Arc` with uninitialized contents, in the provided allocator, returning an
847 /// error if allocation fails.
852 /// #![feature(new_uninit, allocator_api)]
853 /// #![feature(get_mut_unchecked)]
855 /// use std::sync::Arc;
856 /// use std::alloc::System;
858 /// let mut five = Arc::<u32, _>::try_new_uninit_in(System)?;
860 /// let five = unsafe {
861 /// // Deferred initialization:
862 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
864 /// five.assume_init()
867 /// assert_eq!(*five, 5);
868 /// # Ok::<(), std::alloc::AllocError>(())
870 #[unstable(feature = "allocator_api", issue = "32838")]
871 // #[unstable(feature = "new_uninit", issue = "63291")]
873 pub fn try_new_uninit_in(alloc
: A
) -> Result
<Arc
<mem
::MaybeUninit
<T
>, A
>, AllocError
> {
876 Arc
::try_allocate_for_layout(
878 |layout
| alloc
.allocate(layout
),
886 /// Constructs a new `Arc` with uninitialized contents, with the memory
887 /// being filled with `0` bytes, in the provided allocator, returning an error if allocation
890 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
896 /// #![feature(new_uninit, allocator_api)]
898 /// use std::sync::Arc;
899 /// use std::alloc::System;
901 /// let zero = Arc::<u32, _>::try_new_zeroed_in(System)?;
902 /// let zero = unsafe { zero.assume_init() };
904 /// assert_eq!(*zero, 0);
905 /// # Ok::<(), std::alloc::AllocError>(())
908 /// [zeroed]: mem::MaybeUninit::zeroed
909 #[unstable(feature = "allocator_api", issue = "32838")]
910 // #[unstable(feature = "new_uninit", issue = "63291")]
912 pub fn try_new_zeroed_in(alloc
: A
) -> Result
<Arc
<mem
::MaybeUninit
<T
>, A
>, AllocError
> {
915 Arc
::try_allocate_for_layout(
917 |layout
| alloc
.allocate_zeroed(layout
),
924 /// Returns the inner value, if the `Arc` has exactly one strong reference.
926 /// Otherwise, an [`Err`] is returned with the same `Arc` that was
929 /// This will succeed even if there are outstanding weak references.
931 /// It is strongly recommended to use [`Arc::into_inner`] instead if you don't
932 /// want to keep the `Arc` in the [`Err`] case.
933 /// Immediately dropping the [`Err`] payload, like in the expression
934 /// `Arc::try_unwrap(this).ok()`, can still cause the strong count to
935 /// drop to zero and the inner value of the `Arc` to be dropped:
936 /// For instance if two threads each execute this expression in parallel, then
937 /// there is a race condition. The threads could first both check whether they
938 /// have the last clone of their `Arc` via `Arc::try_unwrap`, and then
939 /// both drop their `Arc` in the call to [`ok`][`Result::ok`],
940 /// taking the strong count from two down to zero.
945 /// use std::sync::Arc;
947 /// let x = Arc::new(3);
948 /// assert_eq!(Arc::try_unwrap(x), Ok(3));
950 /// let x = Arc::new(4);
951 /// let _y = Arc::clone(&x);
952 /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
955 #[stable(feature = "arc_unique", since = "1.4.0")]
956 pub fn try_unwrap(this
: Self) -> Result
<T
, Self> {
957 if this
.inner().strong
.compare_exchange(1, 0, Relaxed
, Relaxed
).is_err() {
961 acquire
!(this
.inner().strong
);
964 let elem
= ptr
::read(&this
.ptr
.as_ref().data
);
965 let alloc
= ptr
::read(&this
.alloc
); // copy the allocator
967 // Make a weak pointer to clean up the implicit strong-weak reference
968 let _weak
= Weak { ptr: this.ptr, alloc }
;
975 /// Returns the inner value, if the `Arc` has exactly one strong reference.
977 /// Otherwise, [`None`] is returned and the `Arc` is dropped.
979 /// This will succeed even if there are outstanding weak references.
981 /// If `Arc::into_inner` is called on every clone of this `Arc`,
982 /// it is guaranteed that exactly one of the calls returns the inner value.
983 /// This means in particular that the inner value is not dropped.
985 /// The similar expression `Arc::try_unwrap(this).ok()` does not
986 /// offer such a guarantee. See the last example below
987 /// and the documentation of [`Arc::try_unwrap`].
991 /// Minimal example demonstrating the guarantee that `Arc::into_inner` gives.
993 /// use std::sync::Arc;
995 /// let x = Arc::new(3);
996 /// let y = Arc::clone(&x);
998 /// // Two threads calling `Arc::into_inner` on both clones of an `Arc`:
999 /// let x_thread = std::thread::spawn(|| Arc::into_inner(x));
1000 /// let y_thread = std::thread::spawn(|| Arc::into_inner(y));
1002 /// let x_inner_value = x_thread.join().unwrap();
1003 /// let y_inner_value = y_thread.join().unwrap();
1005 /// // One of the threads is guaranteed to receive the inner value:
1006 /// assert!(matches!(
1007 /// (x_inner_value, y_inner_value),
1008 /// (None, Some(3)) | (Some(3), None)
1010 /// // The result could also be `(None, None)` if the threads called
1011 /// // `Arc::try_unwrap(x).ok()` and `Arc::try_unwrap(y).ok()` instead.
1014 /// A more practical example demonstrating the need for `Arc::into_inner`:
1016 /// use std::sync::Arc;
1018 /// // Definition of a simple singly linked list using `Arc`:
1019 /// #[derive(Clone)]
1020 /// struct LinkedList<T>(Option<Arc<Node<T>>>);
1021 /// struct Node<T>(T, Option<Arc<Node<T>>>);
1023 /// // Dropping a long `LinkedList<T>` relying on the destructor of `Arc`
1024 /// // can cause a stack overflow. To prevent this, we can provide a
1025 /// // manual `Drop` implementation that does the destruction in a loop:
1026 /// impl<T> Drop for LinkedList<T> {
1027 /// fn drop(&mut self) {
1028 /// let mut link = self.0.take();
1029 /// while let Some(arc_node) = link.take() {
1030 /// if let Some(Node(_value, next)) = Arc::into_inner(arc_node) {
1037 /// // Implementation of `new` and `push` omitted
1038 /// impl<T> LinkedList<T> {
1040 /// # fn new() -> Self {
1041 /// # LinkedList(None)
1043 /// # fn push(&mut self, x: T) {
1044 /// # self.0 = Some(Arc::new(Node(x, self.0.take())));
1048 /// // The following code could have still caused a stack overflow
1049 /// // despite the manual `Drop` impl if that `Drop` impl had used
1050 /// // `Arc::try_unwrap(arc).ok()` instead of `Arc::into_inner(arc)`.
1052 /// // Create a long list and clone it
1053 /// let mut x = LinkedList::new();
1054 /// for i in 0..100000 {
1055 /// x.push(i); // Adds i to the front of x
1057 /// let y = x.clone();
1059 /// // Drop the clones in parallel
1060 /// let x_thread = std::thread::spawn(|| drop(x));
1061 /// let y_thread = std::thread::spawn(|| drop(y));
1062 /// x_thread.join().unwrap();
1063 /// y_thread.join().unwrap();
1066 #[stable(feature = "arc_into_inner", since = "1.70.0")]
1067 pub fn into_inner(this
: Self) -> Option
<T
> {
1068 // Make sure that the ordinary `Drop` implementation isn’t called as well
1069 let mut this
= mem
::ManuallyDrop
::new(this
);
1071 // Following the implementation of `drop` and `drop_slow`
1072 if this
.inner().strong
.fetch_sub(1, Release
) != 1 {
1076 acquire
!(this
.inner().strong
);
1078 // SAFETY: This mirrors the line
1080 // unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) };
1082 // in `drop_slow`. Instead of dropping the value behind the pointer,
1083 // it is read and eventually returned; `ptr::read` has the same
1084 // safety conditions as `ptr::drop_in_place`.
1086 let inner
= unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) }
;
1087 let alloc
= unsafe { ptr::read(&this.alloc) }
;
1089 drop(Weak { ptr: this.ptr, alloc }
);
1096 /// Constructs a new atomically reference-counted slice with uninitialized contents.
1101 /// #![feature(new_uninit)]
1102 /// #![feature(get_mut_unchecked)]
1104 /// use std::sync::Arc;
1106 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
1108 /// // Deferred initialization:
1109 /// let data = Arc::get_mut(&mut values).unwrap();
1110 /// data[0].write(1);
1111 /// data[1].write(2);
1112 /// data[2].write(3);
1114 /// let values = unsafe { values.assume_init() };
1116 /// assert_eq!(*values, [1, 2, 3])
1118 #[cfg(not(no_global_oom_handling))]
1120 #[unstable(feature = "new_uninit", issue = "63291")]
1122 pub fn new_uninit_slice(len
: usize) -> Arc
<[mem
::MaybeUninit
<T
>]> {
1123 unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) }
1126 /// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
1127 /// filled with `0` bytes.
1129 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1130 /// incorrect usage of this method.
1135 /// #![feature(new_uninit)]
1137 /// use std::sync::Arc;
1139 /// let values = Arc::<[u32]>::new_zeroed_slice(3);
1140 /// let values = unsafe { values.assume_init() };
1142 /// assert_eq!(*values, [0, 0, 0])
1145 /// [zeroed]: mem::MaybeUninit::zeroed
1146 #[cfg(not(no_global_oom_handling))]
1148 #[unstable(feature = "new_uninit", issue = "63291")]
1150 pub fn new_zeroed_slice(len
: usize) -> Arc
<[mem
::MaybeUninit
<T
>]> {
1152 Arc
::from_ptr(Arc
::allocate_for_layout(
1153 Layout
::array
::<T
>(len
).unwrap(),
1154 |layout
| Global
.allocate_zeroed(layout
),
1156 ptr
::slice_from_raw_parts_mut(mem
as *mut T
, len
)
1157 as *mut ArcInner
<[mem
::MaybeUninit
<T
>]>
1164 impl<T
, A
: Allocator
> Arc
<[T
], A
> {
1165 /// Constructs a new atomically reference-counted slice with uninitialized contents in the
1166 /// provided allocator.
1171 /// #![feature(new_uninit)]
1172 /// #![feature(get_mut_unchecked)]
1173 /// #![feature(allocator_api)]
1175 /// use std::sync::Arc;
1176 /// use std::alloc::System;
1178 /// let mut values = Arc::<[u32], _>::new_uninit_slice_in(3, System);
1180 /// let values = unsafe {
1181 /// // Deferred initialization:
1182 /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
1183 /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
1184 /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
1186 /// values.assume_init()
1189 /// assert_eq!(*values, [1, 2, 3])
1191 #[cfg(not(no_global_oom_handling))]
1192 #[unstable(feature = "new_uninit", issue = "63291")]
1194 pub fn new_uninit_slice_in(len
: usize, alloc
: A
) -> Arc
<[mem
::MaybeUninit
<T
>], A
> {
1195 unsafe { Arc::from_ptr_in(Arc::allocate_for_slice_in(len, &alloc), alloc) }
1198 /// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
1199 /// filled with `0` bytes, in the provided allocator.
1201 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1202 /// incorrect usage of this method.
1207 /// #![feature(new_uninit)]
1208 /// #![feature(allocator_api)]
1210 /// use std::sync::Arc;
1211 /// use std::alloc::System;
1213 /// let values = Arc::<[u32], _>::new_zeroed_slice_in(3, System);
1214 /// let values = unsafe { values.assume_init() };
1216 /// assert_eq!(*values, [0, 0, 0])
1219 /// [zeroed]: mem::MaybeUninit::zeroed
1220 #[cfg(not(no_global_oom_handling))]
1221 #[unstable(feature = "new_uninit", issue = "63291")]
1223 pub fn new_zeroed_slice_in(len
: usize, alloc
: A
) -> Arc
<[mem
::MaybeUninit
<T
>], A
> {
1226 Arc
::allocate_for_layout(
1227 Layout
::array
::<T
>(len
).unwrap(),
1228 |layout
| alloc
.allocate_zeroed(layout
),
1230 ptr
::slice_from_raw_parts_mut(mem
.cast
::<T
>(), len
)
1231 as *mut ArcInner
<[mem
::MaybeUninit
<T
>]>
1240 impl<T
, A
: Allocator
> Arc
<mem
::MaybeUninit
<T
>, A
> {
1241 /// Converts to `Arc<T>`.
1245 /// As with [`MaybeUninit::assume_init`],
1246 /// it is up to the caller to guarantee that the inner value
1247 /// really is in an initialized state.
1248 /// Calling this when the content is not yet fully initialized
1249 /// causes immediate undefined behavior.
1251 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1256 /// #![feature(new_uninit)]
1257 /// #![feature(get_mut_unchecked)]
1259 /// use std::sync::Arc;
1261 /// let mut five = Arc::<u32>::new_uninit();
1263 /// // Deferred initialization:
1264 /// Arc::get_mut(&mut five).unwrap().write(5);
1266 /// let five = unsafe { five.assume_init() };
1268 /// assert_eq!(*five, 5)
1270 #[unstable(feature = "new_uninit", issue = "63291")]
1271 #[must_use = "`self` will be dropped if the result is not used"]
1273 pub unsafe fn assume_init(self) -> Arc
<T
, A
>
1277 let md_self
= mem
::ManuallyDrop
::new(self);
1278 unsafe { Arc::from_inner_in(md_self.ptr.cast(), md_self.alloc.clone()) }
1282 impl<T
, A
: Allocator
> Arc
<[mem
::MaybeUninit
<T
>], A
> {
1283 /// Converts to `Arc<[T]>`.
1287 /// As with [`MaybeUninit::assume_init`],
1288 /// it is up to the caller to guarantee that the inner value
1289 /// really is in an initialized state.
1290 /// Calling this when the content is not yet fully initialized
1291 /// causes immediate undefined behavior.
1293 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1298 /// #![feature(new_uninit)]
1299 /// #![feature(get_mut_unchecked)]
1301 /// use std::sync::Arc;
1303 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
1305 /// // Deferred initialization:
1306 /// let data = Arc::get_mut(&mut values).unwrap();
1307 /// data[0].write(1);
1308 /// data[1].write(2);
1309 /// data[2].write(3);
1311 /// let values = unsafe { values.assume_init() };
1313 /// assert_eq!(*values, [1, 2, 3])
1315 #[unstable(feature = "new_uninit", issue = "63291")]
1316 #[must_use = "`self` will be dropped if the result is not used"]
1318 pub unsafe fn assume_init(self) -> Arc
<[T
], A
>
1322 let md_self
= mem
::ManuallyDrop
::new(self);
1323 unsafe { Arc::from_ptr_in(md_self.ptr.as_ptr() as _, md_self.alloc.clone()) }
1327 impl<T
: ?Sized
> Arc
<T
> {
1328 /// Constructs an `Arc<T>` from a raw pointer.
1330 /// The raw pointer must have been previously returned by a call to
1331 /// [`Arc<U>::into_raw`][into_raw] where `U` must have the same size and
1332 /// alignment as `T`. This is trivially true if `U` is `T`.
1333 /// Note that if `U` is not `T` but has the same size and alignment, this is
1334 /// basically like transmuting references of different types. See
1335 /// [`mem::transmute`][transmute] for more information on what
1336 /// restrictions apply in this case.
1338 /// The user of `from_raw` has to make sure a specific value of `T` is only
1341 /// This function is unsafe because improper use may lead to memory unsafety,
1342 /// even if the returned `Arc<T>` is never accessed.
1344 /// [into_raw]: Arc::into_raw
1345 /// [transmute]: core::mem::transmute
1350 /// use std::sync::Arc;
1352 /// let x = Arc::new("hello".to_owned());
1353 /// let x_ptr = Arc::into_raw(x);
1356 /// // Convert back to an `Arc` to prevent leak.
1357 /// let x = Arc::from_raw(x_ptr);
1358 /// assert_eq!(&*x, "hello");
1360 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
1363 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1366 #[stable(feature = "rc_raw", since = "1.17.0")]
1367 pub unsafe fn from_raw(ptr
: *const T
) -> Self {
1368 unsafe { Arc::from_raw_in(ptr, Global) }
1371 /// Increments the strong reference count on the `Arc<T>` associated with the
1372 /// provided pointer by one.
1376 /// The pointer must have been obtained through `Arc::into_raw`, and the
1377 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1378 /// least 1) for the duration of this method.
1383 /// use std::sync::Arc;
1385 /// let five = Arc::new(5);
1388 /// let ptr = Arc::into_raw(five);
1389 /// Arc::increment_strong_count(ptr);
1391 /// // This assertion is deterministic because we haven't shared
1392 /// // the `Arc` between threads.
1393 /// let five = Arc::from_raw(ptr);
1394 /// assert_eq!(2, Arc::strong_count(&five));
1398 #[stable(feature = "arc_mutate_strong_count", since = "1.51.0")]
1399 pub unsafe fn increment_strong_count(ptr
: *const T
) {
1400 unsafe { Arc::increment_strong_count_in(ptr, Global) }
1403 /// Decrements the strong reference count on the `Arc<T>` associated with the
1404 /// provided pointer by one.
1408 /// The pointer must have been obtained through `Arc::into_raw`, and the
1409 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1410 /// least 1) when invoking this method. This method can be used to release the final
1411 /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
1417 /// use std::sync::Arc;
1419 /// let five = Arc::new(5);
1422 /// let ptr = Arc::into_raw(five);
1423 /// Arc::increment_strong_count(ptr);
1425 /// // Those assertions are deterministic because we haven't shared
1426 /// // the `Arc` between threads.
1427 /// let five = Arc::from_raw(ptr);
1428 /// assert_eq!(2, Arc::strong_count(&five));
1429 /// Arc::decrement_strong_count(ptr);
1430 /// assert_eq!(1, Arc::strong_count(&five));
1434 #[stable(feature = "arc_mutate_strong_count", since = "1.51.0")]
1435 pub unsafe fn decrement_strong_count(ptr
: *const T
) {
1436 unsafe { Arc::decrement_strong_count_in(ptr, Global) }
1440 impl<T
: ?Sized
, A
: Allocator
> Arc
<T
, A
> {
1441 /// Consumes the `Arc`, returning the wrapped pointer.
1443 /// To avoid a memory leak the pointer must be converted back to an `Arc` using
1444 /// [`Arc::from_raw`].
1449 /// use std::sync::Arc;
1451 /// let x = Arc::new("hello".to_owned());
1452 /// let x_ptr = Arc::into_raw(x);
1453 /// assert_eq!(unsafe { &*x_ptr }, "hello");
1455 #[must_use = "losing the pointer will leak memory"]
1456 #[stable(feature = "rc_raw", since = "1.17.0")]
1457 #[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
1458 pub fn into_raw(this
: Self) -> *const T
{
1459 let ptr
= Self::as_ptr(&this
);
1464 /// Provides a raw pointer to the data.
1466 /// The counts are not affected in any way and the `Arc` is not consumed. The pointer is valid for
1467 /// as long as there are strong counts in the `Arc`.
1472 /// use std::sync::Arc;
1474 /// let x = Arc::new("hello".to_owned());
1475 /// let y = Arc::clone(&x);
1476 /// let x_ptr = Arc::as_ptr(&x);
1477 /// assert_eq!(x_ptr, Arc::as_ptr(&y));
1478 /// assert_eq!(unsafe { &*x_ptr }, "hello");
1481 #[stable(feature = "rc_as_ptr", since = "1.45.0")]
1482 #[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
1483 pub fn as_ptr(this
: &Self) -> *const T
{
1484 let ptr
: *mut ArcInner
<T
> = NonNull
::as_ptr(this
.ptr
);
1486 // SAFETY: This cannot go through Deref::deref or RcBoxPtr::inner because
1487 // this is required to retain raw/mut provenance such that e.g. `get_mut` can
1488 // write through the pointer after the Rc is recovered through `from_raw`.
1489 unsafe { ptr::addr_of_mut!((*ptr).data) }
1492 /// Constructs an `Arc<T, A>` from a raw pointer.
1494 /// The raw pointer must have been previously returned by a call to
1495 /// [`Arc<U, A>::into_raw`][into_raw] where `U` must have the same size and
1496 /// alignment as `T`. This is trivially true if `U` is `T`.
1497 /// Note that if `U` is not `T` but has the same size and alignment, this is
1498 /// basically like transmuting references of different types. See
1499 /// [`mem::transmute`] for more information on what
1500 /// restrictions apply in this case.
1502 /// The raw pointer must point to a block of memory allocated by `alloc`
1504 /// The user of `from_raw` has to make sure a specific value of `T` is only
1507 /// This function is unsafe because improper use may lead to memory unsafety,
1508 /// even if the returned `Arc<T>` is never accessed.
1510 /// [into_raw]: Arc::into_raw
1515 /// #![feature(allocator_api)]
1517 /// use std::sync::Arc;
1518 /// use std::alloc::System;
1520 /// let x = Arc::new_in("hello".to_owned(), System);
1521 /// let x_ptr = Arc::into_raw(x);
1524 /// // Convert back to an `Arc` to prevent leak.
1525 /// let x = Arc::from_raw_in(x_ptr, System);
1526 /// assert_eq!(&*x, "hello");
1528 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
1531 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1534 #[unstable(feature = "allocator_api", issue = "32838")]
1535 pub unsafe fn from_raw_in(ptr
: *const T
, alloc
: A
) -> Self {
1537 let offset
= data_offset(ptr
);
1539 // Reverse the offset to find the original ArcInner.
1540 let arc_ptr
= ptr
.byte_sub(offset
) as *mut ArcInner
<T
>;
1542 Self::from_ptr_in(arc_ptr
, alloc
)
1546 /// Creates a new [`Weak`] pointer to this allocation.
1551 /// use std::sync::Arc;
1553 /// let five = Arc::new(5);
1555 /// let weak_five = Arc::downgrade(&five);
1557 #[must_use = "this returns a new `Weak` pointer, \
1558 without modifying the original `Arc`"]
1559 #[stable(feature = "arc_weak", since = "1.4.0")]
1560 pub fn downgrade(this
: &Self) -> Weak
<T
, A
>
1564 // This Relaxed is OK because we're checking the value in the CAS
1566 let mut cur
= this
.inner().weak
.load(Relaxed
);
1569 // check if the weak counter is currently "locked"; if so, spin.
1570 if cur
== usize::MAX
{
1572 cur
= this
.inner().weak
.load(Relaxed
);
1576 // We can't allow the refcount to increase much past `MAX_REFCOUNT`.
1577 assert
!(cur
<= MAX_REFCOUNT
, "{}", INTERNAL_OVERFLOW_ERROR
);
1579 // NOTE: this code currently ignores the possibility of overflow
1580 // into usize::MAX; in general both Rc and Arc need to be adjusted
1581 // to deal with overflow.
1583 // Unlike with Clone(), we need this to be an Acquire read to
1584 // synchronize with the write coming from `is_unique`, so that the
1585 // events prior to that write happen before this read.
1586 match this
.inner().weak
.compare_exchange_weak(cur
, cur
+ 1, Acquire
, Relaxed
) {
1588 // Make sure we do not create a dangling Weak
1589 debug_assert
!(!is_dangling(this
.ptr
.as_ptr()));
1590 return Weak { ptr: this.ptr, alloc: this.alloc.clone() }
;
1592 Err(old
) => cur
= old
,
1597 /// Gets the number of [`Weak`] pointers to this allocation.
1601 /// This method by itself is safe, but using it correctly requires extra care.
1602 /// Another thread can change the weak count at any time,
1603 /// including potentially between calling this method and acting on the result.
1608 /// use std::sync::Arc;
1610 /// let five = Arc::new(5);
1611 /// let _weak_five = Arc::downgrade(&five);
1613 /// // This assertion is deterministic because we haven't shared
1614 /// // the `Arc` or `Weak` between threads.
1615 /// assert_eq!(1, Arc::weak_count(&five));
1619 #[stable(feature = "arc_counts", since = "1.15.0")]
1620 pub fn weak_count(this
: &Self) -> usize {
1621 let cnt
= this
.inner().weak
.load(Relaxed
);
1622 // If the weak count is currently locked, the value of the
1623 // count was 0 just before taking the lock.
1624 if cnt
== usize::MAX { 0 }
else { cnt - 1 }
1627 /// Gets the number of strong (`Arc`) pointers to this allocation.
1631 /// This method by itself is safe, but using it correctly requires extra care.
1632 /// Another thread can change the strong count at any time,
1633 /// including potentially between calling this method and acting on the result.
1638 /// use std::sync::Arc;
1640 /// let five = Arc::new(5);
1641 /// let _also_five = Arc::clone(&five);
1643 /// // This assertion is deterministic because we haven't shared
1644 /// // the `Arc` between threads.
1645 /// assert_eq!(2, Arc::strong_count(&five));
1649 #[stable(feature = "arc_counts", since = "1.15.0")]
1650 pub fn strong_count(this
: &Self) -> usize {
1651 this
.inner().strong
.load(Relaxed
)
1654 /// Increments the strong reference count on the `Arc<T>` associated with the
1655 /// provided pointer by one.
1659 /// The pointer must have been obtained through `Arc::into_raw`, and the
1660 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1661 /// least 1) for the duration of this method,, and `ptr` must point to a block of memory
1662 /// allocated by `alloc`.
1667 /// #![feature(allocator_api)]
1669 /// use std::sync::Arc;
1670 /// use std::alloc::System;
1672 /// let five = Arc::new_in(5, System);
1675 /// let ptr = Arc::into_raw(five);
1676 /// Arc::increment_strong_count_in(ptr, System);
1678 /// // This assertion is deterministic because we haven't shared
1679 /// // the `Arc` between threads.
1680 /// let five = Arc::from_raw_in(ptr, System);
1681 /// assert_eq!(2, Arc::strong_count(&five));
1685 #[unstable(feature = "allocator_api", issue = "32838")]
1686 pub unsafe fn increment_strong_count_in(ptr
: *const T
, alloc
: A
)
1690 // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
1691 let arc
= unsafe { mem::ManuallyDrop::new(Arc::from_raw_in(ptr, alloc)) }
;
1692 // Now increase refcount, but don't drop new refcount either
1693 let _arc_clone
: mem
::ManuallyDrop
<_
> = arc
.clone();
1696 /// Decrements the strong reference count on the `Arc<T>` associated with the
1697 /// provided pointer by one.
1701 /// The pointer must have been obtained through `Arc::into_raw`, the
1702 /// associated `Arc` instance must be valid (i.e. the strong count must be at
1703 /// least 1) when invoking this method, and `ptr` must point to a block of memory
1704 /// allocated by `alloc`. This method can be used to release the final
1705 /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
1711 /// #![feature(allocator_api)]
1713 /// use std::sync::Arc;
1714 /// use std::alloc::System;
1716 /// let five = Arc::new_in(5, System);
1719 /// let ptr = Arc::into_raw(five);
1720 /// Arc::increment_strong_count_in(ptr, System);
1722 /// // Those assertions are deterministic because we haven't shared
1723 /// // the `Arc` between threads.
1724 /// let five = Arc::from_raw_in(ptr, System);
1725 /// assert_eq!(2, Arc::strong_count(&five));
1726 /// Arc::decrement_strong_count_in(ptr, System);
1727 /// assert_eq!(1, Arc::strong_count(&five));
1731 #[unstable(feature = "allocator_api", issue = "32838")]
1732 pub unsafe fn decrement_strong_count_in(ptr
: *const T
, alloc
: A
) {
1733 unsafe { drop(Arc::from_raw_in(ptr, alloc)) }
;
1737 fn inner(&self) -> &ArcInner
<T
> {
1738 // This unsafety is ok because while this arc is alive we're guaranteed
1739 // that the inner pointer is valid. Furthermore, we know that the
1740 // `ArcInner` structure itself is `Sync` because the inner data is
1741 // `Sync` as well, so we're ok loaning out an immutable pointer to these
1743 unsafe { self.ptr.as_ref() }
1746 // Non-inlined part of `drop`.
1748 unsafe fn drop_slow(&mut self) {
1749 // Destroy the data at this time, even though we must not free the box
1750 // allocation itself (there might still be weak pointers lying around).
1751 unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) }
;
1753 // Drop the weak ref collectively held by all strong references
1754 // Take a reference to `self.alloc` instead of cloning because 1. it'll
1755 // last long enough, and 2. you should be able to drop `Arc`s with
1756 // unclonable allocators
1757 drop(Weak { ptr: self.ptr, alloc: &self.alloc }
);
1760 /// Returns `true` if the two `Arc`s point to the same allocation in a vein similar to
1761 /// [`ptr::eq`]. This function ignores the metadata of `dyn Trait` pointers.
1766 /// use std::sync::Arc;
1768 /// let five = Arc::new(5);
1769 /// let same_five = Arc::clone(&five);
1770 /// let other_five = Arc::new(5);
1772 /// assert!(Arc::ptr_eq(&five, &same_five));
1773 /// assert!(!Arc::ptr_eq(&five, &other_five));
1776 /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
1779 #[stable(feature = "ptr_eq", since = "1.17.0")]
1780 pub fn ptr_eq(this
: &Self, other
: &Self) -> bool
{
1781 this
.ptr
.as_ptr() as *const () == other
.ptr
.as_ptr() as *const ()
1785 impl<T
: ?Sized
> Arc
<T
> {
1786 /// Allocates an `ArcInner<T>` with sufficient space for
1787 /// a possibly-unsized inner value where the value has the layout provided.
1789 /// The function `mem_to_arcinner` is called with the data pointer
1790 /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
1791 #[cfg(not(no_global_oom_handling))]
1792 unsafe fn allocate_for_layout(
1793 value_layout
: Layout
,
1794 allocate
: impl FnOnce(Layout
) -> Result
<NonNull
<[u8]>, AllocError
>,
1795 mem_to_arcinner
: impl FnOnce(*mut u8) -> *mut ArcInner
<T
>,
1796 ) -> *mut ArcInner
<T
> {
1797 let layout
= arcinner_layout_for_value_layout(value_layout
);
1799 let ptr
= allocate(layout
).unwrap_or_else(|_
| handle_alloc_error(layout
));
1801 unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) }
1804 /// Allocates an `ArcInner<T>` with sufficient space for
1805 /// a possibly-unsized inner value where the value has the layout provided,
1806 /// returning an error if allocation fails.
1808 /// The function `mem_to_arcinner` is called with the data pointer
1809 /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
1810 unsafe fn try_allocate_for_layout(
1811 value_layout
: Layout
,
1812 allocate
: impl FnOnce(Layout
) -> Result
<NonNull
<[u8]>, AllocError
>,
1813 mem_to_arcinner
: impl FnOnce(*mut u8) -> *mut ArcInner
<T
>,
1814 ) -> Result
<*mut ArcInner
<T
>, AllocError
> {
1815 let layout
= arcinner_layout_for_value_layout(value_layout
);
1817 let ptr
= allocate(layout
)?
;
1819 let inner
= unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) }
;
1824 unsafe fn initialize_arcinner(
1827 mem_to_arcinner
: impl FnOnce(*mut u8) -> *mut ArcInner
<T
>,
1828 ) -> *mut ArcInner
<T
> {
1829 let inner
= mem_to_arcinner(ptr
.as_non_null_ptr().as_ptr());
1830 debug_assert_eq
!(unsafe { Layout::for_value(&*inner) }
, layout
);
1833 ptr
::write(&mut (*inner
).strong
, atomic
::AtomicUsize
::new(1));
1834 ptr
::write(&mut (*inner
).weak
, atomic
::AtomicUsize
::new(1));
1841 impl<T
: ?Sized
, A
: Allocator
> Arc
<T
, A
> {
1842 /// Allocates an `ArcInner<T>` with sufficient space for an unsized inner value.
1844 #[cfg(not(no_global_oom_handling))]
1845 unsafe fn allocate_for_ptr_in(ptr
: *const T
, alloc
: &A
) -> *mut ArcInner
<T
> {
1846 // Allocate for the `ArcInner<T>` using the given value.
1848 Arc
::allocate_for_layout(
1849 Layout
::for_value(&*ptr
),
1850 |layout
| alloc
.allocate(layout
),
1851 |mem
| mem
.with_metadata_of(ptr
as *const ArcInner
<T
>),
1856 #[cfg(not(no_global_oom_handling))]
1857 fn from_box_in(src
: Box
<T
, A
>) -> Arc
<T
, A
> {
1859 let value_size
= size_of_val(&*src
);
1860 let ptr
= Self::allocate_for_ptr_in(&*src
, Box
::allocator(&src
));
1862 // Copy value as bytes
1863 ptr
::copy_nonoverlapping(
1864 &*src
as *const T
as *const u8,
1865 &mut (*ptr
).data
as *mut _
as *mut u8,
1869 // Free the allocation without dropping its contents
1870 let (bptr
, alloc
) = Box
::into_raw_with_allocator(src
);
1871 let src
= Box
::from_raw(bptr
as *mut mem
::ManuallyDrop
<T
>);
1874 Self::from_ptr_in(ptr
, alloc
)
1880 /// Allocates an `ArcInner<[T]>` with the given length.
1881 #[cfg(not(no_global_oom_handling))]
1882 unsafe fn allocate_for_slice(len
: usize) -> *mut ArcInner
<[T
]> {
1884 Self::allocate_for_layout(
1885 Layout
::array
::<T
>(len
).unwrap(),
1886 |layout
| Global
.allocate(layout
),
1887 |mem
| ptr
::slice_from_raw_parts_mut(mem
.cast
::<T
>(), len
) as *mut ArcInner
<[T
]>,
1892 /// Copy elements from slice into newly allocated `Arc<[T]>`
1894 /// Unsafe because the caller must either take ownership or bind `T: Copy`.
1895 #[cfg(not(no_global_oom_handling))]
1896 unsafe fn copy_from_slice(v
: &[T
]) -> Arc
<[T
]> {
1898 let ptr
= Self::allocate_for_slice(v
.len());
1900 ptr
::copy_nonoverlapping(v
.as_ptr(), &mut (*ptr
).data
as *mut [T
] as *mut T
, v
.len());
1906 /// Constructs an `Arc<[T]>` from an iterator known to be of a certain size.
1908 /// Behavior is undefined should the size be wrong.
1909 #[cfg(not(no_global_oom_handling))]
1910 unsafe fn from_iter_exact(iter
: impl Iterator
<Item
= T
>, len
: usize) -> Arc
<[T
]> {
1911 // Panic guard while cloning T elements.
1912 // In the event of a panic, elements that have been written
1913 // into the new ArcInner will be dropped, then the memory freed.
1921 impl<T
> Drop
for Guard
<T
> {
1922 fn drop(&mut self) {
1924 let slice
= from_raw_parts_mut(self.elems
, self.n_elems
);
1925 ptr
::drop_in_place(slice
);
1927 Global
.deallocate(self.mem
, self.layout
);
1933 let ptr
= Self::allocate_for_slice(len
);
1935 let mem
= ptr
as *mut _
as *mut u8;
1936 let layout
= Layout
::for_value(&*ptr
);
1938 // Pointer to first element
1939 let elems
= &mut (*ptr
).data
as *mut [T
] as *mut T
;
1941 let mut guard
= Guard { mem: NonNull::new_unchecked(mem), elems, layout, n_elems: 0 }
;
1943 for (i
, item
) in iter
.enumerate() {
1944 ptr
::write(elems
.add(i
), item
);
1948 // All clear. Forget the guard so it doesn't free the new ArcInner.
1956 impl<T
, A
: Allocator
> Arc
<[T
], A
> {
1957 /// Allocates an `ArcInner<[T]>` with the given length.
1959 #[cfg(not(no_global_oom_handling))]
1960 unsafe fn allocate_for_slice_in(len
: usize, alloc
: &A
) -> *mut ArcInner
<[T
]> {
1962 Arc
::allocate_for_layout(
1963 Layout
::array
::<T
>(len
).unwrap(),
1964 |layout
| alloc
.allocate(layout
),
1965 |mem
| ptr
::slice_from_raw_parts_mut(mem
.cast
::<T
>(), len
) as *mut ArcInner
<[T
]>,
1971 /// Specialization trait used for `From<&[T]>`.
1972 #[cfg(not(no_global_oom_handling))]
1973 trait ArcFromSlice
<T
> {
1974 fn from_slice(slice
: &[T
]) -> Self;
1977 #[cfg(not(no_global_oom_handling))]
1978 impl<T
: Clone
> ArcFromSlice
<T
> for Arc
<[T
]> {
1980 default fn from_slice(v
: &[T
]) -> Self {
1981 unsafe { Self::from_iter_exact(v.iter().cloned(), v.len()) }
1985 #[cfg(not(no_global_oom_handling))]
1986 impl<T
: Copy
> ArcFromSlice
<T
> for Arc
<[T
]> {
1988 fn from_slice(v
: &[T
]) -> Self {
1989 unsafe { Arc::copy_from_slice(v) }
1993 #[stable(feature = "rust1", since = "1.0.0")]
1994 impl<T
: ?Sized
, A
: Allocator
+ Clone
> Clone
for Arc
<T
, A
> {
1995 /// Makes a clone of the `Arc` pointer.
1997 /// This creates another pointer to the same allocation, increasing the
1998 /// strong reference count.
2003 /// use std::sync::Arc;
2005 /// let five = Arc::new(5);
2007 /// let _ = Arc::clone(&five);
2010 fn clone(&self) -> Arc
<T
, A
> {
2011 // Using a relaxed ordering is alright here, as knowledge of the
2012 // original reference prevents other threads from erroneously deleting
2015 // As explained in the [Boost documentation][1], Increasing the
2016 // reference counter can always be done with memory_order_relaxed: New
2017 // references to an object can only be formed from an existing
2018 // reference, and passing an existing reference from one thread to
2019 // another must already provide any required synchronization.
2021 // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
2022 let old_size
= self.inner().strong
.fetch_add(1, Relaxed
);
2024 // However we need to guard against massive refcounts in case someone is `mem::forget`ing
2025 // Arcs. If we don't do this the count can overflow and users will use-after free. This
2026 // branch will never be taken in any realistic program. We abort because such a program is
2027 // incredibly degenerate, and we don't care to support it.
2029 // This check is not 100% water-proof: we error when the refcount grows beyond `isize::MAX`.
2030 // But we do that check *after* having done the increment, so there is a chance here that
2031 // the worst already happened and we actually do overflow the `usize` counter. However, that
2032 // requires the counter to grow from `isize::MAX` to `usize::MAX` between the increment
2033 // above and the `abort` below, which seems exceedingly unlikely.
2035 // This is a global invariant, and also applies when using a compare-exchange loop to increment
2036 // counters in other methods.
2037 // Otherwise, the counter could be brought to an almost-overflow using a compare-exchange loop,
2038 // and then overflow using a few `fetch_add`s.
2039 if old_size
> MAX_REFCOUNT
{
2043 unsafe { Self::from_inner_in(self.ptr, self.alloc.clone()) }
2047 #[stable(feature = "rust1", since = "1.0.0")]
2048 impl<T
: ?Sized
, A
: Allocator
> Deref
for Arc
<T
, A
> {
2052 fn deref(&self) -> &T
{
2057 #[unstable(feature = "receiver_trait", issue = "none")]
2058 impl<T
: ?Sized
> Receiver
for Arc
<T
> {}
2060 impl<T
: Clone
, A
: Allocator
+ Clone
> Arc
<T
, A
> {
2061 /// Makes a mutable reference into the given `Arc`.
2063 /// If there are other `Arc` pointers to the same allocation, then `make_mut` will
2064 /// [`clone`] the inner value to a new allocation to ensure unique ownership. This is also
2065 /// referred to as clone-on-write.
2067 /// However, if there are no other `Arc` pointers to this allocation, but some [`Weak`]
2068 /// pointers, then the [`Weak`] pointers will be dissociated and the inner value will not
2071 /// See also [`get_mut`], which will fail rather than cloning the inner value
2072 /// or dissociating [`Weak`] pointers.
2074 /// [`clone`]: Clone::clone
2075 /// [`get_mut`]: Arc::get_mut
2080 /// use std::sync::Arc;
2082 /// let mut data = Arc::new(5);
2084 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
2085 /// let mut other_data = Arc::clone(&data); // Won't clone inner data
2086 /// *Arc::make_mut(&mut data) += 1; // Clones inner data
2087 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
2088 /// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
2090 /// // Now `data` and `other_data` point to different allocations.
2091 /// assert_eq!(*data, 8);
2092 /// assert_eq!(*other_data, 12);
2095 /// [`Weak`] pointers will be dissociated:
2098 /// use std::sync::Arc;
2100 /// let mut data = Arc::new(75);
2101 /// let weak = Arc::downgrade(&data);
2103 /// assert!(75 == *data);
2104 /// assert!(75 == *weak.upgrade().unwrap());
2106 /// *Arc::make_mut(&mut data) += 1;
2108 /// assert!(76 == *data);
2109 /// assert!(weak.upgrade().is_none());
2111 #[cfg(not(no_global_oom_handling))]
2113 #[stable(feature = "arc_unique", since = "1.4.0")]
2114 pub fn make_mut(this
: &mut Self) -> &mut T
{
2115 // Note that we hold both a strong reference and a weak reference.
2116 // Thus, releasing our strong reference only will not, by itself, cause
2117 // the memory to be deallocated.
2119 // Use Acquire to ensure that we see any writes to `weak` that happen
2120 // before release writes (i.e., decrements) to `strong`. Since we hold a
2121 // weak count, there's no chance the ArcInner itself could be
2123 if this
.inner().strong
.compare_exchange(1, 0, Acquire
, Relaxed
).is_err() {
2124 // Another strong pointer exists, so we must clone.
2125 // Pre-allocate memory to allow writing the cloned value directly.
2126 let mut arc
= Self::new_uninit_in(this
.alloc
.clone());
2128 let data
= Arc
::get_mut_unchecked(&mut arc
);
2129 (**this
).write_clone_into_raw(data
.as_mut_ptr());
2130 *this
= arc
.assume_init();
2132 } else if this
.inner().weak
.load(Relaxed
) != 1 {
2133 // Relaxed suffices in the above because this is fundamentally an
2134 // optimization: we are always racing with weak pointers being
2135 // dropped. Worst case, we end up allocated a new Arc unnecessarily.
2137 // We removed the last strong ref, but there are additional weak
2138 // refs remaining. We'll move the contents to a new Arc, and
2139 // invalidate the other weak refs.
2141 // Note that it is not possible for the read of `weak` to yield
2142 // usize::MAX (i.e., locked), since the weak count can only be
2143 // locked by a thread with a strong reference.
2145 // Materialize our own implicit weak pointer, so that it can clean
2146 // up the ArcInner as needed.
2147 let _weak
= Weak { ptr: this.ptr, alloc: this.alloc.clone() }
;
2149 // Can just steal the data, all that's left is Weaks
2150 let mut arc
= Self::new_uninit_in(this
.alloc
.clone());
2152 let data
= Arc
::get_mut_unchecked(&mut arc
);
2153 data
.as_mut_ptr().copy_from_nonoverlapping(&**this
, 1);
2154 ptr
::write(this
, arc
.assume_init());
2157 // We were the sole reference of either kind; bump back up the
2158 // strong ref count.
2159 this
.inner().strong
.store(1, Release
);
2162 // As with `get_mut()`, the unsafety is ok because our reference was
2163 // either unique to begin with, or became one upon cloning the contents.
2164 unsafe { Self::get_mut_unchecked(this) }
2167 /// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
2170 /// Assuming `arc_t` is of type `Arc<T>`, this function is functionally equivalent to
2171 /// `(*arc_t).clone()`, but will avoid cloning the inner value where possible.
2176 /// #![feature(arc_unwrap_or_clone)]
2177 /// # use std::{ptr, sync::Arc};
2178 /// let inner = String::from("test");
2179 /// let ptr = inner.as_ptr();
2181 /// let arc = Arc::new(inner);
2182 /// let inner = Arc::unwrap_or_clone(arc);
2183 /// // The inner value was not cloned
2184 /// assert!(ptr::eq(ptr, inner.as_ptr()));
2186 /// let arc = Arc::new(inner);
2187 /// let arc2 = arc.clone();
2188 /// let inner = Arc::unwrap_or_clone(arc);
2189 /// // Because there were 2 references, we had to clone the inner value.
2190 /// assert!(!ptr::eq(ptr, inner.as_ptr()));
2191 /// // `arc2` is the last reference, so when we unwrap it we get back
2192 /// // the original `String`.
2193 /// let inner = Arc::unwrap_or_clone(arc2);
2194 /// assert!(ptr::eq(ptr, inner.as_ptr()));
2197 #[unstable(feature = "arc_unwrap_or_clone", issue = "93610")]
2198 pub fn unwrap_or_clone(this
: Self) -> T
{
2199 Arc
::try_unwrap(this
).unwrap_or_else(|arc
| (*arc
).clone())
2203 impl<T
: ?Sized
, A
: Allocator
> Arc
<T
, A
> {
2204 /// Returns a mutable reference into the given `Arc`, if there are
2205 /// no other `Arc` or [`Weak`] pointers to the same allocation.
2207 /// Returns [`None`] otherwise, because it is not safe to
2208 /// mutate a shared value.
2210 /// See also [`make_mut`][make_mut], which will [`clone`][clone]
2211 /// the inner value when there are other `Arc` pointers.
2213 /// [make_mut]: Arc::make_mut
2214 /// [clone]: Clone::clone
2219 /// use std::sync::Arc;
2221 /// let mut x = Arc::new(3);
2222 /// *Arc::get_mut(&mut x).unwrap() = 4;
2223 /// assert_eq!(*x, 4);
2225 /// let _y = Arc::clone(&x);
2226 /// assert!(Arc::get_mut(&mut x).is_none());
2229 #[stable(feature = "arc_unique", since = "1.4.0")]
2230 pub fn get_mut(this
: &mut Self) -> Option
<&mut T
> {
2231 if this
.is_unique() {
2232 // This unsafety is ok because we're guaranteed that the pointer
2233 // returned is the *only* pointer that will ever be returned to T. Our
2234 // reference count is guaranteed to be 1 at this point, and we required
2235 // the Arc itself to be `mut`, so we're returning the only possible
2236 // reference to the inner data.
2237 unsafe { Some(Arc::get_mut_unchecked(this)) }
2243 /// Returns a mutable reference into the given `Arc`,
2244 /// without any check.
2246 /// See also [`get_mut`], which is safe and does appropriate checks.
2248 /// [`get_mut`]: Arc::get_mut
2252 /// If any other `Arc` or [`Weak`] pointers to the same allocation exist, then
2253 /// they must not be dereferenced or have active borrows for the duration
2254 /// of the returned borrow, and their inner type must be exactly the same as the
2255 /// inner type of this Rc (including lifetimes). This is trivially the case if no
2256 /// such pointers exist, for example immediately after `Arc::new`.
2261 /// #![feature(get_mut_unchecked)]
2263 /// use std::sync::Arc;
2265 /// let mut x = Arc::new(String::new());
2267 /// Arc::get_mut_unchecked(&mut x).push_str("foo")
2269 /// assert_eq!(*x, "foo");
2271 /// Other `Arc` pointers to the same allocation must be to the same type.
2273 /// #![feature(get_mut_unchecked)]
2275 /// use std::sync::Arc;
2277 /// let x: Arc<str> = Arc::from("Hello, world!");
2278 /// let mut y: Arc<[u8]> = x.clone().into();
2280 /// // this is Undefined Behavior, because x's inner type is str, not [u8]
2281 /// Arc::get_mut_unchecked(&mut y).fill(0xff); // 0xff is invalid in UTF-8
2283 /// println!("{}", &*x); // Invalid UTF-8 in a str
2285 /// Other `Arc` pointers to the same allocation must be to the exact same type, including lifetimes.
2287 /// #![feature(get_mut_unchecked)]
2289 /// use std::sync::Arc;
2291 /// let x: Arc<&str> = Arc::new("Hello, world!");
2293 /// let s = String::from("Oh, no!");
2294 /// let mut y: Arc<&str> = x.clone().into();
2296 /// // this is Undefined Behavior, because x's inner type
2297 /// // is &'long str, not &'short str
2298 /// *Arc::get_mut_unchecked(&mut y) = &s;
2301 /// println!("{}", &*x); // Use-after-free
2304 #[unstable(feature = "get_mut_unchecked", issue = "63292")]
2305 pub unsafe fn get_mut_unchecked(this
: &mut Self) -> &mut T
{
2306 // We are careful to *not* create a reference covering the "count" fields, as
2307 // this would alias with concurrent access to the reference counts (e.g. by `Weak`).
2308 unsafe { &mut (*this.ptr.as_ptr()).data }
2311 /// Determine whether this is the unique reference (including weak refs) to
2312 /// the underlying data.
2314 /// Note that this requires locking the weak ref count.
2315 fn is_unique(&mut self) -> bool
{
2316 // lock the weak pointer count if we appear to be the sole weak pointer
2319 // The acquire label here ensures a happens-before relationship with any
2320 // writes to `strong` (in particular in `Weak::upgrade`) prior to decrements
2321 // of the `weak` count (via `Weak::drop`, which uses release). If the upgraded
2322 // weak ref was never dropped, the CAS here will fail so we do not care to synchronize.
2323 if self.inner().weak
.compare_exchange(1, usize::MAX
, Acquire
, Relaxed
).is_ok() {
2324 // This needs to be an `Acquire` to synchronize with the decrement of the `strong`
2325 // counter in `drop` -- the only access that happens when any but the last reference
2326 // is being dropped.
2327 let unique
= self.inner().strong
.load(Acquire
) == 1;
2329 // The release write here synchronizes with a read in `downgrade`,
2330 // effectively preventing the above read of `strong` from happening
2332 self.inner().weak
.store(1, Release
); // release the lock
2340 #[stable(feature = "rust1", since = "1.0.0")]
2341 unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Arc<T, A> {
2342 /// Drops the `Arc`.
2344 /// This will decrement the strong reference count. If the strong reference
2345 /// count reaches zero then the only other references (if any) are
2346 /// [`Weak`], so we `drop` the inner value.
2351 /// use std::sync::Arc;
2355 /// impl Drop for Foo {
2356 /// fn drop(&mut self) {
2357 /// println!("dropped!");
2361 /// let foo = Arc::new(Foo);
2362 /// let foo2 = Arc::clone(&foo);
2364 /// drop(foo); // Doesn't print anything
2365 /// drop(foo2); // Prints "dropped!"
2368 fn drop(&mut self) {
2369 // Because `fetch_sub` is already atomic, we do not need to synchronize
2370 // with other threads unless we are going to delete the object. This
2371 // same logic applies to the below `fetch_sub` to the `weak` count.
2372 if self.inner().strong
.fetch_sub(1, Release
) != 1 {
2376 // This fence is needed to prevent reordering of use of the data and
2377 // deletion of the data. Because it is marked `Release`, the decreasing
2378 // of the reference count synchronizes with this `Acquire` fence. This
2379 // means that use of the data happens before decreasing the reference
2380 // count, which happens before this fence, which happens before the
2381 // deletion of the data.
2383 // As explained in the [Boost documentation][1],
2385 // > It is important to enforce any possible access to the object in one
2386 // > thread (through an existing reference) to *happen before* deleting
2387 // > the object in a different thread. This is achieved by a "release"
2388 // > operation after dropping a reference (any access to the object
2389 // > through this reference must obviously happened before), and an
2390 // > "acquire" operation before deleting the object.
2392 // In particular, while the contents of an Arc are usually immutable, it's
2393 // possible to have interior writes to something like a Mutex<T>. Since a
2394 // Mutex is not acquired when it is deleted, we can't rely on its
2395 // synchronization logic to make writes in thread A visible to a destructor
2396 // running in thread B.
2398 // Also note that the Acquire fence here could probably be replaced with an
2399 // Acquire load, which could improve performance in highly-contended
2400 // situations. See [2].
2402 // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
2403 // [2]: (https://github.com/rust-lang/rust/pull/41714)
2404 acquire
!(self.inner().strong
);
2412 impl<A
: Allocator
+ Clone
> Arc
<dyn Any
+ Send
+ Sync
, A
> {
2413 /// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
2418 /// use std::any::Any;
2419 /// use std::sync::Arc;
2421 /// fn print_if_string(value: Arc<dyn Any + Send + Sync>) {
2422 /// if let Ok(string) = value.downcast::<String>() {
2423 /// println!("String ({}): {}", string.len(), string);
2427 /// let my_string = "Hello World".to_string();
2428 /// print_if_string(Arc::new(my_string));
2429 /// print_if_string(Arc::new(0i8));
2432 #[stable(feature = "rc_downcast", since = "1.29.0")]
2433 pub fn downcast
<T
>(self) -> Result
<Arc
<T
, A
>, Self>
2435 T
: Any
+ Send
+ Sync
,
2437 if (*self).is
::<T
>() {
2439 let ptr
= self.ptr
.cast
::<ArcInner
<T
>>();
2440 let alloc
= self.alloc
.clone();
2442 Ok(Arc
::from_inner_in(ptr
, alloc
))
2449 /// Downcasts the `Arc<dyn Any + Send + Sync>` to a concrete type.
2451 /// For a safe alternative see [`downcast`].
2456 /// #![feature(downcast_unchecked)]
2458 /// use std::any::Any;
2459 /// use std::sync::Arc;
2461 /// let x: Arc<dyn Any + Send + Sync> = Arc::new(1_usize);
2464 /// assert_eq!(*x.downcast_unchecked::<usize>(), 1);
2470 /// The contained value must be of type `T`. Calling this method
2471 /// with the incorrect type is *undefined behavior*.
2474 /// [`downcast`]: Self::downcast
2476 #[unstable(feature = "downcast_unchecked", issue = "90850")]
2477 pub unsafe fn downcast_unchecked
<T
>(self) -> Arc
<T
, A
>
2479 T
: Any
+ Send
+ Sync
,
2482 let ptr
= self.ptr
.cast
::<ArcInner
<T
>>();
2483 let alloc
= self.alloc
.clone();
2485 Arc
::from_inner_in(ptr
, alloc
)
2491 /// Constructs a new `Weak<T>`, without allocating any memory.
2492 /// Calling [`upgrade`] on the return value always gives [`None`].
2494 /// [`upgrade`]: Weak::upgrade
2499 /// use std::sync::Weak;
2501 /// let empty: Weak<i64> = Weak::new();
2502 /// assert!(empty.upgrade().is_none());
2505 #[stable(feature = "downgraded_weak", since = "1.10.0")]
2506 #[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
2508 pub const fn new() -> Weak
<T
> {
2510 ptr
: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<ArcInner<T>>(usize::MAX)) }
,
2516 impl<T
, A
: Allocator
> Weak
<T
, A
> {
2517 /// Constructs a new `Weak<T, A>`, without allocating any memory, technically in the provided
2519 /// Calling [`upgrade`] on the return value always gives [`None`].
2521 /// [`upgrade`]: Weak::upgrade
2526 /// #![feature(allocator_api)]
2528 /// use std::sync::Weak;
2529 /// use std::alloc::System;
2531 /// let empty: Weak<i64, _> = Weak::new_in(System);
2532 /// assert!(empty.upgrade().is_none());
2535 #[unstable(feature = "allocator_api", issue = "32838")]
2536 pub fn new_in(alloc
: A
) -> Weak
<T
, A
> {
2538 ptr
: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<ArcInner<T>>(usize::MAX)) }
,
2544 /// Helper type to allow accessing the reference counts without
2545 /// making any assertions about the data field.
2546 struct WeakInner
<'a
> {
2547 weak
: &'a atomic
::AtomicUsize
,
2548 strong
: &'a atomic
::AtomicUsize
,
2551 impl<T
: ?Sized
> Weak
<T
> {
2552 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
2554 /// This can be used to safely get a strong reference (by calling [`upgrade`]
2555 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
2557 /// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
2558 /// as these don't own anything; the method still works on them).
2562 /// The pointer must have originated from the [`into_raw`] and must still own its potential
2565 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
2566 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
2567 /// count is not modified by this operation) and therefore it must be paired with a previous
2568 /// call to [`into_raw`].
2572 /// use std::sync::{Arc, Weak};
2574 /// let strong = Arc::new("hello".to_owned());
2576 /// let raw_1 = Arc::downgrade(&strong).into_raw();
2577 /// let raw_2 = Arc::downgrade(&strong).into_raw();
2579 /// assert_eq!(2, Arc::weak_count(&strong));
2581 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
2582 /// assert_eq!(1, Arc::weak_count(&strong));
2586 /// // Decrement the last weak count.
2587 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
2590 /// [`new`]: Weak::new
2591 /// [`into_raw`]: Weak::into_raw
2592 /// [`upgrade`]: Weak::upgrade
2594 #[stable(feature = "weak_into_raw", since = "1.45.0")]
2595 pub unsafe fn from_raw(ptr
: *const T
) -> Self {
2596 unsafe { Weak::from_raw_in(ptr, Global) }
2600 impl<T
: ?Sized
, A
: Allocator
> Weak
<T
, A
> {
2601 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
2603 /// The pointer is valid only if there are some strong references. The pointer may be dangling,
2604 /// unaligned or even [`null`] otherwise.
2609 /// use std::sync::Arc;
2612 /// let strong = Arc::new("hello".to_owned());
2613 /// let weak = Arc::downgrade(&strong);
2614 /// // Both point to the same object
2615 /// assert!(ptr::eq(&*strong, weak.as_ptr()));
2616 /// // The strong here keeps it alive, so we can still access the object.
2617 /// assert_eq!("hello", unsafe { &*weak.as_ptr() });
2620 /// // But not any more. We can do weak.as_ptr(), but accessing the pointer would lead to
2621 /// // undefined behaviour.
2622 /// // assert_eq!("hello", unsafe { &*weak.as_ptr() });
2625 /// [`null`]: core::ptr::null "ptr::null"
2627 #[stable(feature = "weak_into_raw", since = "1.45.0")]
2628 pub fn as_ptr(&self) -> *const T
{
2629 let ptr
: *mut ArcInner
<T
> = NonNull
::as_ptr(self.ptr
);
2631 if is_dangling(ptr
) {
2632 // If the pointer is dangling, we return the sentinel directly. This cannot be
2633 // a valid payload address, as the payload is at least as aligned as ArcInner (usize).
2636 // SAFETY: if is_dangling returns false, then the pointer is dereferenceable.
2637 // The payload may be dropped at this point, and we have to maintain provenance,
2638 // so use raw pointer manipulation.
2639 unsafe { ptr::addr_of_mut!((*ptr).data) }
2643 /// Consumes the `Weak<T>` and turns it into a raw pointer.
2645 /// This converts the weak pointer into a raw pointer, while still preserving the ownership of
2646 /// one weak reference (the weak count is not modified by this operation). It can be turned
2647 /// back into the `Weak<T>` with [`from_raw`].
2649 /// The same restrictions of accessing the target of the pointer as with
2650 /// [`as_ptr`] apply.
2655 /// use std::sync::{Arc, Weak};
2657 /// let strong = Arc::new("hello".to_owned());
2658 /// let weak = Arc::downgrade(&strong);
2659 /// let raw = weak.into_raw();
2661 /// assert_eq!(1, Arc::weak_count(&strong));
2662 /// assert_eq!("hello", unsafe { &*raw });
2664 /// drop(unsafe { Weak::from_raw(raw) });
2665 /// assert_eq!(0, Arc::weak_count(&strong));
2668 /// [`from_raw`]: Weak::from_raw
2669 /// [`as_ptr`]: Weak::as_ptr
2670 #[must_use = "`self` will be dropped if the result is not used"]
2671 #[stable(feature = "weak_into_raw", since = "1.45.0")]
2672 pub fn into_raw(self) -> *const T
{
2673 let result
= self.as_ptr();
2678 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>` in the provided
2681 /// This can be used to safely get a strong reference (by calling [`upgrade`]
2682 /// later) or to deallocate the weak count by dropping the `Weak<T>`.
2684 /// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
2685 /// as these don't own anything; the method still works on them).
2689 /// The pointer must have originated from the [`into_raw`] and must still own its potential
2690 /// weak reference, and must point to a block of memory allocated by `alloc`.
2692 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
2693 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
2694 /// count is not modified by this operation) and therefore it must be paired with a previous
2695 /// call to [`into_raw`].
2699 /// use std::sync::{Arc, Weak};
2701 /// let strong = Arc::new("hello".to_owned());
2703 /// let raw_1 = Arc::downgrade(&strong).into_raw();
2704 /// let raw_2 = Arc::downgrade(&strong).into_raw();
2706 /// assert_eq!(2, Arc::weak_count(&strong));
2708 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
2709 /// assert_eq!(1, Arc::weak_count(&strong));
2713 /// // Decrement the last weak count.
2714 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
2717 /// [`new`]: Weak::new
2718 /// [`into_raw`]: Weak::into_raw
2719 /// [`upgrade`]: Weak::upgrade
2721 #[unstable(feature = "allocator_api", issue = "32838")]
2722 pub unsafe fn from_raw_in(ptr
: *const T
, alloc
: A
) -> Self {
2723 // See Weak::as_ptr for context on how the input pointer is derived.
2725 let ptr
= if is_dangling(ptr
as *mut T
) {
2726 // This is a dangling Weak.
2727 ptr
as *mut ArcInner
<T
>
2729 // Otherwise, we're guaranteed the pointer came from a nondangling Weak.
2730 // SAFETY: data_offset is safe to call, as ptr references a real (potentially dropped) T.
2731 let offset
= unsafe { data_offset(ptr) }
;
2732 // Thus, we reverse the offset to get the whole RcBox.
2733 // SAFETY: the pointer originated from a Weak, so this offset is safe.
2734 unsafe { ptr.byte_sub(offset) as *mut ArcInner<T> }
2737 // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
2738 Weak { ptr: unsafe { NonNull::new_unchecked(ptr) }
, alloc
}
2742 impl<T
: ?Sized
, A
: Allocator
> Weak
<T
, A
> {
2743 /// Attempts to upgrade the `Weak` pointer to an [`Arc`], delaying
2744 /// dropping of the inner value if successful.
2746 /// Returns [`None`] if the inner value has since been dropped.
2751 /// use std::sync::Arc;
2753 /// let five = Arc::new(5);
2755 /// let weak_five = Arc::downgrade(&five);
2757 /// let strong_five: Option<Arc<_>> = weak_five.upgrade();
2758 /// assert!(strong_five.is_some());
2760 /// // Destroy all strong pointers.
2761 /// drop(strong_five);
2764 /// assert!(weak_five.upgrade().is_none());
2766 #[must_use = "this returns a new `Arc`, \
2767 without modifying the original weak pointer"]
2768 #[stable(feature = "arc_weak", since = "1.4.0")]
2769 pub fn upgrade(&self) -> Option
<Arc
<T
, A
>>
2774 fn checked_increment(n
: usize) -> Option
<usize> {
2775 // Any write of 0 we can observe leaves the field in permanently zero state.
2779 // See comments in `Arc::clone` for why we do this (for `mem::forget`).
2780 assert
!(n
<= MAX_REFCOUNT
, "{}", INTERNAL_OVERFLOW_ERROR
);
2784 // We use a CAS loop to increment the strong count instead of a
2785 // fetch_add as this function should never take the reference count
2786 // from zero to one.
2788 // Relaxed is fine for the failure case because we don't have any expectations about the new state.
2789 // Acquire is necessary for the success case to synchronise with `Arc::new_cyclic`, when the inner
2790 // value can be initialized after `Weak` references have already been created. In that case, we
2791 // expect to observe the fully initialized value.
2792 if self.inner()?
.strong
.fetch_update(Acquire
, Relaxed
, checked_increment
).is_ok() {
2793 // SAFETY: pointer is not null, verified in checked_increment
2794 unsafe { Some(Arc::from_inner_in(self.ptr, self.alloc.clone())) }
2800 /// Gets the number of strong (`Arc`) pointers pointing to this allocation.
2802 /// If `self` was created using [`Weak::new`], this will return 0.
2804 #[stable(feature = "weak_counts", since = "1.41.0")]
2805 pub fn strong_count(&self) -> usize {
2806 if let Some(inner
) = self.inner() { inner.strong.load(Relaxed) }
else { 0 }
2809 /// Gets an approximation of the number of `Weak` pointers pointing to this
2812 /// If `self` was created using [`Weak::new`], or if there are no remaining
2813 /// strong pointers, this will return 0.
2817 /// Due to implementation details, the returned value can be off by 1 in
2818 /// either direction when other threads are manipulating any `Arc`s or
2819 /// `Weak`s pointing to the same allocation.
2821 #[stable(feature = "weak_counts", since = "1.41.0")]
2822 pub fn weak_count(&self) -> usize {
2823 if let Some(inner
) = self.inner() {
2824 let weak
= inner
.weak
.load(Acquire
);
2825 let strong
= inner
.strong
.load(Relaxed
);
2829 // Since we observed that there was at least one strong pointer
2830 // after reading the weak count, we know that the implicit weak
2831 // reference (present whenever any strong references are alive)
2832 // was still around when we observed the weak count, and can
2833 // therefore safely subtract it.
2841 /// Returns `None` when the pointer is dangling and there is no allocated `ArcInner`,
2842 /// (i.e., when this `Weak` was created by `Weak::new`).
2844 fn inner(&self) -> Option
<WeakInner
<'_
>> {
2845 if is_dangling(self.ptr
.as_ptr()) {
2848 // We are careful to *not* create a reference covering the "data" field, as
2849 // the field may be mutated concurrently (for example, if the last `Arc`
2850 // is dropped, the data field will be dropped in-place).
2852 let ptr
= self.ptr
.as_ptr();
2853 WeakInner { strong: &(*ptr).strong, weak: &(*ptr).weak }
2858 /// Returns `true` if the two `Weak`s point to the same allocation similar to [`ptr::eq`], or if
2859 /// both don't point to any allocation (because they were created with `Weak::new()`). However,
2860 /// this function ignores the metadata of `dyn Trait` pointers.
2864 /// Since this compares pointers it means that `Weak::new()` will equal each
2865 /// other, even though they don't point to any allocation.
2870 /// use std::sync::Arc;
2872 /// let first_rc = Arc::new(5);
2873 /// let first = Arc::downgrade(&first_rc);
2874 /// let second = Arc::downgrade(&first_rc);
2876 /// assert!(first.ptr_eq(&second));
2878 /// let third_rc = Arc::new(5);
2879 /// let third = Arc::downgrade(&third_rc);
2881 /// assert!(!first.ptr_eq(&third));
2884 /// Comparing `Weak::new`.
2887 /// use std::sync::{Arc, Weak};
2889 /// let first = Weak::new();
2890 /// let second = Weak::new();
2891 /// assert!(first.ptr_eq(&second));
2893 /// let third_rc = Arc::new(());
2894 /// let third = Arc::downgrade(&third_rc);
2895 /// assert!(!first.ptr_eq(&third));
2898 /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
2901 #[stable(feature = "weak_ptr_eq", since = "1.39.0")]
2902 pub fn ptr_eq(&self, other
: &Self) -> bool
{
2903 ptr
::eq(self.ptr
.as_ptr() as *const (), other
.ptr
.as_ptr() as *const ())
2907 #[stable(feature = "arc_weak", since = "1.4.0")]
2908 impl<T
: ?Sized
, A
: Allocator
+ Clone
> Clone
for Weak
<T
, A
> {
2909 /// Makes a clone of the `Weak` pointer that points to the same allocation.
2914 /// use std::sync::{Arc, Weak};
2916 /// let weak_five = Arc::downgrade(&Arc::new(5));
2918 /// let _ = Weak::clone(&weak_five);
2921 fn clone(&self) -> Weak
<T
, A
> {
2922 let inner
= if let Some(inner
) = self.inner() {
2925 return Weak { ptr: self.ptr, alloc: self.alloc.clone() }
;
2927 // See comments in Arc::clone() for why this is relaxed. This can use a
2928 // fetch_add (ignoring the lock) because the weak count is only locked
2929 // where are *no other* weak pointers in existence. (So we can't be
2930 // running this code in that case).
2931 let old_size
= inner
.weak
.fetch_add(1, Relaxed
);
2933 // See comments in Arc::clone() for why we do this (for mem::forget).
2934 if old_size
> MAX_REFCOUNT
{
2938 Weak { ptr: self.ptr, alloc: self.alloc.clone() }
2942 #[stable(feature = "downgraded_weak", since = "1.10.0")]
2943 impl<T
> Default
for Weak
<T
> {
2944 /// Constructs a new `Weak<T>`, without allocating memory.
2945 /// Calling [`upgrade`] on the return value always
2948 /// [`upgrade`]: Weak::upgrade
2953 /// use std::sync::Weak;
2955 /// let empty: Weak<i64> = Default::default();
2956 /// assert!(empty.upgrade().is_none());
2958 fn default() -> Weak
<T
> {
2963 #[stable(feature = "arc_weak", since = "1.4.0")]
2964 unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Weak<T, A> {
2965 /// Drops the `Weak` pointer.
2970 /// use std::sync::{Arc, Weak};
2974 /// impl Drop for Foo {
2975 /// fn drop(&mut self) {
2976 /// println!("dropped!");
2980 /// let foo = Arc::new(Foo);
2981 /// let weak_foo = Arc::downgrade(&foo);
2982 /// let other_weak_foo = Weak::clone(&weak_foo);
2984 /// drop(weak_foo); // Doesn't print anything
2985 /// drop(foo); // Prints "dropped!"
2987 /// assert!(other_weak_foo.upgrade().is_none());
2989 fn drop(&mut self) {
2990 // If we find out that we were the last weak pointer, then its time to
2991 // deallocate the data entirely. See the discussion in Arc::drop() about
2992 // the memory orderings
2994 // It's not necessary to check for the locked state here, because the
2995 // weak count can only be locked if there was precisely one weak ref,
2996 // meaning that drop could only subsequently run ON that remaining weak
2997 // ref, which can only happen after the lock is released.
2998 let inner
= if let Some(inner
) = self.inner() { inner }
else { return }
;
3000 if inner
.weak
.fetch_sub(1, Release
) == 1 {
3001 acquire
!(inner
.weak
);
3003 self.alloc
.deallocate(self.ptr
.cast(), Layout
::for_value_raw(self.ptr
.as_ptr()))
3009 #[stable(feature = "rust1", since = "1.0.0")]
3010 trait ArcEqIdent
<T
: ?Sized
+ PartialEq
, A
: Allocator
> {
3011 fn eq(&self, other
: &Arc
<T
, A
>) -> bool
;
3012 fn ne(&self, other
: &Arc
<T
, A
>) -> bool
;
3015 #[stable(feature = "rust1", since = "1.0.0")]
3016 impl<T
: ?Sized
+ PartialEq
, A
: Allocator
> ArcEqIdent
<T
, A
> for Arc
<T
, A
> {
3018 default fn eq(&self, other
: &Arc
<T
, A
>) -> bool
{
3022 default fn ne(&self, other
: &Arc
<T
, A
>) -> bool
{
3027 /// We're doing this specialization here, and not as a more general optimization on `&T`, because it
3028 /// would otherwise add a cost to all equality checks on refs. We assume that `Arc`s are used to
3029 /// store large values, that are slow to clone, but also heavy to check for equality, causing this
3030 /// cost to pay off more easily. It's also more likely to have two `Arc` clones, that point to
3031 /// the same value, than two `&T`s.
3033 /// We can only do this when `T: Eq` as a `PartialEq` might be deliberately irreflexive.
3034 #[stable(feature = "rust1", since = "1.0.0")]
3035 impl<T
: ?Sized
+ crate::rc
::MarkerEq
, A
: Allocator
> ArcEqIdent
<T
, A
> for Arc
<T
, A
> {
3037 fn eq(&self, other
: &Arc
<T
, A
>) -> bool
{
3038 Arc
::ptr_eq(self, other
) || **self == **other
3042 fn ne(&self, other
: &Arc
<T
, A
>) -> bool
{
3043 !Arc
::ptr_eq(self, other
) && **self != **other
3047 #[stable(feature = "rust1", since = "1.0.0")]
3048 impl<T
: ?Sized
+ PartialEq
, A
: Allocator
> PartialEq
for Arc
<T
, A
> {
3049 /// Equality for two `Arc`s.
3051 /// Two `Arc`s are equal if their inner values are equal, even if they are
3052 /// stored in different allocation.
3054 /// If `T` also implements `Eq` (implying reflexivity of equality),
3055 /// two `Arc`s that point to the same allocation are always equal.
3060 /// use std::sync::Arc;
3062 /// let five = Arc::new(5);
3064 /// assert!(five == Arc::new(5));
3067 fn eq(&self, other
: &Arc
<T
, A
>) -> bool
{
3068 ArcEqIdent
::eq(self, other
)
3071 /// Inequality for two `Arc`s.
3073 /// Two `Arc`s are not equal if their inner values are not equal.
3075 /// If `T` also implements `Eq` (implying reflexivity of equality),
3076 /// two `Arc`s that point to the same value are always equal.
3081 /// use std::sync::Arc;
3083 /// let five = Arc::new(5);
3085 /// assert!(five != Arc::new(6));
3088 fn ne(&self, other
: &Arc
<T
, A
>) -> bool
{
3089 ArcEqIdent
::ne(self, other
)
3093 #[stable(feature = "rust1", since = "1.0.0")]
3094 impl<T
: ?Sized
+ PartialOrd
, A
: Allocator
> PartialOrd
for Arc
<T
, A
> {
3095 /// Partial comparison for two `Arc`s.
3097 /// The two are compared by calling `partial_cmp()` on their inner values.
3102 /// use std::sync::Arc;
3103 /// use std::cmp::Ordering;
3105 /// let five = Arc::new(5);
3107 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6)));
3109 fn partial_cmp(&self, other
: &Arc
<T
, A
>) -> Option
<Ordering
> {
3110 (**self).partial_cmp(&**other
)
3113 /// Less-than comparison for two `Arc`s.
3115 /// The two are compared by calling `<` on their inner values.
3120 /// use std::sync::Arc;
3122 /// let five = Arc::new(5);
3124 /// assert!(five < Arc::new(6));
3126 fn lt(&self, other
: &Arc
<T
, A
>) -> bool
{
3127 *(*self) < *(*other
)
3130 /// 'Less than or equal to' comparison for two `Arc`s.
3132 /// The two are compared by calling `<=` on their inner values.
3137 /// use std::sync::Arc;
3139 /// let five = Arc::new(5);
3141 /// assert!(five <= Arc::new(5));
3143 fn le(&self, other
: &Arc
<T
, A
>) -> bool
{
3144 *(*self) <= *(*other
)
3147 /// Greater-than comparison for two `Arc`s.
3149 /// The two are compared by calling `>` on their inner values.
3154 /// use std::sync::Arc;
3156 /// let five = Arc::new(5);
3158 /// assert!(five > Arc::new(4));
3160 fn gt(&self, other
: &Arc
<T
, A
>) -> bool
{
3161 *(*self) > *(*other
)
3164 /// 'Greater than or equal to' comparison for two `Arc`s.
3166 /// The two are compared by calling `>=` on their inner values.
3171 /// use std::sync::Arc;
3173 /// let five = Arc::new(5);
3175 /// assert!(five >= Arc::new(5));
3177 fn ge(&self, other
: &Arc
<T
, A
>) -> bool
{
3178 *(*self) >= *(*other
)
3181 #[stable(feature = "rust1", since = "1.0.0")]
3182 impl<T
: ?Sized
+ Ord
, A
: Allocator
> Ord
for Arc
<T
, A
> {
3183 /// Comparison for two `Arc`s.
3185 /// The two are compared by calling `cmp()` on their inner values.
3190 /// use std::sync::Arc;
3191 /// use std::cmp::Ordering;
3193 /// let five = Arc::new(5);
3195 /// assert_eq!(Ordering::Less, five.cmp(&Arc::new(6)));
3197 fn cmp(&self, other
: &Arc
<T
, A
>) -> Ordering
{
3198 (**self).cmp(&**other
)
3201 #[stable(feature = "rust1", since = "1.0.0")]
3202 impl<T
: ?Sized
+ Eq
, A
: Allocator
> Eq
for Arc
<T
, A
> {}
3204 #[stable(feature = "rust1", since = "1.0.0")]
3205 impl<T
: ?Sized
+ fmt
::Display
, A
: Allocator
> fmt
::Display
for Arc
<T
, A
> {
3206 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
3207 fmt
::Display
::fmt(&**self, f
)
3211 #[stable(feature = "rust1", since = "1.0.0")]
3212 impl<T
: ?Sized
+ fmt
::Debug
, A
: Allocator
> fmt
::Debug
for Arc
<T
, A
> {
3213 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
3214 fmt
::Debug
::fmt(&**self, f
)
3218 #[stable(feature = "rust1", since = "1.0.0")]
3219 impl<T
: ?Sized
, A
: Allocator
> fmt
::Pointer
for Arc
<T
, A
> {
3220 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
3221 fmt
::Pointer
::fmt(&(&**self as *const T
), f
)
3225 #[cfg(not(no_global_oom_handling))]
3226 #[stable(feature = "rust1", since = "1.0.0")]
3227 impl<T
: Default
> Default
for Arc
<T
> {
3228 /// Creates a new `Arc<T>`, with the `Default` value for `T`.
3233 /// use std::sync::Arc;
3235 /// let x: Arc<i32> = Default::default();
3236 /// assert_eq!(*x, 0);
3238 fn default() -> Arc
<T
> {
3239 Arc
::new(Default
::default())
3243 #[stable(feature = "rust1", since = "1.0.0")]
3244 impl<T
: ?Sized
+ Hash
, A
: Allocator
> Hash
for Arc
<T
, A
> {
3245 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
3246 (**self).hash(state
)
3250 #[cfg(not(no_global_oom_handling))]
3251 #[stable(feature = "from_for_ptrs", since = "1.6.0")]
3252 impl<T
> From
<T
> for Arc
<T
> {
3253 /// Converts a `T` into an `Arc<T>`
3255 /// The conversion moves the value into a
3256 /// newly allocated `Arc`. It is equivalent to
3257 /// calling `Arc::new(t)`.
3261 /// # use std::sync::Arc;
3263 /// let arc = Arc::new(5);
3265 /// assert_eq!(Arc::from(x), arc);
3267 fn from(t
: T
) -> Self {
3272 #[cfg(not(no_global_oom_handling))]
3273 #[stable(feature = "shared_from_array", since = "1.74.0")]
3274 impl<T
, const N
: usize> From
<[T
; N
]> for Arc
<[T
]> {
3275 /// Converts a [`[T; N]`](prim@array) into an `Arc<[T]>`.
3277 /// The conversion moves the array into a newly allocated `Arc`.
3282 /// # use std::sync::Arc;
3283 /// let original: [i32; 3] = [1, 2, 3];
3284 /// let shared: Arc<[i32]> = Arc::from(original);
3285 /// assert_eq!(&[1, 2, 3], &shared[..]);
3288 fn from(v
: [T
; N
]) -> Arc
<[T
]> {
3289 Arc
::<[T
; N
]>::from(v
)
3293 #[cfg(not(no_global_oom_handling))]
3294 #[stable(feature = "shared_from_slice", since = "1.21.0")]
3295 impl<T
: Clone
> From
<&[T
]> for Arc
<[T
]> {
3296 /// Allocate a reference-counted slice and fill it by cloning `v`'s items.
3301 /// # use std::sync::Arc;
3302 /// let original: &[i32] = &[1, 2, 3];
3303 /// let shared: Arc<[i32]> = Arc::from(original);
3304 /// assert_eq!(&[1, 2, 3], &shared[..]);
3307 fn from(v
: &[T
]) -> Arc
<[T
]> {
3308 <Self as ArcFromSlice
<T
>>::from_slice(v
)
3312 #[cfg(not(no_global_oom_handling))]
3313 #[stable(feature = "shared_from_slice", since = "1.21.0")]
3314 impl From
<&str> for Arc
<str> {
3315 /// Allocate a reference-counted `str` and copy `v` into it.
3320 /// # use std::sync::Arc;
3321 /// let shared: Arc<str> = Arc::from("eggplant");
3322 /// assert_eq!("eggplant", &shared[..]);
3325 fn from(v
: &str) -> Arc
<str> {
3326 let arc
= Arc
::<[u8]>::from(v
.as_bytes());
3327 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const str) }
3331 #[cfg(not(no_global_oom_handling))]
3332 #[stable(feature = "shared_from_slice", since = "1.21.0")]
3333 impl From
<String
> for Arc
<str> {
3334 /// Allocate a reference-counted `str` and copy `v` into it.
3339 /// # use std::sync::Arc;
3340 /// let unique: String = "eggplant".to_owned();
3341 /// let shared: Arc<str> = Arc::from(unique);
3342 /// assert_eq!("eggplant", &shared[..]);
3345 fn from(v
: String
) -> Arc
<str> {
3350 #[cfg(not(no_global_oom_handling))]
3351 #[stable(feature = "shared_from_slice", since = "1.21.0")]
3352 impl<T
: ?Sized
, A
: Allocator
> From
<Box
<T
, A
>> for Arc
<T
, A
> {
3353 /// Move a boxed object to a new, reference-counted allocation.
3358 /// # use std::sync::Arc;
3359 /// let unique: Box<str> = Box::from("eggplant");
3360 /// let shared: Arc<str> = Arc::from(unique);
3361 /// assert_eq!("eggplant", &shared[..]);
3364 fn from(v
: Box
<T
, A
>) -> Arc
<T
, A
> {
3369 #[cfg(not(no_global_oom_handling))]
3370 #[stable(feature = "shared_from_slice", since = "1.21.0")]
3371 impl<T
, A
: Allocator
+ Clone
> From
<Vec
<T
, A
>> for Arc
<[T
], A
> {
3372 /// Allocate a reference-counted slice and move `v`'s items into it.
3377 /// # use std::sync::Arc;
3378 /// let unique: Vec<i32> = vec![1, 2, 3];
3379 /// let shared: Arc<[i32]> = Arc::from(unique);
3380 /// assert_eq!(&[1, 2, 3], &shared[..]);
3383 fn from(v
: Vec
<T
, A
>) -> Arc
<[T
], A
> {
3385 let (vec_ptr
, len
, cap
, alloc
) = v
.into_raw_parts_with_alloc();
3387 let rc_ptr
= Self::allocate_for_slice_in(len
, &alloc
);
3388 ptr
::copy_nonoverlapping(vec_ptr
, &mut (*rc_ptr
).data
as *mut [T
] as *mut T
, len
);
3390 // Create a `Vec<T, &A>` with length 0, to deallocate the buffer
3391 // without dropping its contents or the allocator
3392 let _
= Vec
::from_raw_parts_in(vec_ptr
, 0, cap
, &alloc
);
3394 Self::from_ptr_in(rc_ptr
, alloc
)
3399 #[stable(feature = "shared_from_cow", since = "1.45.0")]
3400 impl<'a
, B
> From
<Cow
<'a
, B
>> for Arc
<B
>
3402 B
: ToOwned
+ ?Sized
,
3403 Arc
<B
>: From
<&'a B
> + From
<B
::Owned
>,
3405 /// Create an atomically reference-counted pointer from
3406 /// a clone-on-write pointer by copying its content.
3411 /// # use std::sync::Arc;
3412 /// # use std::borrow::Cow;
3413 /// let cow: Cow<'_, str> = Cow::Borrowed("eggplant");
3414 /// let shared: Arc<str> = Arc::from(cow);
3415 /// assert_eq!("eggplant", &shared[..]);
3418 fn from(cow
: Cow
<'a
, B
>) -> Arc
<B
> {
3420 Cow
::Borrowed(s
) => Arc
::from(s
),
3421 Cow
::Owned(s
) => Arc
::from(s
),
3426 #[stable(feature = "shared_from_str", since = "1.62.0")]
3427 impl From
<Arc
<str>> for Arc
<[u8]> {
3428 /// Converts an atomically reference-counted string slice into a byte slice.
3433 /// # use std::sync::Arc;
3434 /// let string: Arc<str> = Arc::from("eggplant");
3435 /// let bytes: Arc<[u8]> = Arc::from(string);
3436 /// assert_eq!("eggplant".as_bytes(), bytes.as_ref());
3439 fn from(rc
: Arc
<str>) -> Self {
3440 // SAFETY: `str` has the same layout as `[u8]`.
3441 unsafe { Arc::from_raw(Arc::into_raw(rc) as *const [u8]) }
3445 #[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
3446 impl<T
, A
: Allocator
+ Clone
, const N
: usize> TryFrom
<Arc
<[T
], A
>> for Arc
<[T
; N
], A
> {
3447 type Error
= Arc
<[T
], A
>;
3449 fn try_from(boxed_slice
: Arc
<[T
], A
>) -> Result
<Self, Self::Error
> {
3450 if boxed_slice
.len() == N
{
3451 let alloc
= boxed_slice
.alloc
.clone();
3452 Ok(unsafe { Arc::from_raw_in(Arc::into_raw(boxed_slice) as *mut [T; N], alloc) }
)
3459 #[cfg(not(no_global_oom_handling))]
3460 #[stable(feature = "shared_from_iter", since = "1.37.0")]
3461 impl<T
> FromIterator
<T
> for Arc
<[T
]> {
3462 /// Takes each element in the `Iterator` and collects it into an `Arc<[T]>`.
3464 /// # Performance characteristics
3466 /// ## The general case
3468 /// In the general case, collecting into `Arc<[T]>` is done by first
3469 /// collecting into a `Vec<T>`. That is, when writing the following:
3472 /// # use std::sync::Arc;
3473 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0).collect();
3474 /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
3477 /// this behaves as if we wrote:
3480 /// # use std::sync::Arc;
3481 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0)
3482 /// .collect::<Vec<_>>() // The first set of allocations happens here.
3483 /// .into(); // A second allocation for `Arc<[T]>` happens here.
3484 /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
3487 /// This will allocate as many times as needed for constructing the `Vec<T>`
3488 /// and then it will allocate once for turning the `Vec<T>` into the `Arc<[T]>`.
3490 /// ## Iterators of known length
3492 /// When your `Iterator` implements `TrustedLen` and is of an exact size,
3493 /// a single allocation will be made for the `Arc<[T]>`. For example:
3496 /// # use std::sync::Arc;
3497 /// let evens: Arc<[u8]> = (0..10).collect(); // Just a single allocation happens here.
3498 /// # assert_eq!(&*evens, &*(0..10).collect::<Vec<_>>());
3500 fn from_iter
<I
: IntoIterator
<Item
= T
>>(iter
: I
) -> Self {
3501 ToArcSlice
::to_arc_slice(iter
.into_iter())
3505 /// Specialization trait used for collecting into `Arc<[T]>`.
3506 trait ToArcSlice
<T
>: Iterator
<Item
= T
> + Sized
{
3507 fn to_arc_slice(self) -> Arc
<[T
]>;
3510 #[cfg(not(no_global_oom_handling))]
3511 impl<T
, I
: Iterator
<Item
= T
>> ToArcSlice
<T
> for I
{
3512 default fn to_arc_slice(self) -> Arc
<[T
]> {
3513 self.collect
::<Vec
<T
>>().into()
3517 #[cfg(not(no_global_oom_handling))]
3518 impl<T
, I
: iter
::TrustedLen
<Item
= T
>> ToArcSlice
<T
> for I
{
3519 fn to_arc_slice(self) -> Arc
<[T
]> {
3520 // This is the case for a `TrustedLen` iterator.
3521 let (low
, high
) = self.size_hint();
3522 if let Some(high
) = high
{
3526 "TrustedLen iterator's size hint is not exact: {:?}",
3531 // SAFETY: We need to ensure that the iterator has an exact length and we have.
3532 Arc
::from_iter_exact(self, low
)
3535 // TrustedLen contract guarantees that `upper_bound == None` implies an iterator
3536 // length exceeding `usize::MAX`.
3537 // The default implementation would collect into a vec which would panic.
3538 // Thus we panic here immediately without invoking `Vec` code.
3539 panic
!("capacity overflow");
3544 #[stable(feature = "rust1", since = "1.0.0")]
3545 impl<T
: ?Sized
, A
: Allocator
> borrow
::Borrow
<T
> for Arc
<T
, A
> {
3546 fn borrow(&self) -> &T
{
3551 #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
3552 impl<T
: ?Sized
, A
: Allocator
> AsRef
<T
> for Arc
<T
, A
> {
3553 fn as_ref(&self) -> &T
{
3558 #[stable(feature = "pin", since = "1.33.0")]
3559 impl<T
: ?Sized
, A
: Allocator
> Unpin
for Arc
<T
, A
> {}
3561 /// Get the offset within an `ArcInner` for the payload behind a pointer.
3565 /// The pointer must point to (and have valid metadata for) a previously
3566 /// valid instance of T, but the T is allowed to be dropped.
3567 unsafe fn data_offset
<T
: ?Sized
>(ptr
: *const T
) -> usize {
3568 // Align the unsized value to the end of the ArcInner.
3569 // Because RcBox is repr(C), it will always be the last field in memory.
3570 // SAFETY: since the only unsized types possible are slices, trait objects,
3571 // and extern types, the input safety requirement is currently enough to
3572 // satisfy the requirements of align_of_val_raw; this is an implementation
3573 // detail of the language that must not be relied upon outside of std.
3574 unsafe { data_offset_align(align_of_val_raw(ptr)) }
3578 fn data_offset_align(align
: usize) -> usize {
3579 let layout
= Layout
::new
::<ArcInner
<()>>();
3580 layout
.size() + layout
.padding_needed_for(align
)
3583 #[stable(feature = "arc_error", since = "1.52.0")]
3584 impl<T
: core
::error
::Error
+ ?Sized
> core
::error
::Error
for Arc
<T
> {
3585 #[allow(deprecated, deprecated_in_future)]
3586 fn description(&self) -> &str {
3587 core
::error
::Error
::description(&**self)
3590 #[allow(deprecated)]
3591 fn cause(&self) -> Option
<&dyn core
::error
::Error
> {
3592 core
::error
::Error
::cause(&**self)
3595 fn source(&self) -> Option
<&(dyn core
::error
::Error
+ '
static)> {
3596 core
::error
::Error
::source(&**self)
3599 fn provide
<'a
>(&'a
self, req
: &mut core
::error
::Request
<'a
>) {
3600 core
::error
::Error
::provide(&**self, req
);