Lines Matching refs:Arc

3302 use std::sync::{Arc, Weak};
3318 let a: Arc<[u32; 3]> = Arc::new([3, 2, 1]);
3319 let a: Arc<[u32]> = a; // Unsizing
3320 let b: Arc<[u32]> = Arc::from(&[3, 2, 1][..]); // Conversion
3324 let mut a = Arc::downgrade(&a);
3331 let a: Arc<u32> = Arc::new(4);
3332 let a: Arc<dyn Any> = a; // Unsizing
3335 let mut a = Arc::downgrade(&a);
3349 let x = Arc::new(f32::NAN);
3364 let x = Arc::new(TestPEq(RefCell::new(0)));
3381 let x = Arc::new(TestEq(RefCell::new(0)));
3389 type Rc<T> = Arc<T>;
6474 use std::sync::Arc;
6484 test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
10731 use std::sync::Arc;
10734 count: Arc<AtomicUsize>,
10754 let drop_count = Arc::new(AtomicUsize::new(0));
14402 /// Used by `Box::clone` and `Rc`/`Arc::make_mut`.
15588 let arc_v = Arc::new(v);
15593 let arc_v: Arc<Vec<i32>> = rx.recv().unwrap();
15605 let mut x = Arc::new(3);
15606 *Arc::get_mut(&mut x).unwrap() = 4;
15609 assert!(Arc::get_mut(&mut x).is_none());
15611 assert!(Arc::get_mut(&mut x).is_some());
15612 let _w = Arc::downgrade(&x);
15613 assert!(Arc::get_mut(&mut x).is_none());
15621 let a = Arc::new(0);
15622 let w = Arc::downgrade(&a);
15645 let x = Arc::new(3);
15646 assert_eq!(Arc::try_unwrap(x), Ok(3));
15647 let x = Arc::new(4);
15649 assert_eq!(Arc::try_unwrap(x), Err(Arc::new(4)));
15650 let x = Arc::new(5);
15651 let _w = Arc::downgrade(&x);
15652 assert_eq!(Arc::try_unwrap(x), Ok(5));
15657 let x = Arc::new(box "hello");
15660 let x_ptr = Arc::into_raw(x);
15665 let x = Arc::from_raw(x_ptr);
15668 assert_eq!(Arc::try_unwrap(x).map(|x| *x), Ok("hello"));
15677 let arc: Arc<str> = Arc::from("foo");
15679 let ptr = Arc::into_raw(arc.clone());
15680 let arc2 = unsafe { Arc::from_raw(ptr) };
15685 let arc: Arc<dyn Display> = Arc::new(123);
15687 let ptr = Arc::into_raw(arc.clone());
15688 let arc2 = unsafe { Arc::from_raw(ptr) };
15696 let x = Arc::new(box "hello");
15697 let y = Arc::downgrade(&x);
15708 assert_eq!(Arc::try_unwrap(x).map(|x| *x), Ok("hello"));
15717 let arc: Arc<str> = Arc::from("foo");
15718 let weak: Weak<str> = Arc::downgrade(&arc);
15726 let arc: Arc<dyn Display> = Arc::new(123);
15727 let weak: Weak<dyn Display> = Arc::downgrade(&arc);
15738 let mut cow0 = Arc::new(75);
15742 assert!(75 == *Arc::make_mut(&mut cow0));
15743 assert!(75 == *Arc::make_mut(&mut cow1));
15744 assert!(75 == *Arc::make_mut(&mut cow2));
15746 *Arc::make_mut(&mut cow0) += 1;
15747 *Arc::make_mut(&mut cow1) += 2;
15748 *Arc::make_mut(&mut cow2) += 3;
15762 let mut cow0 = Arc::new(75);
15770 *Arc::make_mut(&mut cow0) += 1;
15784 let mut cow0 = Arc::new(75);
15785 let cow1_weak = Arc::downgrade(&cow0);
15790 *Arc::make_mut(&mut cow0) += 1;
15798 let x = Arc::new(5);
15799 let y = Arc::downgrade(&x);
15805 let x = Arc::new(5);
15806 let y = Arc::downgrade(&x);
15817 let a = Arc::new(Cycle { x: Mutex::new(None) });
15818 let b = Arc::downgrade(&a.clone());
15827 let x = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize));
15835 let arc = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize));
15836 let arc_weak = Arc::downgrade(&arc);
15845 let a = Arc::new(0);
15846 assert!(Arc::strong_count(&a) == 1);
15847 let w = Arc::downgrade(&a);
15848 assert!(Arc::strong_count(&a) == 1);
15850 assert!(Arc::strong_count(&b) == 2);
15851 assert!(Arc::strong_count(&a) == 2);
15854 assert!(Arc::strong_count(&b) == 1);
15856 assert!(Arc::strong_count(&b) == 2);
15857 assert!(Arc::strong_count(&c) == 2);
15862 let a = Arc::new(0);
15863 assert!(Arc::strong_count(&a) == 1);
15864 assert!(Arc::weak_count(&a) == 0);
15865 let w = Arc::downgrade(&a);
15866 assert!(Arc::strong_count(&a) == 1);
15867 assert!(Arc::weak_count(&a) == 1);
15869 assert!(Arc::weak_count(&a) == 2);
15872 assert!(Arc::strong_count(&a) == 1);
15873 assert!(Arc::weak_count(&a) == 0);
15875 assert!(Arc::strong_count(&a) == 2);
15876 assert!(Arc::weak_count(&a) == 0);
15877 let d = Arc::downgrade(&c);
15878 assert!(Arc::weak_count(&c) == 1);
15879 assert!(Arc::strong_count(&c) == 2);
15888 let a = Arc::new(5);
15892 // Make sure deriving works with Arc<T>
15895 inner: Arc<i32>,
15900 let x: Arc<[i32]> = Arc::new([1, 2, 3]);
15902 let y = Arc::downgrade(&x.clone());
15912 let x: Arc<CStr> = Arc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
15914 let y: Weak<CStr> = Arc::downgrade(&x);
15927 let foo_arc = Arc::from(foo);
15939 let five = Arc::new(5);
15941 let other_five = Arc::new(5);
15943 assert!(Arc::ptr_eq(&five, &same_five));
15944 assert!(!Arc::ptr_eq(&five, &other_five));
15950 let mut a = Arc::new(atomic::AtomicBool::new(false));
15956 Arc::get_mut(&mut a);
15962 let n = Arc::weak_count(&a2);
15972 let r: Arc<str> = Arc::from("foo");
15980 let r: Arc<[u32]> = Arc::from(s);
15991 let r: Arc<[X]> = Arc::from(s);
16016 let _r: Arc<[Fail]> = Arc::from(s);
16022 let r: Arc<u32> = Arc::from(b);
16032 let r: Arc<str> = Arc::from(s);
16040 let r: Arc<[u32]> = Arc::from(s);
16051 let r: Arc<dyn Display> = Arc::from(b);
16061 let r: Arc<dyn Debug> = Arc::from(b);
16069 let r: Arc<[u32]> = Arc::from(v);
16078 let r1: Arc<dyn Any + Send + Sync> = Arc::new(i32::MAX);
16079 let r2: Arc<dyn Any + Send + Sync> = Arc::new("abc");
16085 assert_eq!(r1i32.unwrap(), Arc::new(i32::MAX));
16091 assert_eq!(r2str.unwrap(), Arc::new("abc"));
16097 let r: Arc<[u32]> = Arc::from(v);
16099 let a: Result<Arc<[u32; 3]>, _> = r.clone().try_into();
16102 let a: Result<Arc<[u32; 2]>, _> = r.clone().try_into();
16111 let zero_refs = Arc::new_cyclic(|inner| {
16117 assert_eq!(Arc::strong_count(&zero_refs), 1);
16118 assert_eq!(Arc::weak_count(&zero_refs), 0);
16128 let one_ref = Arc::new_cyclic(|inner| {
16134 assert_eq!(Arc::strong_count(&one_ref), 1);
16135 assert_eq!(Arc::weak_count(&one_ref), 1);
16138 assert!(Arc::ptr_eq(&one_ref, &one_ref2));
16140 assert_eq!(Arc::strong_count(&one_ref), 2);
16141 assert_eq!(Arc::weak_count(&one_ref), 1);
16150 let two_refs = Arc::new_cyclic(|inner| {
16160 assert_eq!(Arc::strong_count(&two_refs), 1);
16161 assert_eq!(Arc::weak_count(&two_refs), 2);
16164 assert!(Arc::ptr_eq(&two_refs, &two_refs1));
16167 assert!(Arc::ptr_eq(&two_refs, &two_refs2));
16169 assert_eq!(Arc::strong_count(&two_refs), 3);
16170 assert_eq!(Arc::weak_count(&two_refs), 2);
18924 /// [`Arc::make_mut`][crate::sync::Arc::make_mut] can provide clone-on-write
19332 //! The [`Arc`] type is the threadsafe equivalent of the [`Rc`] type. It
19334 //! contained type `T` is shareable. Additionally, [`Arc<T>`][`Arc`] is itself
19352 //! [`Arc`]: sync
19516 //! [`sync::Arc`][arc].
19731 //! [arc]: crate::sync::Arc
23129 use crate::sync::Arc;
23139 /// to wake up a task is stored in an [`Arc`]. Some executors (especially
23143 /// [arc]: ../../std/sync/struct.Arc.html
23156 /// use std::sync::Arc;
23164 /// fn wake(self: Arc<Self>) {
23176 /// let waker = Arc::new(ThreadWaker(t)).into();
23196 fn wake(self: Arc<Self>);
23202 /// [`Arc`] and calls [`wake`] on the clone.
23206 fn wake_by_ref(self: &Arc<Self>) {
23212 impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
23216 fn from(waker: Arc<W>) -> Waker {
23218 // a RawWaker from Arc<W>.
23224 impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
23228 fn from(waker: Arc<W>) -> RawWaker {
23234 // inlining this into the `From<Arc<W>> for RawWaker` impl, to ensure that
23235 // the safety of `From<Arc<W>> for Waker` does not depend on the correct
23239 fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
23242 unsafe { Arc::increment_strong_count(waker as *const W) };
23249 // Wake by value, moving the Arc into the Wake::wake function
23251 let waker = unsafe { Arc::from_raw(waker as *const W) };
23257 let waker = unsafe { ManuallyDrop::new(Arc::from_raw(waker as *const W)) };
23261 // Decrement the reference count of the Arc on drop
23263 unsafe { Arc::decrement_strong_count(waker as *const W) };
23267 Arc::into_raw(waker) as *const (),
23275 //! See the [`Arc<T>`][Arc] documentation for more details.
23307 /// A soft limit on the amount of references that may be made to an `Arc`.
23321 // reports in Arc / Weak implementation use atomic loads for synchronization
23330 /// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
23333 /// The type `Arc<T>` provides shared ownership of a value of type `T`,
23334 /// allocated in the heap. Invoking [`clone`][clone] on `Arc` produces
23335 /// a new `Arc` instance, which points to the same allocation on the heap as the
23336 /// source `Arc`, while increasing a reference count. When the last `Arc`
23340 /// Shared references in Rust disallow mutation by default, and `Arc` is no
23342 /// inside an `Arc`. If you need to mutate through an `Arc`, use
23348 /// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
23354 /// However, a library might choose `Arc<T>` in order to give library consumers
23357 /// `Arc<T>` will implement [`Send`] and [`Sync`] as long as the `T` implements
23359 /// `Arc<T>` to make it thread-safe? This may be a bit counter-intuitive at
23360 /// first: after all, isn't the point of `Arc<T>` thread safety? The key is
23361 /// this: `Arc<T>` makes it thread safe to have multiple ownership of the same
23363 /// `Arc<`[`RefCell<T>`]`>`. [`RefCell<T>`] isn't [`Sync`], and if `Arc<T>` was always
23364 /// [`Send`], `Arc<`[`RefCell<T>`]`>` would be as well. But then we'd have a problem:
23368 /// In the end, this means that you may need to pair `Arc<T>` with some sort of
23375 /// to an `Arc`, but this will return [`None`] if the value stored in the allocation has
23380 /// A cycle between `Arc` pointers will never be deallocated. For this reason,
23382 /// strong `Arc` pointers from parent nodes to children, and [`Weak`]
23388 /// `Clone` trait implemented for [`Arc<T>`][Arc] and [`Weak<T>`][Weak].
23391 /// use std::sync::Arc;
23392 /// let foo = Arc::new(vec![1.0, 2.0, 3.0]);
23395 /// let b = Arc::clone(&foo);
23401 /// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
23402 /// so you can call `T`'s methods on a value of type `Arc<T>`. To avoid name
23403 /// clashes with `T`'s methods, the methods of `Arc<T>` itself are associated
23407 /// use std::sync::Arc;
23409 /// let my_arc = Arc::new(());
23410 /// Arc::downgrade(&my_arc);
23413 /// `Arc<T>`'s implementations of traits like `Clone` may also be called using
23418 /// use std::sync::Arc;
23420 /// let arc = Arc::new(());
23424 /// let arc3 = Arc::clone(&arc);
23438 /// [downgrade]: Arc::downgrade
23442 /// [`Arc::clone(&from)`]: Arc::clone
23454 /// use std::sync::Arc;
23457 /// let five = Arc::new(5);
23460 /// let five = Arc::clone(&five);
23473 /// use std::sync::Arc;
23477 /// let val = Arc::new(AtomicUsize::new(5));
23480 /// let val = Arc::clone(&val);
23493 #[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
23495 pub struct Arc<T: ?Sized> {
23501 unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
23503 unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
23506 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
23509 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T> {}
23511 impl<T: ?Sized> Arc<T> {
23521 /// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
23523 /// pointer, which returns an [`Option`]`<`[`Arc`]`<T>>`.
23532 /// managed by [`Arc`] without preventing its inner value from being dropped. It is also used to
23533 /// prevent circular references between [`Arc`] pointers, since mutual owning references
23534 /// would never allow either [`Arc`] to be dropped. For example, a tree could
23535 /// have strong [`Arc`] pointers from parent nodes to children, and `Weak`
23538 /// The typical way to obtain a `Weak` pointer is to call [`Arc::downgrade`].
23587 impl<T> Arc<T> {
23588 /// Constructs a new `Arc<T>`.
23593 /// use std::sync::Arc;
23595 /// let five = Arc::new(5);
23599 pub fn new(data: T) -> Arc<T> {
23610 /// Constructs a new `Arc<T>` using a weak reference to itself. Attempting
23620 /// use std::sync::{Arc, Weak};
23626 /// let foo = Arc::new_cyclic(|me| Foo {
23632 pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Arc<T> {
23675 let strong = Arc::from_inner(init_ptr);
23683 /// Constructs a new `Arc` with uninitialized contents.
23691 /// use std::sync::Arc;
23693 /// let mut five = Arc::<u32>::new_uninit();
23697 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
23705 pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
23707 Arc::from_ptr(Arc::allocate_for_layout(
23715 /// Constructs a new `Arc` with uninitialized contents, with the memory
23726 /// use std::sync::Arc;
23728 /// let zero = Arc::<u32>::new_zeroed();
23736 pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
23738 Arc::from_ptr(Arc::allocate_for_layout(
23746 /// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
23749 pub fn pin(data: T) -> Pin<Arc<T>> {
23750 unsafe { Pin::new_unchecked(Arc::new(data)) }
23753 /// Constructs a new `Arc<T>`, returning an error if allocation fails.
23759 /// use std::sync::Arc;
23761 /// let five = Arc::try_new(5)?;
23766 pub fn try_new(data: T) -> Result<Arc<T>, AllocError> {
23777 /// Constructs a new `Arc` with uninitialized contents, returning an error
23786 /// use std::sync::Arc;
23788 /// let mut five = Arc::<u32>::try_new_uninit()?;
23792 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
23802 pub fn try_new_uninit() -> Result<Arc<mem::MaybeUninit<T>>, AllocError> {
23804 Ok(Arc::from_ptr(Arc::try_allocate_for_layout(
23812 /// Constructs a new `Arc` with uninitialized contents, with the memory
23823 /// use std::sync::Arc;
23825 /// let zero = Arc::<u32>::try_new_zeroed()?;
23835 pub fn try_new_zeroed() -> Result<Arc<mem::MaybeUninit<T>>, AllocError> {
23837 Ok(Arc::from_ptr(Arc::try_allocate_for_layout(
23844 /// Returns the inner value, if the `Arc` has exactly one strong reference.
23846 /// Otherwise, an [`Err`] is returned with the same `Arc` that was
23854 /// use std::sync::Arc;
23856 /// let x = Arc::new(3);
23857 /// assert_eq!(Arc::try_unwrap(x), Ok(3));
23859 /// let x = Arc::new(4);
23860 /// let _y = Arc::clone(&x);
23861 /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
23884 impl<T> Arc<[T]> {
23893 /// use std::sync::Arc;
23895 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
23899 /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
23900 /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
23901 /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
23909 pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
23910 unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) }
23924 /// use std::sync::Arc;
23926 /// let values = Arc::<[u32]>::new_zeroed_slice(3);
23934 pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
23936 Arc::from_ptr(Arc::allocate_for_layout(
23948 impl<T> Arc<mem::MaybeUninit<T>> {
23949 /// Converts to `Arc<T>`.
23967 /// use std::sync::Arc;
23969 /// let mut five = Arc::<u32>::new_uninit();
23973 /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
23982 pub unsafe fn assume_init(self) -> Arc<T> {
23983 Arc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
23987 impl<T> Arc<[mem::MaybeUninit<T>]> {
23988 /// Converts to `Arc<[T]>`.
24006 /// use std::sync::Arc;
24008 /// let mut values = Arc::<[u32]>::new_uninit_slice(3);
24012 /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
24013 /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
24014 /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
24023 pub unsafe fn assume_init(self) -> Arc<[T]> {
24024 unsafe { Arc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _) }
24028 impl<T: ?Sized> Arc<T> {
24029 /// Consumes the `Arc`, returning the wrapped pointer.
24031 /// To avoid a memory leak the pointer must be converted back to an `Arc` using
24032 /// [`Arc::from_raw`].
24037 /// use std::sync::Arc;
24039 /// let x = Arc::new("hello".to_owned());
24040 /// let x_ptr = Arc::into_raw(x);
24052 /// The counts are not affected in any way and the `Arc` is not consumed. The pointer is valid for
24053 /// as long as there are strong counts in the `Arc`.
24058 /// use std::sync::Arc;
24060 /// let x = Arc::new("hello".to_owned());
24061 /// let y = Arc::clone(&x);
24062 /// let x_ptr = Arc::as_ptr(&x);
24063 /// assert_eq!(x_ptr, Arc::as_ptr(&y));
24076 /// Constructs an `Arc<T>` from a raw pointer.
24079 /// [`Arc<U>::into_raw`][into_raw] where `U` must have the same size and
24090 /// even if the returned `Arc<T>` is never accessed.
24092 /// [into_raw]: Arc::into_raw
24098 /// use std::sync::Arc;
24100 /// let x = Arc::new("hello".to_owned());
24101 /// let x_ptr = Arc::into_raw(x);
24104 /// // Convert back to an `Arc` to prevent leak.
24105 /// let x = Arc::from_raw(x_ptr);
24108 /// // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe.
24130 /// use std::sync::Arc;
24132 /// let five = Arc::new(5);
24134 /// let weak_five = Arc::downgrade(&five);
24151 // into usize::MAX; in general both Rc and Arc need to be adjusted
24179 /// use std::sync::Arc;
24181 /// let five = Arc::new(5);
24182 /// let _weak_five = Arc::downgrade(&five);
24185 /// // the `Arc` or `Weak` between threads.
24186 /// assert_eq!(1, Arc::weak_count(&five));
24197 /// Gets the number of strong (`Arc`) pointers to this allocation.
24208 /// use std::sync::Arc;
24210 /// let five = Arc::new(5);
24211 /// let _also_five = Arc::clone(&five);
24214 /// // the `Arc` between threads.
24215 /// assert_eq!(2, Arc::strong_count(&five));
24223 /// Increments the strong reference count on the `Arc<T>` associated with the
24228 /// The pointer must have been obtained through `Arc::into_raw`, and the
24229 /// associated `Arc` instance must be valid (i.e. the strong count must be at
24235 /// use std::sync::Arc;
24237 /// let five = Arc::new(5);
24240 /// let ptr = Arc::into_raw(five);
24241 /// Arc::increment_strong_count(ptr);
24244 /// // the `Arc` between threads.
24245 /// let five = Arc::from_raw(ptr);
24246 /// assert_eq!(2, Arc::strong_count(&five));
24252 // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
24253 let arc = unsafe { mem::ManuallyDrop::new(Arc::<T>::from_raw(ptr)) };
24258 /// Decrements the strong reference count on the `Arc<T>` associated with the
24263 /// The pointer must have been obtained through `Arc::into_raw`, and the
24264 /// associated `Arc` instance must be valid (i.e. the strong count must be at
24266 /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
24272 /// use std::sync::Arc;
24274 /// let five = Arc::new(5);
24277 /// let ptr = Arc::into_raw(five);
24278 /// Arc::increment_strong_count(ptr);
24281 /// // the `Arc` between threads.
24282 /// let five = Arc::from_raw(ptr);
24283 /// assert_eq!(2, Arc::strong_count(&five));
24284 /// Arc::decrement_strong_count(ptr);
24285 /// assert_eq!(1, Arc::strong_count(&five));
24291 unsafe { mem::drop(Arc::from_raw(ptr)) };
24317 /// Returns `true` if the two `Arc`s point to the same allocation
24323 /// use std::sync::Arc;
24325 /// let five = Arc::new(5);
24326 /// let same_five = Arc::clone(&five);
24327 /// let other_five = Arc::new(5);
24329 /// assert!(Arc::ptr_eq(&five, &same_five));
24330 /// assert!(!Arc::ptr_eq(&five, &other_five));
24339 impl<T: ?Sized> Arc<T> {
24356 Arc::try_allocate_for_layout(value_layout, allocate, mem_to_arcinner)
24404 fn from_box(v: Box<T>) -> Arc<T> {
24427 impl<T> Arc<[T]> {
24439 /// Copy elements from slice into newly allocated Arc<\[T\]>
24442 unsafe fn copy_from_slice(v: &[T]) -> Arc<[T]> {
24452 /// Constructs an `Arc<[T]>` from an iterator known to be of a certain size.
24455 unsafe fn from_iter_exact(iter: impl iter::Iterator<Item = T>, len: usize) -> Arc<[T]> {
24506 impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
24513 impl<T: Copy> ArcFromSlice<T> for Arc<[T]> {
24516 unsafe { Arc::copy_from_slice(v) }
24521 impl<T: ?Sized> Clone for Arc<T> {
24522 /// Makes a clone of the `Arc` pointer.
24530 /// use std::sync::Arc;
24532 /// let five = Arc::new(5);
24534 /// let _ = Arc::clone(&five);
24537 fn clone(&self) -> Arc<T> {
24569 impl<T: ?Sized> Deref for Arc<T> {
24579 impl<T: ?Sized> Receiver for Arc<T> {}
24581 impl<T: Clone> Arc<T> {
24582 /// Makes a mutable reference into the given `Arc`.
24584 /// If there are other `Arc` or [`Weak`] pointers to the same allocation,
24594 /// [get_mut]: Arc::get_mut
24600 /// use std::sync::Arc;
24602 /// let mut data = Arc::new(5);
24604 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
24605 /// let mut other_data = Arc::clone(&data); // Won't clone inner data
24606 /// *Arc::make_mut(&mut data) += 1; // Clones inner data
24607 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
24608 /// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
24630 let data = Arc::get_mut_unchecked(&mut arc);
24637 // dropped. Worst case, we end up allocated a new Arc unnecessarily.
24640 // refs remaining. We'll move the contents to a new Arc, and
24654 let data = Arc::get_mut_unchecked(&mut arc);
24670 impl<T: ?Sized> Arc<T> {
24671 /// Returns a mutable reference into the given `Arc`, if there are
24672 /// no other `Arc` or [`Weak`] pointers to the same allocation.
24680 /// [make_mut]: Arc::make_mut
24686 /// use std::sync::Arc;
24688 /// let mut x = Arc::new(3);
24689 /// *Arc::get_mut(&mut x).unwrap() = 4;
24692 /// let _y = Arc::clone(&x);
24693 /// assert!(Arc::get_mut(&mut x).is_none());
24702 // the Arc itself to be `mut`, so we're returning the only possible
24704 unsafe { Some(Arc::get_mut_unchecked(this)) }
24710 /// Returns a mutable reference into the given `Arc`,
24715 /// [`get_mut`]: Arc::get_mut
24719 /// Any other `Arc` or [`Weak`] pointers to the same allocation must not be dereferenced
24722 /// for example immediately after `Arc::new`.
24729 /// use std::sync::Arc;
24731 /// let mut x = Arc::new(String::new());
24733 /// Arc::get_mut_unchecked(&mut x).push_str("foo")
24775 unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
24776 /// Drops the `Arc`.
24785 /// use std::sync::Arc;
24795 /// let foo = Arc::new(Foo);
24796 /// let foo2 = Arc::clone(&foo);
24826 // In particular, while the contents of an Arc are usually immutable, it's
24846 impl Arc<dyn Any + Send + Sync> {
24849 /// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
24855 /// use std::sync::Arc;
24857 /// fn print_if_string(value: Arc<dyn Any + Send + Sync>) {
24864 /// print_if_string(Arc::new(my_string));
24865 /// print_if_string(Arc::new(0i8));
24867 pub fn downcast<T>(self) -> Result<Arc<T>, Self>
24874 Ok(Arc::from_inner(ptr))
24917 /// use std::sync::Arc;
24920 /// let strong = Arc::new("hello".to_owned());
24921 /// let weak = Arc::downgrade(&strong);
24962 /// use std::sync::{Arc, Weak};
24964 /// let strong = Arc::new("hello".to_owned());
24965 /// let weak = Arc::downgrade(&strong);
24968 /// assert_eq!(1, Arc::weak_count(&strong));
24972 /// assert_eq!(0, Arc::weak_count(&strong));
25004 /// use std::sync::{Arc, Weak};
25006 /// let strong = Arc::new("hello".to_owned());
25008 /// let raw_1 = Arc::downgrade(&strong).into_raw();
25009 /// let raw_2 = Arc::downgrade(&strong).into_raw();
25011 /// assert_eq!(2, Arc::weak_count(&strong));
25014 /// assert_eq!(1, Arc::weak_count(&strong));
25048 /// Attempts to upgrade the `Weak` pointer to an [`Arc`], delaying
25056 /// use std::sync::Arc;
25058 /// let five = Arc::new(5);
25060 /// let weak_five = Arc::downgrade(&five);
25062 /// let strong_five: Option<Arc<_>> = weak_five.upgrade();
25072 pub fn upgrade(&self) -> Option<Arc<T>> {
25089 // See comments in `Arc::clone` for why we do this (for `mem::forget`).
25095 // Acquire is necessary for the success case to synchronise with `Arc::new_cyclic`, when the inner
25099 Ok(_) => return Some(Arc::from_inner(self.ptr)), // null checked above
25105 /// Gets the number of strong (`Arc`) pointers pointing to this allocation.
25122 /// either direction when other threads are manipulating any `Arc`s or
25152 // the field may be mutated concurrently (for example, if the last `Arc`
25173 /// use std::sync::Arc;
25175 /// let first_rc = Arc::new(5);
25176 /// let first = Arc::downgrade(&first_rc);
25177 /// let second = Arc::downgrade(&first_rc);
25181 /// let third_rc = Arc::new(5);
25182 /// let third = Arc::downgrade(&third_rc);
25190 /// use std::sync::{Arc, Weak};
25196 /// let third_rc = Arc::new(());
25197 /// let third = Arc::downgrade(&third_rc);
25216 /// use std::sync::{Arc, Weak};
25218 /// let weak_five = Arc::downgrade(&Arc::new(5));
25229 // See comments in Arc::clone() for why this is relaxed. This can use a
25235 // See comments in Arc::clone() for why we do this (for mem::forget).
25272 /// use std::sync::{Arc, Weak};
25282 /// let foo = Arc::new(Foo);
25283 /// let weak_foo = Arc::downgrade(&foo);
25293 // deallocate the data entirely. See the discussion in Arc::drop() about
25311 fn eq(&self, other: &Arc<T>) -> bool;
25312 fn ne(&self, other: &Arc<T>) -> bool;
25316 impl<T: ?Sized + PartialEq> ArcEqIdent<T> for Arc<T> {
25318 default fn eq(&self, other: &Arc<T>) -> bool {
25322 default fn ne(&self, other: &Arc<T>) -> bool {
25328 /// would otherwise add a cost to all equality checks on refs. We assume that `Arc`s are used to
25330 /// cost to pay off more easily. It's also more likely to have two `Arc` clones, that point to
25335 impl<T: ?Sized + crate::rc::MarkerEq> ArcEqIdent<T> for Arc<T> {
25337 fn eq(&self, other: &Arc<T>) -> bool {
25338 Arc::ptr_eq(self, other) || **self == **other
25342 fn ne(&self, other: &Arc<T>) -> bool {
25343 !Arc::ptr_eq(self, other) && **self != **other
25348 impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
25349 /// Equality for two `Arc`s.
25351 /// Two `Arc`s are equal if their inner values are equal, even if they are
25355 /// two `Arc`s that point to the same allocation are always equal.
25360 /// use std::sync::Arc;
25362 /// let five = Arc::new(5);
25364 /// assert!(five == Arc::new(5));
25367 fn eq(&self, other: &Arc<T>) -> bool {
25371 /// Inequality for two `Arc`s.
25373 /// Two `Arc`s are unequal if their inner values are unequal.
25376 /// two `Arc`s that point to the same value are never unequal.
25381 /// use std::sync::Arc;
25383 /// let five = Arc::new(5);
25385 /// assert!(five != Arc::new(6));
25388 fn ne(&self, other: &Arc<T>) -> bool {
25394 impl<T: ?Sized + PartialOrd> PartialOrd for Arc<T> {
25395 /// Partial comparison for two `Arc`s.
25402 /// use std::sync::Arc;
25405 /// let five = Arc::new(5);
25407 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6)));
25409 fn partial_cmp(&self, other: &Arc<T>) -> Option<Ordering> {
25413 /// Less-than comparison for two `Arc`s.
25420 /// use std::sync::Arc;
25422 /// let five = Arc::new(5);
25424 /// assert!(five < Arc::new(6));
25426 fn lt(&self, other: &Arc<T>) -> bool {
25430 /// 'Less than or equal to' comparison for two `Arc`s.
25437 /// use std::sync::Arc;
25439 /// let five = Arc::new(5);
25441 /// assert!(five <= Arc::new(5));
25443 fn le(&self, other: &Arc<T>) -> bool {
25447 /// Greater-than comparison for two `Arc`s.
25454 /// use std::sync::Arc;
25456 /// let five = Arc::new(5);
25458 /// assert!(five > Arc::new(4));
25460 fn gt(&self, other: &Arc<T>) -> bool {
25464 /// 'Greater than or equal to' comparison for two `Arc`s.
25471 /// use std::sync::Arc;
25473 /// let five = Arc::new(5);
25475 /// assert!(five >= Arc::new(5));
25477 fn ge(&self, other: &Arc<T>) -> bool {
25482 impl<T: ?Sized + Ord> Ord for Arc<T> {
25483 /// Comparison for two `Arc`s.
25490 /// use std::sync::Arc;
25493 /// let five = Arc::new(5);
25495 /// assert_eq!(Ordering::Less, five.cmp(&Arc::new(6)));
25497 fn cmp(&self, other: &Arc<T>) -> Ordering {
25502 impl<T: ?Sized + Eq> Eq for Arc<T> {}
25505 impl<T: ?Sized + fmt::Display> fmt::Display for Arc<T> {
25512 impl<T: ?Sized + fmt::Debug> fmt::Debug for Arc<T> {
25519 impl<T: ?Sized> fmt::Pointer for Arc<T> {
25526 impl<T: Default> Default for Arc<T> {
25527 /// Creates a new `Arc<T>`, with the `Default` value for `T`.
25532 /// use std::sync::Arc;
25534 /// let x: Arc<i32> = Default::default();
25537 fn default() -> Arc<T> {
25538 Arc::new(Default::default())
25543 impl<T: ?Sized + Hash> Hash for Arc<T> {
25550 impl<T> From<T> for Arc<T> {
25552 Arc::new(t)
25557 impl<T: Clone> From<&[T]> for Arc<[T]> {
25563 /// # use std::sync::Arc;
25565 /// let shared: Arc<[i32]> = Arc::from(original);
25569 fn from(v: &[T]) -> Arc<[T]> {
25575 impl From<&str> for Arc<str> {
25581 /// # use std::sync::Arc;
25582 /// let shared: Arc<str> = Arc::from("eggplant");
25586 fn from(v: &str) -> Arc<str> {
25587 let arc = Arc::<[u8]>::from(v.as_bytes());
25588 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const str) }
25593 impl From<String> for Arc<str> {
25599 /// # use std::sync::Arc;
25601 /// let shared: Arc<str> = Arc::from(unique);
25605 fn from(v: String) -> Arc<str> {
25606 Arc::from(&v[..])
25611 impl<T: ?Sized> From<Box<T>> for Arc<T> {
25617 /// # use std::sync::Arc;
25619 /// let shared: Arc<str> = Arc::from(unique);
25623 fn from(v: Box<T>) -> Arc<T> {
25624 Arc::from_box(v)
25629 impl<T> From<Vec<T>> for Arc<[T]> {
25635 /// # use std::sync::Arc;
25637 /// let shared: Arc<[i32]> = Arc::from(unique);
25641 fn from(mut v: Vec<T>) -> Arc<[T]> {
25643 let arc = Arc::copy_from_slice(&v);
25654 impl<'a, B> From<Cow<'a, B>> for Arc<B>
25657 Arc<B>: From<&'a B> + From<B::Owned>,
25660 fn from(cow: Cow<'a, B>) -> Arc<B> {
25662 Cow::Borrowed(s) => Arc::from(s),
25663 Cow::Owned(s) => Arc::from(s),
25669 impl<T, const N: usize> TryFrom<Arc<[T]>> for Arc<[T; N]> {
25670 type Error = Arc<[T]>;
25672 fn try_from(boxed_slice: Arc<[T]>) -> Result<Self, Self::Error> {
25674 Ok(unsafe { Arc::from_raw(Arc::into_raw(boxed_slice) as *mut [T; N]) })
25682 impl<T> iter::FromIterator<T> for Arc<[T]> {
25683 /// Takes each element in the `Iterator` and collects it into an `Arc<[T]>`.
25689 /// In the general case, collecting into `Arc<[T]>` is done by first
25693 /// # use std::sync::Arc;
25694 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0).collect();
25701 /// # use std::sync::Arc;
25702 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0)
25704 /// .into(); // A second allocation for `Arc<[T]>` happens here.
25709 /// and then it will allocate once for turning the `Vec<T>` into the `Arc<[T]>`.
25714 /// a single allocation will be made for the `Arc<[T]>`. For example:
25717 /// # use std::sync::Arc;
25718 /// let evens: Arc<[u8]> = (0..10).collect(); // Just a single allocation happens here.
25726 /// Specialization trait used for collecting into `Arc<[T]>`.
25728 fn to_arc_slice(self) -> Arc<[T]>;
25732 default fn to_arc_slice(self) -> Arc<[T]> {
25738 fn to_arc_slice(self) -> Arc<[T]> {
25751 Arc::from_iter_exact(self, low)
25764 impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
25771 impl<T: ?Sized> AsRef<T> for Arc<T> {
25778 impl<T: ?Sized> Unpin for Arc<T> {}