]> git.proxmox.com Git - rustc.git/blob - library/alloc/src/sync.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / library / alloc / src / sync.rs
1 #![stable(feature = "rust1", since = "1.0.0")]
2
3 //! Thread-safe reference-counting pointers.
4 //!
5 //! See the [`Arc<T>`][Arc] documentation for more details.
6 //!
7 //! **Note**: This module is only available on platforms that support atomic
8 //! loads and stores of pointers. This may be detected at compile time using
9 //! `#[cfg(target_has_atomic = "ptr")]`.
10
11 use core::any::Any;
12 use core::borrow;
13 use core::cmp::Ordering;
14 use core::fmt;
15 use core::hash::{Hash, Hasher};
16 use core::hint;
17 use core::intrinsics::abort;
18 #[cfg(not(no_global_oom_handling))]
19 use core::iter;
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};
26 use core::pin::Pin;
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};
32
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))]
44 use crate::vec::Vec;
45
46 #[cfg(test)]
47 mod tests;
48
49 /// A soft limit on the amount of references that may be made to an `Arc`.
50 ///
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).
54 ///
55 /// This is a global invariant, and also applies when using a compare-exchange loop.
56 ///
57 /// See comment in `Arc::clone`.
58 const MAX_REFCOUNT: usize = (isize::MAX) as usize;
59
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";
62
63 #[cfg(not(sanitize = "thread"))]
64 macro_rules! acquire {
65 ($x:expr) => {
66 atomic::fence(Acquire)
67 };
68 }
69
70 // ThreadSanitizer does not support memory fences. To avoid false positive
71 // reports in Arc / Weak implementation use atomic loads for synchronization
72 // instead.
73 #[cfg(sanitize = "thread")]
74 macro_rules! acquire {
75 ($x:expr) => {
76 $x.load(Acquire)
77 };
78 }
79
80 /// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
81 /// Reference Counted'.
82 ///
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.
89 ///
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]
94 /// types.
95 ///
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")]`.
100 ///
101 /// ## Thread Safety
102 ///
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.
111 ///
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.
122 ///
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].
125 ///
126 /// ## Breaking cycles with `Weak`
127 ///
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.
134 ///
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.
139 ///
140 /// # Cloning references
141 ///
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].
144 ///
145 /// ```
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
152 /// ```
153 ///
154 /// ## `Deref` behavior
155 ///
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]:
160 ///
161 /// ```
162 /// use std::sync::Arc;
163 ///
164 /// let my_arc = Arc::new(());
165 /// let my_weak = Arc::downgrade(&my_arc);
166 /// ```
167 ///
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.
171 ///
172 /// ```
173 /// use std::sync::Arc;
174 ///
175 /// let arc = Arc::new(());
176 /// // Method-call syntax
177 /// let arc2 = arc.clone();
178 /// // Fully qualified syntax
179 /// let arc3 = Arc::clone(&arc);
180 /// ```
181 ///
182 /// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
183 /// already been dropped.
184 ///
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
197 ///
198 /// # Examples
199 ///
200 /// Sharing some immutable data between threads:
201 ///
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
205 // tests.
206 /// ```no_run
207 /// use std::sync::Arc;
208 /// use std::thread;
209 ///
210 /// let five = Arc::new(5);
211 ///
212 /// for _ in 0..10 {
213 /// let five = Arc::clone(&five);
214 ///
215 /// thread::spawn(move || {
216 /// println!("{five:?}");
217 /// });
218 /// }
219 /// ```
220 ///
221 /// Sharing a mutable [`AtomicUsize`]:
222 ///
223 /// [`AtomicUsize`]: core::sync::atomic::AtomicUsize "sync::atomic::AtomicUsize"
224 ///
225 /// ```no_run
226 /// use std::sync::Arc;
227 /// use std::sync::atomic::{AtomicUsize, Ordering};
228 /// use std::thread;
229 ///
230 /// let val = Arc::new(AtomicUsize::new(5));
231 ///
232 /// for _ in 0..10 {
233 /// let val = Arc::clone(&val);
234 ///
235 /// thread::spawn(move || {
236 /// let v = val.fetch_add(1, Ordering::SeqCst);
237 /// println!("{v:?}");
238 /// });
239 /// }
240 /// ```
241 ///
242 /// See the [`rc` documentation][rc_examples] for more examples of reference
243 /// counting in general.
244 ///
245 /// [rc_examples]: crate::rc#examples
246 #[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
247 #[stable(feature = "rust1", since = "1.0.0")]
248 pub struct Arc<
249 T: ?Sized,
250 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
251 > {
252 ptr: NonNull<ArcInner<T>>,
253 phantom: PhantomData<ArcInner<T>>,
254 alloc: A,
255 }
256
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> {}
261
262 #[stable(feature = "catch_unwind", since = "1.9.0")]
263 impl<T: RefUnwindSafe + ?Sized, A: Allocator + UnwindSafe> UnwindSafe for Arc<T, A> {}
264
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> {}
267
268 #[unstable(feature = "dispatch_from_dyn", issue = "none")]
269 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T> {}
270
271 impl<T: ?Sized> Arc<T> {
272 unsafe fn from_inner(ptr: NonNull<ArcInner<T>>) -> Self {
273 unsafe { Self::from_inner_in(ptr, Global) }
274 }
275
276 unsafe fn from_ptr(ptr: *mut ArcInner<T>) -> Self {
277 unsafe { Self::from_ptr_in(ptr, Global) }
278 }
279 }
280
281 impl<T: ?Sized, A: Allocator> Arc<T, A> {
282 #[inline]
283 unsafe fn from_inner_in(ptr: NonNull<ArcInner<T>>, alloc: A) -> Self {
284 Self { ptr, phantom: PhantomData, alloc }
285 }
286
287 #[inline]
288 unsafe fn from_ptr_in(ptr: *mut ArcInner<T>, alloc: A) -> Self {
289 unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) }
290 }
291 }
292
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>.
296 ///
297 /// Since a `Weak` reference does not count towards ownership, it will not
298 /// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
299 /// guarantees about the value still being present. Thus it may return [`None`]
300 /// when [`upgrade`]d. Note however that a `Weak` reference *does* prevent the allocation
301 /// itself (the backing store) from being deallocated.
302 ///
303 /// A `Weak` pointer is useful for keeping a temporary reference to the allocation
304 /// managed by [`Arc`] without preventing its inner value from being dropped. It is also used to
305 /// prevent circular references between [`Arc`] pointers, since mutual owning references
306 /// would never allow either [`Arc`] to be dropped. For example, a tree could
307 /// have strong [`Arc`] pointers from parent nodes to children, and `Weak`
308 /// pointers from children back to their parents.
309 ///
310 /// The typical way to obtain a `Weak` pointer is to call [`Arc::downgrade`].
311 ///
312 /// [`upgrade`]: Weak::upgrade
313 #[stable(feature = "arc_weak", since = "1.4.0")]
314 pub struct Weak<
315 T: ?Sized,
316 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
317 > {
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>>,
325 alloc: A,
326 }
327
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> {}
332
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> {}
337
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 {
341 write!(f, "(Weak)")
342 }
343 }
344
345 // This is repr(C) to future-proof against possible field-reordering, which
346 // would interfere with otherwise safe [into|from]_raw() of transmutable
347 // inner types.
348 #[repr(C)]
349 struct ArcInner<T: ?Sized> {
350 strong: atomic::AtomicUsize,
351
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,
356
357 data: T,
358 }
359
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()
367 }
368
369 unsafe impl<T: ?Sized + Sync + Send> Send for ArcInner<T> {}
370 unsafe impl<T: ?Sized + Sync + Send> Sync for ArcInner<T> {}
371
372 impl<T> Arc<T> {
373 /// Constructs a new `Arc<T>`.
374 ///
375 /// # Examples
376 ///
377 /// ```
378 /// use std::sync::Arc;
379 ///
380 /// let five = Arc::new(5);
381 /// ```
382 #[cfg(not(no_global_oom_handling))]
383 #[inline]
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),
391 data,
392 });
393 unsafe { Self::from_inner(Box::leak(x).into()) }
394 }
395
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.
398 ///
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`.
404 ///
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.
409 ///
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.
413 ///
414 /// # Panics
415 ///
416 /// If `data_fn` panics, the panic is propagated to the caller, and the
417 /// temporary [`Weak<T>`] is dropped normally.
418 ///
419 /// # Example
420 ///
421 /// ```
422 /// # #![allow(dead_code)]
423 /// use std::sync::{Arc, Weak};
424 ///
425 /// struct Gadget {
426 /// me: Weak<Gadget>,
427 /// }
428 ///
429 /// impl 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() }
437 /// })
438 /// }
439 ///
440 /// /// Return a reference counted pointer to Self.
441 /// fn me(&self) -> Arc<Self> {
442 /// self.me.upgrade().unwrap()
443 /// }
444 /// }
445 /// ```
446 /// [`upgrade`]: Weak::upgrade
447 #[cfg(not(no_global_oom_handling))]
448 #[inline]
449 #[stable(feature = "arc_new_cyclic", since = "1.60.0")]
450 pub fn new_cyclic<F>(data_fn: F) -> Arc<T>
451 where
452 F: FnOnce(&Weak<T>) -> T,
453 {
454 // Construct the inner in the "uninitialized" state with a single
455 // weak reference.
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(),
460 }))
461 .into();
462 let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast();
463
464 let weak = Weak { ptr: init_ptr, alloc: Global };
465
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
471 // otherwise.
472 let data = data_fn(&weak);
473
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);
479
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`.
483 //
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).
489 //
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");
494
495 Arc::from_inner(init_ptr)
496 };
497
498 // Strong references should collectively own a shared weak reference,
499 // so don't run the destructor for our old weak reference.
500 mem::forget(weak);
501 strong
502 }
503
504 /// Constructs a new `Arc` with uninitialized contents.
505 ///
506 /// # Examples
507 ///
508 /// ```
509 /// #![feature(new_uninit)]
510 /// #![feature(get_mut_unchecked)]
511 ///
512 /// use std::sync::Arc;
513 ///
514 /// let mut five = Arc::<u32>::new_uninit();
515 ///
516 /// // Deferred initialization:
517 /// Arc::get_mut(&mut five).unwrap().write(5);
518 ///
519 /// let five = unsafe { five.assume_init() };
520 ///
521 /// assert_eq!(*five, 5)
522 /// ```
523 #[cfg(not(no_global_oom_handling))]
524 #[inline]
525 #[unstable(feature = "new_uninit", issue = "63291")]
526 #[must_use]
527 pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
528 unsafe {
529 Arc::from_ptr(Arc::allocate_for_layout(
530 Layout::new::<T>(),
531 |layout| Global.allocate(layout),
532 <*mut u8>::cast,
533 ))
534 }
535 }
536
537 /// Constructs a new `Arc` with uninitialized contents, with the memory
538 /// being filled with `0` bytes.
539 ///
540 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
541 /// of this method.
542 ///
543 /// # Examples
544 ///
545 /// ```
546 /// #![feature(new_uninit)]
547 ///
548 /// use std::sync::Arc;
549 ///
550 /// let zero = Arc::<u32>::new_zeroed();
551 /// let zero = unsafe { zero.assume_init() };
552 ///
553 /// assert_eq!(*zero, 0)
554 /// ```
555 ///
556 /// [zeroed]: mem::MaybeUninit::zeroed
557 #[cfg(not(no_global_oom_handling))]
558 #[inline]
559 #[unstable(feature = "new_uninit", issue = "63291")]
560 #[must_use]
561 pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
562 unsafe {
563 Arc::from_ptr(Arc::allocate_for_layout(
564 Layout::new::<T>(),
565 |layout| Global.allocate_zeroed(layout),
566 <*mut u8>::cast,
567 ))
568 }
569 }
570
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")]
575 #[must_use]
576 pub fn pin(data: T) -> Pin<Arc<T>> {
577 unsafe { Pin::new_unchecked(Arc::new(data)) }
578 }
579
580 /// Constructs a new `Pin<Arc<T>>`, return an error if allocation fails.
581 #[unstable(feature = "allocator_api", issue = "32838")]
582 #[inline]
583 pub fn try_pin(data: T) -> Result<Pin<Arc<T>>, AllocError> {
584 unsafe { Ok(Pin::new_unchecked(Arc::try_new(data)?)) }
585 }
586
587 /// Constructs a new `Arc<T>`, returning an error if allocation fails.
588 ///
589 /// # Examples
590 ///
591 /// ```
592 /// #![feature(allocator_api)]
593 /// use std::sync::Arc;
594 ///
595 /// let five = Arc::try_new(5)?;
596 /// # Ok::<(), std::alloc::AllocError>(())
597 /// ```
598 #[unstable(feature = "allocator_api", issue = "32838")]
599 #[inline]
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),
606 data,
607 })?;
608 unsafe { Ok(Self::from_inner(Box::leak(x).into())) }
609 }
610
611 /// Constructs a new `Arc` with uninitialized contents, returning an error
612 /// if allocation fails.
613 ///
614 /// # Examples
615 ///
616 /// ```
617 /// #![feature(new_uninit, allocator_api)]
618 /// #![feature(get_mut_unchecked)]
619 ///
620 /// use std::sync::Arc;
621 ///
622 /// let mut five = Arc::<u32>::try_new_uninit()?;
623 ///
624 /// // Deferred initialization:
625 /// Arc::get_mut(&mut five).unwrap().write(5);
626 ///
627 /// let five = unsafe { five.assume_init() };
628 ///
629 /// assert_eq!(*five, 5);
630 /// # Ok::<(), std::alloc::AllocError>(())
631 /// ```
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> {
635 unsafe {
636 Ok(Arc::from_ptr(Arc::try_allocate_for_layout(
637 Layout::new::<T>(),
638 |layout| Global.allocate(layout),
639 <*mut u8>::cast,
640 )?))
641 }
642 }
643
644 /// Constructs a new `Arc` with uninitialized contents, with the memory
645 /// being filled with `0` bytes, returning an error if allocation fails.
646 ///
647 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
648 /// of this method.
649 ///
650 /// # Examples
651 ///
652 /// ```
653 /// #![feature(new_uninit, allocator_api)]
654 ///
655 /// use std::sync::Arc;
656 ///
657 /// let zero = Arc::<u32>::try_new_zeroed()?;
658 /// let zero = unsafe { zero.assume_init() };
659 ///
660 /// assert_eq!(*zero, 0);
661 /// # Ok::<(), std::alloc::AllocError>(())
662 /// ```
663 ///
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> {
668 unsafe {
669 Ok(Arc::from_ptr(Arc::try_allocate_for_layout(
670 Layout::new::<T>(),
671 |layout| Global.allocate_zeroed(layout),
672 <*mut u8>::cast,
673 )?))
674 }
675 }
676 }
677
678 impl<T, A: Allocator> Arc<T, A> {
679 /// Returns a reference to the underlying allocator.
680 ///
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.
684 #[inline]
685 #[unstable(feature = "allocator_api", issue = "32838")]
686 pub fn allocator(this: &Self) -> &A {
687 &this.alloc
688 }
689 /// Constructs a new `Arc<T>` in the provided allocator.
690 ///
691 /// # Examples
692 ///
693 /// ```
694 /// #![feature(allocator_api)]
695 ///
696 /// use std::sync::Arc;
697 /// use std::alloc::System;
698 ///
699 /// let five = Arc::new_in(5, System);
700 /// ```
701 #[inline]
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
707 let x = Box::new_in(
708 ArcInner {
709 strong: atomic::AtomicUsize::new(1),
710 weak: atomic::AtomicUsize::new(1),
711 data,
712 },
713 alloc,
714 );
715 let (ptr, alloc) = Box::into_unique(x);
716 unsafe { Self::from_inner_in(ptr.into(), alloc) }
717 }
718
719 /// Constructs a new `Arc` with uninitialized contents in the provided allocator.
720 ///
721 /// # Examples
722 ///
723 /// ```
724 /// #![feature(new_uninit)]
725 /// #![feature(get_mut_unchecked)]
726 /// #![feature(allocator_api)]
727 ///
728 /// use std::sync::Arc;
729 /// use std::alloc::System;
730 ///
731 /// let mut five = Arc::<u32, _>::new_uninit_in(System);
732 ///
733 /// let five = unsafe {
734 /// // Deferred initialization:
735 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
736 ///
737 /// five.assume_init()
738 /// };
739 ///
740 /// assert_eq!(*five, 5)
741 /// ```
742 #[cfg(not(no_global_oom_handling))]
743 #[unstable(feature = "allocator_api", issue = "32838")]
744 // #[unstable(feature = "new_uninit", issue = "63291")]
745 #[inline]
746 pub fn new_uninit_in(alloc: A) -> Arc<mem::MaybeUninit<T>, A> {
747 unsafe {
748 Arc::from_ptr_in(
749 Arc::allocate_for_layout(
750 Layout::new::<T>(),
751 |layout| alloc.allocate(layout),
752 <*mut u8>::cast,
753 ),
754 alloc,
755 )
756 }
757 }
758
759 /// Constructs a new `Arc` with uninitialized contents, with the memory
760 /// being filled with `0` bytes, in the provided allocator.
761 ///
762 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
763 /// of this method.
764 ///
765 /// # Examples
766 ///
767 /// ```
768 /// #![feature(new_uninit)]
769 /// #![feature(allocator_api)]
770 ///
771 /// use std::sync::Arc;
772 /// use std::alloc::System;
773 ///
774 /// let zero = Arc::<u32, _>::new_zeroed_in(System);
775 /// let zero = unsafe { zero.assume_init() };
776 ///
777 /// assert_eq!(*zero, 0)
778 /// ```
779 ///
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")]
784 #[inline]
785 pub fn new_zeroed_in(alloc: A) -> Arc<mem::MaybeUninit<T>, A> {
786 unsafe {
787 Arc::from_ptr_in(
788 Arc::allocate_for_layout(
789 Layout::new::<T>(),
790 |layout| alloc.allocate_zeroed(layout),
791 <*mut u8>::cast,
792 ),
793 alloc,
794 )
795 }
796 }
797
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")]
802 #[inline]
803 pub fn pin_in(data: T, alloc: A) -> Pin<Arc<T, A>> {
804 unsafe { Pin::new_unchecked(Arc::new_in(data, alloc)) }
805 }
806
807 /// Constructs a new `Pin<Arc<T, A>>` in the provided allocator, return an error if allocation
808 /// fails.
809 #[inline]
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)?)) }
813 }
814
815 /// Constructs a new `Arc<T, A>` in the provided allocator, returning an error if allocation fails.
816 ///
817 /// # Examples
818 ///
819 /// ```
820 /// #![feature(allocator_api)]
821 ///
822 /// use std::sync::Arc;
823 /// use std::alloc::System;
824 ///
825 /// let five = Arc::try_new_in(5, System)?;
826 /// # Ok::<(), std::alloc::AllocError>(())
827 /// ```
828 #[inline]
829 #[unstable(feature = "allocator_api", issue = "32838")]
830 #[inline]
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(
835 ArcInner {
836 strong: atomic::AtomicUsize::new(1),
837 weak: atomic::AtomicUsize::new(1),
838 data,
839 },
840 alloc,
841 )?;
842 let (ptr, alloc) = Box::into_unique(x);
843 Ok(unsafe { Self::from_inner_in(ptr.into(), alloc) })
844 }
845
846 /// Constructs a new `Arc` with uninitialized contents, in the provided allocator, returning an
847 /// error if allocation fails.
848 ///
849 /// # Examples
850 ///
851 /// ```
852 /// #![feature(new_uninit, allocator_api)]
853 /// #![feature(get_mut_unchecked)]
854 ///
855 /// use std::sync::Arc;
856 /// use std::alloc::System;
857 ///
858 /// let mut five = Arc::<u32, _>::try_new_uninit_in(System)?;
859 ///
860 /// let five = unsafe {
861 /// // Deferred initialization:
862 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
863 ///
864 /// five.assume_init()
865 /// };
866 ///
867 /// assert_eq!(*five, 5);
868 /// # Ok::<(), std::alloc::AllocError>(())
869 /// ```
870 #[unstable(feature = "allocator_api", issue = "32838")]
871 // #[unstable(feature = "new_uninit", issue = "63291")]
872 #[inline]
873 pub fn try_new_uninit_in(alloc: A) -> Result<Arc<mem::MaybeUninit<T>, A>, AllocError> {
874 unsafe {
875 Ok(Arc::from_ptr_in(
876 Arc::try_allocate_for_layout(
877 Layout::new::<T>(),
878 |layout| alloc.allocate(layout),
879 <*mut u8>::cast,
880 )?,
881 alloc,
882 ))
883 }
884 }
885
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
888 /// fails.
889 ///
890 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
891 /// of this method.
892 ///
893 /// # Examples
894 ///
895 /// ```
896 /// #![feature(new_uninit, allocator_api)]
897 ///
898 /// use std::sync::Arc;
899 /// use std::alloc::System;
900 ///
901 /// let zero = Arc::<u32, _>::try_new_zeroed_in(System)?;
902 /// let zero = unsafe { zero.assume_init() };
903 ///
904 /// assert_eq!(*zero, 0);
905 /// # Ok::<(), std::alloc::AllocError>(())
906 /// ```
907 ///
908 /// [zeroed]: mem::MaybeUninit::zeroed
909 #[unstable(feature = "allocator_api", issue = "32838")]
910 // #[unstable(feature = "new_uninit", issue = "63291")]
911 #[inline]
912 pub fn try_new_zeroed_in(alloc: A) -> Result<Arc<mem::MaybeUninit<T>, A>, AllocError> {
913 unsafe {
914 Ok(Arc::from_ptr_in(
915 Arc::try_allocate_for_layout(
916 Layout::new::<T>(),
917 |layout| alloc.allocate_zeroed(layout),
918 <*mut u8>::cast,
919 )?,
920 alloc,
921 ))
922 }
923 }
924 /// Returns the inner value, if the `Arc` has exactly one strong reference.
925 ///
926 /// Otherwise, an [`Err`] is returned with the same `Arc` that was
927 /// passed in.
928 ///
929 /// This will succeed even if there are outstanding weak references.
930 ///
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.
941 ///
942 /// # Examples
943 ///
944 /// ```
945 /// use std::sync::Arc;
946 ///
947 /// let x = Arc::new(3);
948 /// assert_eq!(Arc::try_unwrap(x), Ok(3));
949 ///
950 /// let x = Arc::new(4);
951 /// let _y = Arc::clone(&x);
952 /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
953 /// ```
954 #[inline]
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() {
958 return Err(this);
959 }
960
961 acquire!(this.inner().strong);
962
963 unsafe {
964 let elem = ptr::read(&this.ptr.as_ref().data);
965 let alloc = ptr::read(&this.alloc); // copy the allocator
966
967 // Make a weak pointer to clean up the implicit strong-weak reference
968 let _weak = Weak { ptr: this.ptr, alloc };
969 mem::forget(this);
970
971 Ok(elem)
972 }
973 }
974
975 /// Returns the inner value, if the `Arc` has exactly one strong reference.
976 ///
977 /// Otherwise, [`None`] is returned and the `Arc` is dropped.
978 ///
979 /// This will succeed even if there are outstanding weak references.
980 ///
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.
984 ///
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`].
988 ///
989 /// # Examples
990 ///
991 /// Minimal example demonstrating the guarantee that `Arc::into_inner` gives.
992 /// ```
993 /// use std::sync::Arc;
994 ///
995 /// let x = Arc::new(3);
996 /// let y = Arc::clone(&x);
997 ///
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));
1001 ///
1002 /// let x_inner_value = x_thread.join().unwrap();
1003 /// let y_inner_value = y_thread.join().unwrap();
1004 ///
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)
1009 /// ));
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.
1012 /// ```
1013 ///
1014 /// A more practical example demonstrating the need for `Arc::into_inner`:
1015 /// ```
1016 /// use std::sync::Arc;
1017 ///
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>>>);
1022 ///
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) {
1031 /// link = next;
1032 /// }
1033 /// }
1034 /// }
1035 /// }
1036 ///
1037 /// // Implementation of `new` and `push` omitted
1038 /// impl<T> LinkedList<T> {
1039 /// /* ... */
1040 /// # fn new() -> Self {
1041 /// # LinkedList(None)
1042 /// # }
1043 /// # fn push(&mut self, x: T) {
1044 /// # self.0 = Some(Arc::new(Node(x, self.0.take())));
1045 /// # }
1046 /// }
1047 ///
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)`.
1051 ///
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
1056 /// }
1057 /// let y = x.clone();
1058 ///
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();
1064 /// ```
1065 #[inline]
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);
1070
1071 // Following the implementation of `drop` and `drop_slow`
1072 if this.inner().strong.fetch_sub(1, Release) != 1 {
1073 return None;
1074 }
1075
1076 acquire!(this.inner().strong);
1077
1078 // SAFETY: This mirrors the line
1079 //
1080 // unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) };
1081 //
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`.
1085
1086 let inner = unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) };
1087 let alloc = unsafe { ptr::read(&this.alloc) };
1088
1089 drop(Weak { ptr: this.ptr, alloc });
1090
1091 Some(inner)
1092 }
1093 }
1094
1095 impl<T> Arc<[T]> {
1096 /// Constructs a new atomically reference-counted slice with uninitialized contents.
1097 ///
1098 /// # Examples
1099 ///
1100 /// ```
1101 /// #![feature(new_uninit)]
1102 /// #![feature(get_mut_unchecked)]
1103 ///
1104 /// use std::sync::Arc;
1105 ///
1106 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
1107 ///
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);
1113 ///
1114 /// let values = unsafe { values.assume_init() };
1115 ///
1116 /// assert_eq!(*values, [1, 2, 3])
1117 /// ```
1118 #[cfg(not(no_global_oom_handling))]
1119 #[inline]
1120 #[unstable(feature = "new_uninit", issue = "63291")]
1121 #[must_use]
1122 pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
1123 unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) }
1124 }
1125
1126 /// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
1127 /// filled with `0` bytes.
1128 ///
1129 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1130 /// incorrect usage of this method.
1131 ///
1132 /// # Examples
1133 ///
1134 /// ```
1135 /// #![feature(new_uninit)]
1136 ///
1137 /// use std::sync::Arc;
1138 ///
1139 /// let values = Arc::<[u32]>::new_zeroed_slice(3);
1140 /// let values = unsafe { values.assume_init() };
1141 ///
1142 /// assert_eq!(*values, [0, 0, 0])
1143 /// ```
1144 ///
1145 /// [zeroed]: mem::MaybeUninit::zeroed
1146 #[cfg(not(no_global_oom_handling))]
1147 #[inline]
1148 #[unstable(feature = "new_uninit", issue = "63291")]
1149 #[must_use]
1150 pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
1151 unsafe {
1152 Arc::from_ptr(Arc::allocate_for_layout(
1153 Layout::array::<T>(len).unwrap(),
1154 |layout| Global.allocate_zeroed(layout),
1155 |mem| {
1156 ptr::slice_from_raw_parts_mut(mem as *mut T, len)
1157 as *mut ArcInner<[mem::MaybeUninit<T>]>
1158 },
1159 ))
1160 }
1161 }
1162 }
1163
1164 impl<T, A: Allocator> Arc<[T], A> {
1165 /// Constructs a new atomically reference-counted slice with uninitialized contents in the
1166 /// provided allocator.
1167 ///
1168 /// # Examples
1169 ///
1170 /// ```
1171 /// #![feature(new_uninit)]
1172 /// #![feature(get_mut_unchecked)]
1173 /// #![feature(allocator_api)]
1174 ///
1175 /// use std::sync::Arc;
1176 /// use std::alloc::System;
1177 ///
1178 /// let mut values = Arc::<[u32], _>::new_uninit_slice_in(3, System);
1179 ///
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);
1185 ///
1186 /// values.assume_init()
1187 /// };
1188 ///
1189 /// assert_eq!(*values, [1, 2, 3])
1190 /// ```
1191 #[cfg(not(no_global_oom_handling))]
1192 #[unstable(feature = "new_uninit", issue = "63291")]
1193 #[inline]
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) }
1196 }
1197
1198 /// Constructs a new atomically reference-counted slice with uninitialized contents, with the memory being
1199 /// filled with `0` bytes, in the provided allocator.
1200 ///
1201 /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
1202 /// incorrect usage of this method.
1203 ///
1204 /// # Examples
1205 ///
1206 /// ```
1207 /// #![feature(new_uninit)]
1208 /// #![feature(allocator_api)]
1209 ///
1210 /// use std::sync::Arc;
1211 /// use std::alloc::System;
1212 ///
1213 /// let values = Arc::<[u32], _>::new_zeroed_slice_in(3, System);
1214 /// let values = unsafe { values.assume_init() };
1215 ///
1216 /// assert_eq!(*values, [0, 0, 0])
1217 /// ```
1218 ///
1219 /// [zeroed]: mem::MaybeUninit::zeroed
1220 #[cfg(not(no_global_oom_handling))]
1221 #[unstable(feature = "new_uninit", issue = "63291")]
1222 #[inline]
1223 pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Arc<[mem::MaybeUninit<T>], A> {
1224 unsafe {
1225 Arc::from_ptr_in(
1226 Arc::allocate_for_layout(
1227 Layout::array::<T>(len).unwrap(),
1228 |layout| alloc.allocate_zeroed(layout),
1229 |mem| {
1230 ptr::slice_from_raw_parts_mut(mem.cast::<T>(), len)
1231 as *mut ArcInner<[mem::MaybeUninit<T>]>
1232 },
1233 ),
1234 alloc,
1235 )
1236 }
1237 }
1238 }
1239
1240 impl<T, A: Allocator> Arc<mem::MaybeUninit<T>, A> {
1241 /// Converts to `Arc<T>`.
1242 ///
1243 /// # Safety
1244 ///
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.
1250 ///
1251 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1252 ///
1253 /// # Examples
1254 ///
1255 /// ```
1256 /// #![feature(new_uninit)]
1257 /// #![feature(get_mut_unchecked)]
1258 ///
1259 /// use std::sync::Arc;
1260 ///
1261 /// let mut five = Arc::<u32>::new_uninit();
1262 ///
1263 /// // Deferred initialization:
1264 /// Arc::get_mut(&mut five).unwrap().write(5);
1265 ///
1266 /// let five = unsafe { five.assume_init() };
1267 ///
1268 /// assert_eq!(*five, 5)
1269 /// ```
1270 #[unstable(feature = "new_uninit", issue = "63291")]
1271 #[must_use = "`self` will be dropped if the result is not used"]
1272 #[inline]
1273 pub unsafe fn assume_init(self) -> Arc<T, A>
1274 where
1275 A: Clone,
1276 {
1277 let md_self = mem::ManuallyDrop::new(self);
1278 unsafe { Arc::from_inner_in(md_self.ptr.cast(), md_self.alloc.clone()) }
1279 }
1280 }
1281
1282 impl<T, A: Allocator> Arc<[mem::MaybeUninit<T>], A> {
1283 /// Converts to `Arc<[T]>`.
1284 ///
1285 /// # Safety
1286 ///
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.
1292 ///
1293 /// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
1294 ///
1295 /// # Examples
1296 ///
1297 /// ```
1298 /// #![feature(new_uninit)]
1299 /// #![feature(get_mut_unchecked)]
1300 ///
1301 /// use std::sync::Arc;
1302 ///
1303 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
1304 ///
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);
1310 ///
1311 /// let values = unsafe { values.assume_init() };
1312 ///
1313 /// assert_eq!(*values, [1, 2, 3])
1314 /// ```
1315 #[unstable(feature = "new_uninit", issue = "63291")]
1316 #[must_use = "`self` will be dropped if the result is not used"]
1317 #[inline]
1318 pub unsafe fn assume_init(self) -> Arc<[T], A>
1319 where
1320 A: Clone,
1321 {
1322 let md_self = mem::ManuallyDrop::new(self);
1323 unsafe { Arc::from_ptr_in(md_self.ptr.as_ptr() as _, md_self.alloc.clone()) }
1324 }
1325 }
1326
1327 impl<T: ?Sized> Arc<T> {
1328 /// Constructs an `Arc<T>` from a raw pointer.
1329 ///
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.
1337 ///
1338 /// The user of `from_raw` has to make sure a specific value of `T` is only
1339 /// dropped once.
1340 ///
1341 /// This function is unsafe because improper use may lead to memory unsafety,
1342 /// even if the returned `Arc<T>` is never accessed.
1343 ///
1344 /// [into_raw]: Arc::into_raw
1345 /// [transmute]: core::mem::transmute
1346 ///
1347 /// # Examples
1348 ///
1349 /// ```
1350 /// use std::sync::Arc;
1351 ///
1352 /// let x = Arc::new("hello".to_owned());
1353 /// let x_ptr = Arc::into_raw(x);
1354 ///
1355 /// unsafe {
1356 /// // Convert back to an `Arc` to prevent leak.
1357 /// let x = Arc::from_raw(x_ptr);
1358 /// assert_eq!(&*x, "hello");
1359 ///
1360 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
1361 /// }
1362 ///
1363 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1364 /// ```
1365 #[inline]
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) }
1369 }
1370
1371 /// Increments the strong reference count on the `Arc<T>` associated with the
1372 /// provided pointer by one.
1373 ///
1374 /// # Safety
1375 ///
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.
1379 ///
1380 /// # Examples
1381 ///
1382 /// ```
1383 /// use std::sync::Arc;
1384 ///
1385 /// let five = Arc::new(5);
1386 ///
1387 /// unsafe {
1388 /// let ptr = Arc::into_raw(five);
1389 /// Arc::increment_strong_count(ptr);
1390 ///
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));
1395 /// }
1396 /// ```
1397 #[inline]
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) }
1401 }
1402
1403 /// Decrements the strong reference count on the `Arc<T>` associated with the
1404 /// provided pointer by one.
1405 ///
1406 /// # Safety
1407 ///
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
1412 /// released.
1413 ///
1414 /// # Examples
1415 ///
1416 /// ```
1417 /// use std::sync::Arc;
1418 ///
1419 /// let five = Arc::new(5);
1420 ///
1421 /// unsafe {
1422 /// let ptr = Arc::into_raw(five);
1423 /// Arc::increment_strong_count(ptr);
1424 ///
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));
1431 /// }
1432 /// ```
1433 #[inline]
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) }
1437 }
1438 }
1439
1440 impl<T: ?Sized, A: Allocator> Arc<T, A> {
1441 /// Consumes the `Arc`, returning the wrapped pointer.
1442 ///
1443 /// To avoid a memory leak the pointer must be converted back to an `Arc` using
1444 /// [`Arc::from_raw`].
1445 ///
1446 /// # Examples
1447 ///
1448 /// ```
1449 /// use std::sync::Arc;
1450 ///
1451 /// let x = Arc::new("hello".to_owned());
1452 /// let x_ptr = Arc::into_raw(x);
1453 /// assert_eq!(unsafe { &*x_ptr }, "hello");
1454 /// ```
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);
1460 mem::forget(this);
1461 ptr
1462 }
1463
1464 /// Provides a raw pointer to the data.
1465 ///
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`.
1468 ///
1469 /// # Examples
1470 ///
1471 /// ```
1472 /// use std::sync::Arc;
1473 ///
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");
1479 /// ```
1480 #[must_use]
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);
1485
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) }
1490 }
1491
1492 /// Constructs an `Arc<T, A>` from a raw pointer.
1493 ///
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.
1501 ///
1502 /// The raw pointer must point to a block of memory allocated by `alloc`
1503 ///
1504 /// The user of `from_raw` has to make sure a specific value of `T` is only
1505 /// dropped once.
1506 ///
1507 /// This function is unsafe because improper use may lead to memory unsafety,
1508 /// even if the returned `Arc<T>` is never accessed.
1509 ///
1510 /// [into_raw]: Arc::into_raw
1511 ///
1512 /// # Examples
1513 ///
1514 /// ```
1515 /// #![feature(allocator_api)]
1516 ///
1517 /// use std::sync::Arc;
1518 /// use std::alloc::System;
1519 ///
1520 /// let x = Arc::new_in("hello".to_owned(), System);
1521 /// let x_ptr = Arc::into_raw(x);
1522 ///
1523 /// unsafe {
1524 /// // Convert back to an `Arc` to prevent leak.
1525 /// let x = Arc::from_raw_in(x_ptr, System);
1526 /// assert_eq!(&*x, "hello");
1527 ///
1528 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
1529 /// }
1530 ///
1531 /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
1532 /// ```
1533 #[inline]
1534 #[unstable(feature = "allocator_api", issue = "32838")]
1535 pub unsafe fn from_raw_in(ptr: *const T, alloc: A) -> Self {
1536 unsafe {
1537 let offset = data_offset(ptr);
1538
1539 // Reverse the offset to find the original ArcInner.
1540 let arc_ptr = ptr.byte_sub(offset) as *mut ArcInner<T>;
1541
1542 Self::from_ptr_in(arc_ptr, alloc)
1543 }
1544 }
1545
1546 /// Creates a new [`Weak`] pointer to this allocation.
1547 ///
1548 /// # Examples
1549 ///
1550 /// ```
1551 /// use std::sync::Arc;
1552 ///
1553 /// let five = Arc::new(5);
1554 ///
1555 /// let weak_five = Arc::downgrade(&five);
1556 /// ```
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>
1561 where
1562 A: Clone,
1563 {
1564 // This Relaxed is OK because we're checking the value in the CAS
1565 // below.
1566 let mut cur = this.inner().weak.load(Relaxed);
1567
1568 loop {
1569 // check if the weak counter is currently "locked"; if so, spin.
1570 if cur == usize::MAX {
1571 hint::spin_loop();
1572 cur = this.inner().weak.load(Relaxed);
1573 continue;
1574 }
1575
1576 // We can't allow the refcount to increase much past `MAX_REFCOUNT`.
1577 assert!(cur <= MAX_REFCOUNT, "{}", INTERNAL_OVERFLOW_ERROR);
1578
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.
1582
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) {
1587 Ok(_) => {
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() };
1591 }
1592 Err(old) => cur = old,
1593 }
1594 }
1595 }
1596
1597 /// Gets the number of [`Weak`] pointers to this allocation.
1598 ///
1599 /// # Safety
1600 ///
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.
1604 ///
1605 /// # Examples
1606 ///
1607 /// ```
1608 /// use std::sync::Arc;
1609 ///
1610 /// let five = Arc::new(5);
1611 /// let _weak_five = Arc::downgrade(&five);
1612 ///
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));
1616 /// ```
1617 #[inline]
1618 #[must_use]
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 }
1625 }
1626
1627 /// Gets the number of strong (`Arc`) pointers to this allocation.
1628 ///
1629 /// # Safety
1630 ///
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.
1634 ///
1635 /// # Examples
1636 ///
1637 /// ```
1638 /// use std::sync::Arc;
1639 ///
1640 /// let five = Arc::new(5);
1641 /// let _also_five = Arc::clone(&five);
1642 ///
1643 /// // This assertion is deterministic because we haven't shared
1644 /// // the `Arc` between threads.
1645 /// assert_eq!(2, Arc::strong_count(&five));
1646 /// ```
1647 #[inline]
1648 #[must_use]
1649 #[stable(feature = "arc_counts", since = "1.15.0")]
1650 pub fn strong_count(this: &Self) -> usize {
1651 this.inner().strong.load(Relaxed)
1652 }
1653
1654 /// Increments the strong reference count on the `Arc<T>` associated with the
1655 /// provided pointer by one.
1656 ///
1657 /// # Safety
1658 ///
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`.
1663 ///
1664 /// # Examples
1665 ///
1666 /// ```
1667 /// #![feature(allocator_api)]
1668 ///
1669 /// use std::sync::Arc;
1670 /// use std::alloc::System;
1671 ///
1672 /// let five = Arc::new_in(5, System);
1673 ///
1674 /// unsafe {
1675 /// let ptr = Arc::into_raw(five);
1676 /// Arc::increment_strong_count_in(ptr, System);
1677 ///
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));
1682 /// }
1683 /// ```
1684 #[inline]
1685 #[unstable(feature = "allocator_api", issue = "32838")]
1686 pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
1687 where
1688 A: Clone,
1689 {
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();
1694 }
1695
1696 /// Decrements the strong reference count on the `Arc<T>` associated with the
1697 /// provided pointer by one.
1698 ///
1699 /// # Safety
1700 ///
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
1706 /// released.
1707 ///
1708 /// # Examples
1709 ///
1710 /// ```
1711 /// #![feature(allocator_api)]
1712 ///
1713 /// use std::sync::Arc;
1714 /// use std::alloc::System;
1715 ///
1716 /// let five = Arc::new_in(5, System);
1717 ///
1718 /// unsafe {
1719 /// let ptr = Arc::into_raw(five);
1720 /// Arc::increment_strong_count_in(ptr, System);
1721 ///
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));
1728 /// }
1729 /// ```
1730 #[inline]
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)) };
1734 }
1735
1736 #[inline]
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
1742 // contents.
1743 unsafe { self.ptr.as_ref() }
1744 }
1745
1746 // Non-inlined part of `drop`.
1747 #[inline(never)]
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)) };
1752
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 });
1758 }
1759
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.
1762 ///
1763 /// # Examples
1764 ///
1765 /// ```
1766 /// use std::sync::Arc;
1767 ///
1768 /// let five = Arc::new(5);
1769 /// let same_five = Arc::clone(&five);
1770 /// let other_five = Arc::new(5);
1771 ///
1772 /// assert!(Arc::ptr_eq(&five, &same_five));
1773 /// assert!(!Arc::ptr_eq(&five, &other_five));
1774 /// ```
1775 ///
1776 /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
1777 #[inline]
1778 #[must_use]
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 ()
1782 }
1783 }
1784
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.
1788 ///
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);
1798
1799 let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
1800
1801 unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) }
1802 }
1803
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.
1807 ///
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);
1816
1817 let ptr = allocate(layout)?;
1818
1819 let inner = unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) };
1820
1821 Ok(inner)
1822 }
1823
1824 unsafe fn initialize_arcinner(
1825 ptr: NonNull<[u8]>,
1826 layout: Layout,
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);
1831
1832 unsafe {
1833 ptr::write(&mut (*inner).strong, atomic::AtomicUsize::new(1));
1834 ptr::write(&mut (*inner).weak, atomic::AtomicUsize::new(1));
1835 }
1836
1837 inner
1838 }
1839 }
1840
1841 impl<T: ?Sized, A: Allocator> Arc<T, A> {
1842 /// Allocates an `ArcInner<T>` with sufficient space for an unsized inner value.
1843 #[inline]
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.
1847 unsafe {
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>),
1852 )
1853 }
1854 }
1855
1856 #[cfg(not(no_global_oom_handling))]
1857 fn from_box_in(src: Box<T, A>) -> Arc<T, A> {
1858 unsafe {
1859 let value_size = size_of_val(&*src);
1860 let ptr = Self::allocate_for_ptr_in(&*src, Box::allocator(&src));
1861
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,
1866 value_size,
1867 );
1868
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>);
1872 drop(src);
1873
1874 Self::from_ptr_in(ptr, alloc)
1875 }
1876 }
1877 }
1878
1879 impl<T> Arc<[T]> {
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]> {
1883 unsafe {
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]>,
1888 )
1889 }
1890 }
1891
1892 /// Copy elements from slice into newly allocated `Arc<[T]>`
1893 ///
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]> {
1897 unsafe {
1898 let ptr = Self::allocate_for_slice(v.len());
1899
1900 ptr::copy_nonoverlapping(v.as_ptr(), &mut (*ptr).data as *mut [T] as *mut T, v.len());
1901
1902 Self::from_ptr(ptr)
1903 }
1904 }
1905
1906 /// Constructs an `Arc<[T]>` from an iterator known to be of a certain size.
1907 ///
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.
1914 struct Guard<T> {
1915 mem: NonNull<u8>,
1916 elems: *mut T,
1917 layout: Layout,
1918 n_elems: usize,
1919 }
1920
1921 impl<T> Drop for Guard<T> {
1922 fn drop(&mut self) {
1923 unsafe {
1924 let slice = from_raw_parts_mut(self.elems, self.n_elems);
1925 ptr::drop_in_place(slice);
1926
1927 Global.deallocate(self.mem, self.layout);
1928 }
1929 }
1930 }
1931
1932 unsafe {
1933 let ptr = Self::allocate_for_slice(len);
1934
1935 let mem = ptr as *mut _ as *mut u8;
1936 let layout = Layout::for_value(&*ptr);
1937
1938 // Pointer to first element
1939 let elems = &mut (*ptr).data as *mut [T] as *mut T;
1940
1941 let mut guard = Guard { mem: NonNull::new_unchecked(mem), elems, layout, n_elems: 0 };
1942
1943 for (i, item) in iter.enumerate() {
1944 ptr::write(elems.add(i), item);
1945 guard.n_elems += 1;
1946 }
1947
1948 // All clear. Forget the guard so it doesn't free the new ArcInner.
1949 mem::forget(guard);
1950
1951 Self::from_ptr(ptr)
1952 }
1953 }
1954 }
1955
1956 impl<T, A: Allocator> Arc<[T], A> {
1957 /// Allocates an `ArcInner<[T]>` with the given length.
1958 #[inline]
1959 #[cfg(not(no_global_oom_handling))]
1960 unsafe fn allocate_for_slice_in(len: usize, alloc: &A) -> *mut ArcInner<[T]> {
1961 unsafe {
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]>,
1966 )
1967 }
1968 }
1969 }
1970
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;
1975 }
1976
1977 #[cfg(not(no_global_oom_handling))]
1978 impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
1979 #[inline]
1980 default fn from_slice(v: &[T]) -> Self {
1981 unsafe { Self::from_iter_exact(v.iter().cloned(), v.len()) }
1982 }
1983 }
1984
1985 #[cfg(not(no_global_oom_handling))]
1986 impl<T: Copy> ArcFromSlice<T> for Arc<[T]> {
1987 #[inline]
1988 fn from_slice(v: &[T]) -> Self {
1989 unsafe { Arc::copy_from_slice(v) }
1990 }
1991 }
1992
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.
1996 ///
1997 /// This creates another pointer to the same allocation, increasing the
1998 /// strong reference count.
1999 ///
2000 /// # Examples
2001 ///
2002 /// ```
2003 /// use std::sync::Arc;
2004 ///
2005 /// let five = Arc::new(5);
2006 ///
2007 /// let _ = Arc::clone(&five);
2008 /// ```
2009 #[inline]
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
2013 // the object.
2014 //
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.
2020 //
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);
2023
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.
2028 //
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.
2034 //
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 {
2040 abort();
2041 }
2042
2043 unsafe { Self::from_inner_in(self.ptr, self.alloc.clone()) }
2044 }
2045 }
2046
2047 #[stable(feature = "rust1", since = "1.0.0")]
2048 impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
2049 type Target = T;
2050
2051 #[inline]
2052 fn deref(&self) -> &T {
2053 &self.inner().data
2054 }
2055 }
2056
2057 #[unstable(feature = "receiver_trait", issue = "none")]
2058 impl<T: ?Sized> Receiver for Arc<T> {}
2059
2060 impl<T: Clone, A: Allocator + Clone> Arc<T, A> {
2061 /// Makes a mutable reference into the given `Arc`.
2062 ///
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.
2066 ///
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
2069 /// be cloned.
2070 ///
2071 /// See also [`get_mut`], which will fail rather than cloning the inner value
2072 /// or dissociating [`Weak`] pointers.
2073 ///
2074 /// [`clone`]: Clone::clone
2075 /// [`get_mut`]: Arc::get_mut
2076 ///
2077 /// # Examples
2078 ///
2079 /// ```
2080 /// use std::sync::Arc;
2081 ///
2082 /// let mut data = Arc::new(5);
2083 ///
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
2089 ///
2090 /// // Now `data` and `other_data` point to different allocations.
2091 /// assert_eq!(*data, 8);
2092 /// assert_eq!(*other_data, 12);
2093 /// ```
2094 ///
2095 /// [`Weak`] pointers will be dissociated:
2096 ///
2097 /// ```
2098 /// use std::sync::Arc;
2099 ///
2100 /// let mut data = Arc::new(75);
2101 /// let weak = Arc::downgrade(&data);
2102 ///
2103 /// assert!(75 == *data);
2104 /// assert!(75 == *weak.upgrade().unwrap());
2105 ///
2106 /// *Arc::make_mut(&mut data) += 1;
2107 ///
2108 /// assert!(76 == *data);
2109 /// assert!(weak.upgrade().is_none());
2110 /// ```
2111 #[cfg(not(no_global_oom_handling))]
2112 #[inline]
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.
2118 //
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
2122 // deallocated.
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());
2127 unsafe {
2128 let data = Arc::get_mut_unchecked(&mut arc);
2129 (**this).write_clone_into_raw(data.as_mut_ptr());
2130 *this = arc.assume_init();
2131 }
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.
2136
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.
2140
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.
2144
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() };
2148
2149 // Can just steal the data, all that's left is Weaks
2150 let mut arc = Self::new_uninit_in(this.alloc.clone());
2151 unsafe {
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());
2155 }
2156 } else {
2157 // We were the sole reference of either kind; bump back up the
2158 // strong ref count.
2159 this.inner().strong.store(1, Release);
2160 }
2161
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) }
2165 }
2166
2167 /// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
2168 /// clone.
2169 ///
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.
2172 ///
2173 /// # Examples
2174 ///
2175 /// ```
2176 /// #![feature(arc_unwrap_or_clone)]
2177 /// # use std::{ptr, sync::Arc};
2178 /// let inner = String::from("test");
2179 /// let ptr = inner.as_ptr();
2180 ///
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()));
2185 ///
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()));
2195 /// ```
2196 #[inline]
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())
2200 }
2201 }
2202
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.
2206 ///
2207 /// Returns [`None`] otherwise, because it is not safe to
2208 /// mutate a shared value.
2209 ///
2210 /// See also [`make_mut`][make_mut], which will [`clone`][clone]
2211 /// the inner value when there are other `Arc` pointers.
2212 ///
2213 /// [make_mut]: Arc::make_mut
2214 /// [clone]: Clone::clone
2215 ///
2216 /// # Examples
2217 ///
2218 /// ```
2219 /// use std::sync::Arc;
2220 ///
2221 /// let mut x = Arc::new(3);
2222 /// *Arc::get_mut(&mut x).unwrap() = 4;
2223 /// assert_eq!(*x, 4);
2224 ///
2225 /// let _y = Arc::clone(&x);
2226 /// assert!(Arc::get_mut(&mut x).is_none());
2227 /// ```
2228 #[inline]
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)) }
2238 } else {
2239 None
2240 }
2241 }
2242
2243 /// Returns a mutable reference into the given `Arc`,
2244 /// without any check.
2245 ///
2246 /// See also [`get_mut`], which is safe and does appropriate checks.
2247 ///
2248 /// [`get_mut`]: Arc::get_mut
2249 ///
2250 /// # Safety
2251 ///
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`.
2257 ///
2258 /// # Examples
2259 ///
2260 /// ```
2261 /// #![feature(get_mut_unchecked)]
2262 ///
2263 /// use std::sync::Arc;
2264 ///
2265 /// let mut x = Arc::new(String::new());
2266 /// unsafe {
2267 /// Arc::get_mut_unchecked(&mut x).push_str("foo")
2268 /// }
2269 /// assert_eq!(*x, "foo");
2270 /// ```
2271 /// Other `Arc` pointers to the same allocation must be to the same type.
2272 /// ```no_run
2273 /// #![feature(get_mut_unchecked)]
2274 ///
2275 /// use std::sync::Arc;
2276 ///
2277 /// let x: Arc<str> = Arc::from("Hello, world!");
2278 /// let mut y: Arc<[u8]> = x.clone().into();
2279 /// unsafe {
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
2282 /// }
2283 /// println!("{}", &*x); // Invalid UTF-8 in a str
2284 /// ```
2285 /// Other `Arc` pointers to the same allocation must be to the exact same type, including lifetimes.
2286 /// ```no_run
2287 /// #![feature(get_mut_unchecked)]
2288 ///
2289 /// use std::sync::Arc;
2290 ///
2291 /// let x: Arc<&str> = Arc::new("Hello, world!");
2292 /// {
2293 /// let s = String::from("Oh, no!");
2294 /// let mut y: Arc<&str> = x.clone().into();
2295 /// unsafe {
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;
2299 /// }
2300 /// }
2301 /// println!("{}", &*x); // Use-after-free
2302 /// ```
2303 #[inline]
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 }
2309 }
2310
2311 /// Determine whether this is the unique reference (including weak refs) to
2312 /// the underlying data.
2313 ///
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
2317 // holder.
2318 //
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;
2328
2329 // The release write here synchronizes with a read in `downgrade`,
2330 // effectively preventing the above read of `strong` from happening
2331 // after the write.
2332 self.inner().weak.store(1, Release); // release the lock
2333 unique
2334 } else {
2335 false
2336 }
2337 }
2338 }
2339
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`.
2343 ///
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.
2347 ///
2348 /// # Examples
2349 ///
2350 /// ```
2351 /// use std::sync::Arc;
2352 ///
2353 /// struct Foo;
2354 ///
2355 /// impl Drop for Foo {
2356 /// fn drop(&mut self) {
2357 /// println!("dropped!");
2358 /// }
2359 /// }
2360 ///
2361 /// let foo = Arc::new(Foo);
2362 /// let foo2 = Arc::clone(&foo);
2363 ///
2364 /// drop(foo); // Doesn't print anything
2365 /// drop(foo2); // Prints "dropped!"
2366 /// ```
2367 #[inline]
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 {
2373 return;
2374 }
2375
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.
2382 //
2383 // As explained in the [Boost documentation][1],
2384 //
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.
2391 //
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.
2397 //
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].
2401 //
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);
2405
2406 unsafe {
2407 self.drop_slow();
2408 }
2409 }
2410 }
2411
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.
2414 ///
2415 /// # Examples
2416 ///
2417 /// ```
2418 /// use std::any::Any;
2419 /// use std::sync::Arc;
2420 ///
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);
2424 /// }
2425 /// }
2426 ///
2427 /// let my_string = "Hello World".to_string();
2428 /// print_if_string(Arc::new(my_string));
2429 /// print_if_string(Arc::new(0i8));
2430 /// ```
2431 #[inline]
2432 #[stable(feature = "rc_downcast", since = "1.29.0")]
2433 pub fn downcast<T>(self) -> Result<Arc<T, A>, Self>
2434 where
2435 T: Any + Send + Sync,
2436 {
2437 if (*self).is::<T>() {
2438 unsafe {
2439 let ptr = self.ptr.cast::<ArcInner<T>>();
2440 let alloc = self.alloc.clone();
2441 mem::forget(self);
2442 Ok(Arc::from_inner_in(ptr, alloc))
2443 }
2444 } else {
2445 Err(self)
2446 }
2447 }
2448
2449 /// Downcasts the `Arc<dyn Any + Send + Sync>` to a concrete type.
2450 ///
2451 /// For a safe alternative see [`downcast`].
2452 ///
2453 /// # Examples
2454 ///
2455 /// ```
2456 /// #![feature(downcast_unchecked)]
2457 ///
2458 /// use std::any::Any;
2459 /// use std::sync::Arc;
2460 ///
2461 /// let x: Arc<dyn Any + Send + Sync> = Arc::new(1_usize);
2462 ///
2463 /// unsafe {
2464 /// assert_eq!(*x.downcast_unchecked::<usize>(), 1);
2465 /// }
2466 /// ```
2467 ///
2468 /// # Safety
2469 ///
2470 /// The contained value must be of type `T`. Calling this method
2471 /// with the incorrect type is *undefined behavior*.
2472 ///
2473 ///
2474 /// [`downcast`]: Self::downcast
2475 #[inline]
2476 #[unstable(feature = "downcast_unchecked", issue = "90850")]
2477 pub unsafe fn downcast_unchecked<T>(self) -> Arc<T, A>
2478 where
2479 T: Any + Send + Sync,
2480 {
2481 unsafe {
2482 let ptr = self.ptr.cast::<ArcInner<T>>();
2483 let alloc = self.alloc.clone();
2484 mem::forget(self);
2485 Arc::from_inner_in(ptr, alloc)
2486 }
2487 }
2488 }
2489
2490 impl<T> Weak<T> {
2491 /// Constructs a new `Weak<T>`, without allocating any memory.
2492 /// Calling [`upgrade`] on the return value always gives [`None`].
2493 ///
2494 /// [`upgrade`]: Weak::upgrade
2495 ///
2496 /// # Examples
2497 ///
2498 /// ```
2499 /// use std::sync::Weak;
2500 ///
2501 /// let empty: Weak<i64> = Weak::new();
2502 /// assert!(empty.upgrade().is_none());
2503 /// ```
2504 #[inline]
2505 #[stable(feature = "downgraded_weak", since = "1.10.0")]
2506 #[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
2507 #[must_use]
2508 pub const fn new() -> Weak<T> {
2509 Weak {
2510 ptr: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<ArcInner<T>>(usize::MAX)) },
2511 alloc: Global,
2512 }
2513 }
2514 }
2515
2516 impl<T, A: Allocator> Weak<T, A> {
2517 /// Constructs a new `Weak<T, A>`, without allocating any memory, technically in the provided
2518 /// allocator.
2519 /// Calling [`upgrade`] on the return value always gives [`None`].
2520 ///
2521 /// [`upgrade`]: Weak::upgrade
2522 ///
2523 /// # Examples
2524 ///
2525 /// ```
2526 /// #![feature(allocator_api)]
2527 ///
2528 /// use std::sync::Weak;
2529 /// use std::alloc::System;
2530 ///
2531 /// let empty: Weak<i64, _> = Weak::new_in(System);
2532 /// assert!(empty.upgrade().is_none());
2533 /// ```
2534 #[inline]
2535 #[unstable(feature = "allocator_api", issue = "32838")]
2536 pub fn new_in(alloc: A) -> Weak<T, A> {
2537 Weak {
2538 ptr: unsafe { NonNull::new_unchecked(ptr::invalid_mut::<ArcInner<T>>(usize::MAX)) },
2539 alloc,
2540 }
2541 }
2542 }
2543
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,
2549 }
2550
2551 impl<T: ?Sized> Weak<T> {
2552 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
2553 ///
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>`.
2556 ///
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).
2559 ///
2560 /// # Safety
2561 ///
2562 /// The pointer must have originated from the [`into_raw`] and must still own its potential
2563 /// weak reference.
2564 ///
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`].
2569 /// # Examples
2570 ///
2571 /// ```
2572 /// use std::sync::{Arc, Weak};
2573 ///
2574 /// let strong = Arc::new("hello".to_owned());
2575 ///
2576 /// let raw_1 = Arc::downgrade(&strong).into_raw();
2577 /// let raw_2 = Arc::downgrade(&strong).into_raw();
2578 ///
2579 /// assert_eq!(2, Arc::weak_count(&strong));
2580 ///
2581 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
2582 /// assert_eq!(1, Arc::weak_count(&strong));
2583 ///
2584 /// drop(strong);
2585 ///
2586 /// // Decrement the last weak count.
2587 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
2588 /// ```
2589 ///
2590 /// [`new`]: Weak::new
2591 /// [`into_raw`]: Weak::into_raw
2592 /// [`upgrade`]: Weak::upgrade
2593 #[inline]
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) }
2597 }
2598 }
2599
2600 impl<T: ?Sized, A: Allocator> Weak<T, A> {
2601 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
2602 ///
2603 /// The pointer is valid only if there are some strong references. The pointer may be dangling,
2604 /// unaligned or even [`null`] otherwise.
2605 ///
2606 /// # Examples
2607 ///
2608 /// ```
2609 /// use std::sync::Arc;
2610 /// use std::ptr;
2611 ///
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() });
2618 ///
2619 /// drop(strong);
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() });
2623 /// ```
2624 ///
2625 /// [`null`]: core::ptr::null "ptr::null"
2626 #[must_use]
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);
2630
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).
2634 ptr as *const T
2635 } else {
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) }
2640 }
2641 }
2642
2643 /// Consumes the `Weak<T>` and turns it into a raw pointer.
2644 ///
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`].
2648 ///
2649 /// The same restrictions of accessing the target of the pointer as with
2650 /// [`as_ptr`] apply.
2651 ///
2652 /// # Examples
2653 ///
2654 /// ```
2655 /// use std::sync::{Arc, Weak};
2656 ///
2657 /// let strong = Arc::new("hello".to_owned());
2658 /// let weak = Arc::downgrade(&strong);
2659 /// let raw = weak.into_raw();
2660 ///
2661 /// assert_eq!(1, Arc::weak_count(&strong));
2662 /// assert_eq!("hello", unsafe { &*raw });
2663 ///
2664 /// drop(unsafe { Weak::from_raw(raw) });
2665 /// assert_eq!(0, Arc::weak_count(&strong));
2666 /// ```
2667 ///
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();
2674 mem::forget(self);
2675 result
2676 }
2677
2678 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>` in the provided
2679 /// allocator.
2680 ///
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>`.
2683 ///
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).
2686 ///
2687 /// # Safety
2688 ///
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`.
2691 ///
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`].
2696 /// # Examples
2697 ///
2698 /// ```
2699 /// use std::sync::{Arc, Weak};
2700 ///
2701 /// let strong = Arc::new("hello".to_owned());
2702 ///
2703 /// let raw_1 = Arc::downgrade(&strong).into_raw();
2704 /// let raw_2 = Arc::downgrade(&strong).into_raw();
2705 ///
2706 /// assert_eq!(2, Arc::weak_count(&strong));
2707 ///
2708 /// assert_eq!("hello", &*unsafe { Weak::from_raw(raw_1) }.upgrade().unwrap());
2709 /// assert_eq!(1, Arc::weak_count(&strong));
2710 ///
2711 /// drop(strong);
2712 ///
2713 /// // Decrement the last weak count.
2714 /// assert!(unsafe { Weak::from_raw(raw_2) }.upgrade().is_none());
2715 /// ```
2716 ///
2717 /// [`new`]: Weak::new
2718 /// [`into_raw`]: Weak::into_raw
2719 /// [`upgrade`]: Weak::upgrade
2720 #[inline]
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.
2724
2725 let ptr = if is_dangling(ptr as *mut T) {
2726 // This is a dangling Weak.
2727 ptr as *mut ArcInner<T>
2728 } else {
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> }
2735 };
2736
2737 // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
2738 Weak { ptr: unsafe { NonNull::new_unchecked(ptr) }, alloc }
2739 }
2740 }
2741
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.
2745 ///
2746 /// Returns [`None`] if the inner value has since been dropped.
2747 ///
2748 /// # Examples
2749 ///
2750 /// ```
2751 /// use std::sync::Arc;
2752 ///
2753 /// let five = Arc::new(5);
2754 ///
2755 /// let weak_five = Arc::downgrade(&five);
2756 ///
2757 /// let strong_five: Option<Arc<_>> = weak_five.upgrade();
2758 /// assert!(strong_five.is_some());
2759 ///
2760 /// // Destroy all strong pointers.
2761 /// drop(strong_five);
2762 /// drop(five);
2763 ///
2764 /// assert!(weak_five.upgrade().is_none());
2765 /// ```
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>>
2770 where
2771 A: Clone,
2772 {
2773 #[inline]
2774 fn checked_increment(n: usize) -> Option<usize> {
2775 // Any write of 0 we can observe leaves the field in permanently zero state.
2776 if n == 0 {
2777 return None;
2778 }
2779 // See comments in `Arc::clone` for why we do this (for `mem::forget`).
2780 assert!(n <= MAX_REFCOUNT, "{}", INTERNAL_OVERFLOW_ERROR);
2781 Some(n + 1)
2782 }
2783
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.
2787 //
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())) }
2795 } else {
2796 None
2797 }
2798 }
2799
2800 /// Gets the number of strong (`Arc`) pointers pointing to this allocation.
2801 ///
2802 /// If `self` was created using [`Weak::new`], this will return 0.
2803 #[must_use]
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 }
2807 }
2808
2809 /// Gets an approximation of the number of `Weak` pointers pointing to this
2810 /// allocation.
2811 ///
2812 /// If `self` was created using [`Weak::new`], or if there are no remaining
2813 /// strong pointers, this will return 0.
2814 ///
2815 /// # Accuracy
2816 ///
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.
2820 #[must_use]
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);
2826 if strong == 0 {
2827 0
2828 } else {
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.
2834 weak - 1
2835 }
2836 } else {
2837 0
2838 }
2839 }
2840
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`).
2843 #[inline]
2844 fn inner(&self) -> Option<WeakInner<'_>> {
2845 if is_dangling(self.ptr.as_ptr()) {
2846 None
2847 } else {
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).
2851 Some(unsafe {
2852 let ptr = self.ptr.as_ptr();
2853 WeakInner { strong: &(*ptr).strong, weak: &(*ptr).weak }
2854 })
2855 }
2856 }
2857
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.
2861 ///
2862 /// # Notes
2863 ///
2864 /// Since this compares pointers it means that `Weak::new()` will equal each
2865 /// other, even though they don't point to any allocation.
2866 ///
2867 /// # Examples
2868 ///
2869 /// ```
2870 /// use std::sync::Arc;
2871 ///
2872 /// let first_rc = Arc::new(5);
2873 /// let first = Arc::downgrade(&first_rc);
2874 /// let second = Arc::downgrade(&first_rc);
2875 ///
2876 /// assert!(first.ptr_eq(&second));
2877 ///
2878 /// let third_rc = Arc::new(5);
2879 /// let third = Arc::downgrade(&third_rc);
2880 ///
2881 /// assert!(!first.ptr_eq(&third));
2882 /// ```
2883 ///
2884 /// Comparing `Weak::new`.
2885 ///
2886 /// ```
2887 /// use std::sync::{Arc, Weak};
2888 ///
2889 /// let first = Weak::new();
2890 /// let second = Weak::new();
2891 /// assert!(first.ptr_eq(&second));
2892 ///
2893 /// let third_rc = Arc::new(());
2894 /// let third = Arc::downgrade(&third_rc);
2895 /// assert!(!first.ptr_eq(&third));
2896 /// ```
2897 ///
2898 /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
2899 #[inline]
2900 #[must_use]
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 ())
2904 }
2905 }
2906
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.
2910 ///
2911 /// # Examples
2912 ///
2913 /// ```
2914 /// use std::sync::{Arc, Weak};
2915 ///
2916 /// let weak_five = Arc::downgrade(&Arc::new(5));
2917 ///
2918 /// let _ = Weak::clone(&weak_five);
2919 /// ```
2920 #[inline]
2921 fn clone(&self) -> Weak<T, A> {
2922 let inner = if let Some(inner) = self.inner() {
2923 inner
2924 } else {
2925 return Weak { ptr: self.ptr, alloc: self.alloc.clone() };
2926 };
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);
2932
2933 // See comments in Arc::clone() for why we do this (for mem::forget).
2934 if old_size > MAX_REFCOUNT {
2935 abort();
2936 }
2937
2938 Weak { ptr: self.ptr, alloc: self.alloc.clone() }
2939 }
2940 }
2941
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
2946 /// gives [`None`].
2947 ///
2948 /// [`upgrade`]: Weak::upgrade
2949 ///
2950 /// # Examples
2951 ///
2952 /// ```
2953 /// use std::sync::Weak;
2954 ///
2955 /// let empty: Weak<i64> = Default::default();
2956 /// assert!(empty.upgrade().is_none());
2957 /// ```
2958 fn default() -> Weak<T> {
2959 Weak::new()
2960 }
2961 }
2962
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.
2966 ///
2967 /// # Examples
2968 ///
2969 /// ```
2970 /// use std::sync::{Arc, Weak};
2971 ///
2972 /// struct Foo;
2973 ///
2974 /// impl Drop for Foo {
2975 /// fn drop(&mut self) {
2976 /// println!("dropped!");
2977 /// }
2978 /// }
2979 ///
2980 /// let foo = Arc::new(Foo);
2981 /// let weak_foo = Arc::downgrade(&foo);
2982 /// let other_weak_foo = Weak::clone(&weak_foo);
2983 ///
2984 /// drop(weak_foo); // Doesn't print anything
2985 /// drop(foo); // Prints "dropped!"
2986 ///
2987 /// assert!(other_weak_foo.upgrade().is_none());
2988 /// ```
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
2993 //
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 };
2999
3000 if inner.weak.fetch_sub(1, Release) == 1 {
3001 acquire!(inner.weak);
3002 unsafe {
3003 self.alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()))
3004 }
3005 }
3006 }
3007 }
3008
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;
3013 }
3014
3015 #[stable(feature = "rust1", since = "1.0.0")]
3016 impl<T: ?Sized + PartialEq, A: Allocator> ArcEqIdent<T, A> for Arc<T, A> {
3017 #[inline]
3018 default fn eq(&self, other: &Arc<T, A>) -> bool {
3019 **self == **other
3020 }
3021 #[inline]
3022 default fn ne(&self, other: &Arc<T, A>) -> bool {
3023 **self != **other
3024 }
3025 }
3026
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.
3032 ///
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> {
3036 #[inline]
3037 fn eq(&self, other: &Arc<T, A>) -> bool {
3038 Arc::ptr_eq(self, other) || **self == **other
3039 }
3040
3041 #[inline]
3042 fn ne(&self, other: &Arc<T, A>) -> bool {
3043 !Arc::ptr_eq(self, other) && **self != **other
3044 }
3045 }
3046
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.
3050 ///
3051 /// Two `Arc`s are equal if their inner values are equal, even if they are
3052 /// stored in different allocation.
3053 ///
3054 /// If `T` also implements `Eq` (implying reflexivity of equality),
3055 /// two `Arc`s that point to the same allocation are always equal.
3056 ///
3057 /// # Examples
3058 ///
3059 /// ```
3060 /// use std::sync::Arc;
3061 ///
3062 /// let five = Arc::new(5);
3063 ///
3064 /// assert!(five == Arc::new(5));
3065 /// ```
3066 #[inline]
3067 fn eq(&self, other: &Arc<T, A>) -> bool {
3068 ArcEqIdent::eq(self, other)
3069 }
3070
3071 /// Inequality for two `Arc`s.
3072 ///
3073 /// Two `Arc`s are not equal if their inner values are not equal.
3074 ///
3075 /// If `T` also implements `Eq` (implying reflexivity of equality),
3076 /// two `Arc`s that point to the same value are always equal.
3077 ///
3078 /// # Examples
3079 ///
3080 /// ```
3081 /// use std::sync::Arc;
3082 ///
3083 /// let five = Arc::new(5);
3084 ///
3085 /// assert!(five != Arc::new(6));
3086 /// ```
3087 #[inline]
3088 fn ne(&self, other: &Arc<T, A>) -> bool {
3089 ArcEqIdent::ne(self, other)
3090 }
3091 }
3092
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.
3096 ///
3097 /// The two are compared by calling `partial_cmp()` on their inner values.
3098 ///
3099 /// # Examples
3100 ///
3101 /// ```
3102 /// use std::sync::Arc;
3103 /// use std::cmp::Ordering;
3104 ///
3105 /// let five = Arc::new(5);
3106 ///
3107 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6)));
3108 /// ```
3109 fn partial_cmp(&self, other: &Arc<T, A>) -> Option<Ordering> {
3110 (**self).partial_cmp(&**other)
3111 }
3112
3113 /// Less-than comparison for two `Arc`s.
3114 ///
3115 /// The two are compared by calling `<` on their inner values.
3116 ///
3117 /// # Examples
3118 ///
3119 /// ```
3120 /// use std::sync::Arc;
3121 ///
3122 /// let five = Arc::new(5);
3123 ///
3124 /// assert!(five < Arc::new(6));
3125 /// ```
3126 fn lt(&self, other: &Arc<T, A>) -> bool {
3127 *(*self) < *(*other)
3128 }
3129
3130 /// 'Less than or equal to' comparison for two `Arc`s.
3131 ///
3132 /// The two are compared by calling `<=` on their inner values.
3133 ///
3134 /// # Examples
3135 ///
3136 /// ```
3137 /// use std::sync::Arc;
3138 ///
3139 /// let five = Arc::new(5);
3140 ///
3141 /// assert!(five <= Arc::new(5));
3142 /// ```
3143 fn le(&self, other: &Arc<T, A>) -> bool {
3144 *(*self) <= *(*other)
3145 }
3146
3147 /// Greater-than comparison for two `Arc`s.
3148 ///
3149 /// The two are compared by calling `>` on their inner values.
3150 ///
3151 /// # Examples
3152 ///
3153 /// ```
3154 /// use std::sync::Arc;
3155 ///
3156 /// let five = Arc::new(5);
3157 ///
3158 /// assert!(five > Arc::new(4));
3159 /// ```
3160 fn gt(&self, other: &Arc<T, A>) -> bool {
3161 *(*self) > *(*other)
3162 }
3163
3164 /// 'Greater than or equal to' comparison for two `Arc`s.
3165 ///
3166 /// The two are compared by calling `>=` on their inner values.
3167 ///
3168 /// # Examples
3169 ///
3170 /// ```
3171 /// use std::sync::Arc;
3172 ///
3173 /// let five = Arc::new(5);
3174 ///
3175 /// assert!(five >= Arc::new(5));
3176 /// ```
3177 fn ge(&self, other: &Arc<T, A>) -> bool {
3178 *(*self) >= *(*other)
3179 }
3180 }
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.
3184 ///
3185 /// The two are compared by calling `cmp()` on their inner values.
3186 ///
3187 /// # Examples
3188 ///
3189 /// ```
3190 /// use std::sync::Arc;
3191 /// use std::cmp::Ordering;
3192 ///
3193 /// let five = Arc::new(5);
3194 ///
3195 /// assert_eq!(Ordering::Less, five.cmp(&Arc::new(6)));
3196 /// ```
3197 fn cmp(&self, other: &Arc<T, A>) -> Ordering {
3198 (**self).cmp(&**other)
3199 }
3200 }
3201 #[stable(feature = "rust1", since = "1.0.0")]
3202 impl<T: ?Sized + Eq, A: Allocator> Eq for Arc<T, A> {}
3203
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)
3208 }
3209 }
3210
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)
3215 }
3216 }
3217
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)
3222 }
3223 }
3224
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`.
3229 ///
3230 /// # Examples
3231 ///
3232 /// ```
3233 /// use std::sync::Arc;
3234 ///
3235 /// let x: Arc<i32> = Default::default();
3236 /// assert_eq!(*x, 0);
3237 /// ```
3238 fn default() -> Arc<T> {
3239 Arc::new(Default::default())
3240 }
3241 }
3242
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)
3247 }
3248 }
3249
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>`
3254 ///
3255 /// The conversion moves the value into a
3256 /// newly allocated `Arc`. It is equivalent to
3257 /// calling `Arc::new(t)`.
3258 ///
3259 /// # Example
3260 /// ```rust
3261 /// # use std::sync::Arc;
3262 /// let x = 5;
3263 /// let arc = Arc::new(5);
3264 ///
3265 /// assert_eq!(Arc::from(x), arc);
3266 /// ```
3267 fn from(t: T) -> Self {
3268 Arc::new(t)
3269 }
3270 }
3271
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]>`.
3276 ///
3277 /// The conversion moves the array into a newly allocated `Arc`.
3278 ///
3279 /// # Example
3280 ///
3281 /// ```
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[..]);
3286 /// ```
3287 #[inline]
3288 fn from(v: [T; N]) -> Arc<[T]> {
3289 Arc::<[T; N]>::from(v)
3290 }
3291 }
3292
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.
3297 ///
3298 /// # Example
3299 ///
3300 /// ```
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[..]);
3305 /// ```
3306 #[inline]
3307 fn from(v: &[T]) -> Arc<[T]> {
3308 <Self as ArcFromSlice<T>>::from_slice(v)
3309 }
3310 }
3311
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.
3316 ///
3317 /// # Example
3318 ///
3319 /// ```
3320 /// # use std::sync::Arc;
3321 /// let shared: Arc<str> = Arc::from("eggplant");
3322 /// assert_eq!("eggplant", &shared[..]);
3323 /// ```
3324 #[inline]
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) }
3328 }
3329 }
3330
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.
3335 ///
3336 /// # Example
3337 ///
3338 /// ```
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[..]);
3343 /// ```
3344 #[inline]
3345 fn from(v: String) -> Arc<str> {
3346 Arc::from(&v[..])
3347 }
3348 }
3349
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.
3354 ///
3355 /// # Example
3356 ///
3357 /// ```
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[..]);
3362 /// ```
3363 #[inline]
3364 fn from(v: Box<T, A>) -> Arc<T, A> {
3365 Arc::from_box_in(v)
3366 }
3367 }
3368
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.
3373 ///
3374 /// # Example
3375 ///
3376 /// ```
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[..]);
3381 /// ```
3382 #[inline]
3383 fn from(v: Vec<T, A>) -> Arc<[T], A> {
3384 unsafe {
3385 let (vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
3386
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);
3389
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);
3393
3394 Self::from_ptr_in(rc_ptr, alloc)
3395 }
3396 }
3397 }
3398
3399 #[stable(feature = "shared_from_cow", since = "1.45.0")]
3400 impl<'a, B> From<Cow<'a, B>> for Arc<B>
3401 where
3402 B: ToOwned + ?Sized,
3403 Arc<B>: From<&'a B> + From<B::Owned>,
3404 {
3405 /// Create an atomically reference-counted pointer from
3406 /// a clone-on-write pointer by copying its content.
3407 ///
3408 /// # Example
3409 ///
3410 /// ```rust
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[..]);
3416 /// ```
3417 #[inline]
3418 fn from(cow: Cow<'a, B>) -> Arc<B> {
3419 match cow {
3420 Cow::Borrowed(s) => Arc::from(s),
3421 Cow::Owned(s) => Arc::from(s),
3422 }
3423 }
3424 }
3425
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.
3429 ///
3430 /// # Example
3431 ///
3432 /// ```
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());
3437 /// ```
3438 #[inline]
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]) }
3442 }
3443 }
3444
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>;
3448
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) })
3453 } else {
3454 Err(boxed_slice)
3455 }
3456 }
3457 }
3458
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]>`.
3463 ///
3464 /// # Performance characteristics
3465 ///
3466 /// ## The general case
3467 ///
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:
3470 ///
3471 /// ```rust
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]);
3475 /// ```
3476 ///
3477 /// this behaves as if we wrote:
3478 ///
3479 /// ```rust
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]);
3485 /// ```
3486 ///
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]>`.
3489 ///
3490 /// ## Iterators of known length
3491 ///
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:
3494 ///
3495 /// ```rust
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<_>>());
3499 /// ```
3500 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3501 ToArcSlice::to_arc_slice(iter.into_iter())
3502 }
3503 }
3504
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]>;
3508 }
3509
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()
3514 }
3515 }
3516
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 {
3523 debug_assert_eq!(
3524 low,
3525 high,
3526 "TrustedLen iterator's size hint is not exact: {:?}",
3527 (low, high)
3528 );
3529
3530 unsafe {
3531 // SAFETY: We need to ensure that the iterator has an exact length and we have.
3532 Arc::from_iter_exact(self, low)
3533 }
3534 } else {
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");
3540 }
3541 }
3542 }
3543
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 {
3547 &**self
3548 }
3549 }
3550
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 {
3554 &**self
3555 }
3556 }
3557
3558 #[stable(feature = "pin", since = "1.33.0")]
3559 impl<T: ?Sized, A: Allocator> Unpin for Arc<T, A> {}
3560
3561 /// Get the offset within an `ArcInner` for the payload behind a pointer.
3562 ///
3563 /// # Safety
3564 ///
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)) }
3575 }
3576
3577 #[inline]
3578 fn data_offset_align(align: usize) -> usize {
3579 let layout = Layout::new::<ArcInner<()>>();
3580 layout.size() + layout.padding_needed_for(align)
3581 }
3582
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)
3588 }
3589
3590 #[allow(deprecated)]
3591 fn cause(&self) -> Option<&dyn core::error::Error> {
3592 core::error::Error::cause(&**self)
3593 }
3594
3595 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
3596 core::error::Error::source(&**self)
3597 }
3598
3599 fn provide<'a>(&'a self, req: &mut core::error::Request<'a>) {
3600 core::error::Error::provide(&**self, req);
3601 }
3602 }