Lines Matching refs:Rc
3389 type Rc<T> = Arc<T>;
3403 // Collecting into a `Vec<T>` or `Rc<[T]>` should make no difference:
3405 let rc = iter.collect::<Rc<[_]>>();
3412 let _rc_4 = Rc::downgrade(&_rc_3);
3425 // Collecting into a `Vec<T>` or `Rc<[T]>` should make no difference:
3427 let rc = iter.collect::<Rc<[_]>>();
3435 let _rc_4 = Rc::downgrade(&_rc_3);
3443 let rc = iter.collect::<Rc<[_]>>();
3449 let _rc_4 = Rc::downgrade(&_rc_3);
3464 let _ = iter.collect::<Rc<[_]>>();
3492 assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
6473 use std::rc::Rc;
6484 test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
6538 use std::rc::Rc;
6822 let v = (0..5).map(Rc::new).collect::<Vec<_>>();
6834 assert!(v.iter().all(|r| Rc::strong_count(r) == 1));
6839 let v = (0..5).map(Rc::new).collect::<Vec<_>>();
6849 assert!(v.iter().all(|r| Rc::strong_count(r) == 1));
6854 struct Wrap(Rc<i32>);
6864 let v = (0..5).map(|x| Rc::new(x)).collect::<Vec<_>>();
6877 // The panicked wrapper also has its Rc dropped.
6878 assert!(v.iter().all(|r| Rc::strong_count(r) == 1));
7545 let drop_count: Vec<_> = (0..=2).map(|_| Rc::new(())).collect();
7552 assert_eq!(Rc::strong_count(&drop_count[0]), 1, "front was dropped");
7553 assert_eq!(Rc::strong_count(&drop_count[1]), 2, "one element was collected");
7554 assert_eq!(Rc::strong_count(&drop_count[2]), 1, "tail was dropped");
7560 let drop_count: Vec<_> = (0..=2).map(|_| Rc::new(())).collect();
7577 drop_count.iter().map(Rc::strong_count).all(|count| count == 1),
7842 use std::rc::Rc;
7847 drop_counts: Rc<Mutex<Vec<usize>>>,
7858 let drop_counts = Rc::new(Mutex::new(vec![0_usize; check_count]));
7860 .map(|index| Check { index, drop_counts: Rc::clone(&drop_counts) })
7894 use std::rc::Rc;
7899 drop_counts: Rc<Mutex<Vec<usize>>>,
7910 let drop_counts = Rc::new(Mutex::new(vec![0_usize; check_count]));
7912 .map(|index| Check { index, drop_counts: Rc::clone(&drop_counts) })
9059 use std::rc::{Rc, Weak};
9075 let a: Rc<[u32; 3]> = Rc::new([3, 2, 1]);
9076 let a: Rc<[u32]> = a; // Unsizing
9077 let b: Rc<[u32]> = Rc::from(&[3, 2, 1][..]); // Conversion
9081 let mut a = Rc::downgrade(&a);
9088 let a: Rc<u32> = Rc::new(4);
9089 let a: Rc<dyn Any> = a; // Unsizing
9092 let mut a = Rc::downgrade(&a);
9106 let x = Rc::new(f32::NAN);
9121 let x = Rc::new(TestPEq(RefCell::new(0)));
9138 let x = Rc::new(TestEq(RefCell::new(0)));
9156 // Collecting into a `Vec<T>` or `Rc<[T]>` should make no difference:
9158 let rc = iter.collect::<Rc<[_]>>();
9165 let _rc_4 = Rc::downgrade(&_rc_3);
9178 // Collecting into a `Vec<T>` or `Rc<[T]>` should make no difference:
9180 let rc = iter.collect::<Rc<[_]>>();
9188 let _rc_4 = Rc::downgrade(&_rc_3);
9196 let rc = iter.collect::<Rc<[_]>>();
9202 let _rc_4 = Rc::downgrade(&_rc_3);
9217 let _ = iter.collect::<Rc<[_]>>();
9245 assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
9252 use std::rc::Rc;
10417 let mut v = vec![Rc::new(1)];
10419 v.push(Rc::new(2));
13902 /// example, `vec![Rc::new(1); 5]` will create a vector of five references
14402 /// Used by `Box::clone` and `Rc`/`Arc::make_mut`.
18923 /// [`Rc::make_mut`][crate::rc::Rc::make_mut] and
19322 //! The [`Rc`] type is a non-threadsafe reference-counted pointer type intended
19323 //! for sharing memory within a thread. An [`Rc`] pointer wraps a type, `T`, and
19332 //! The [`Arc`] type is the threadsafe equivalent of the [`Rc`] type. It
19333 //! provides all the same functionality of [`Rc`], except it requires that the
19335 //! sendable while [`Rc<T>`][`Rc`] is not.
19355 //! [`Rc`]: rc
19496 //! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference
19499 //! The type [`Rc<T>`][`Rc`] provides shared ownership of a value of type `T`,
19500 //! allocated in the heap. Invoking [`clone`][clone] on [`Rc`] produces a new
19501 //! pointer to the same allocation in the heap. When the last [`Rc`] pointer to a
19505 //! Shared references in Rust disallow mutation by default, and [`Rc`]
19507 //! something inside an [`Rc`]. If you need mutability, put a [`Cell`]
19508 //! or [`RefCell`] inside the [`Rc`]; see [an example of mutability
19509 //! inside an `Rc`][mutability].
19511 //! [`Rc`] uses non-atomic reference counting. This means that overhead is very
19512 //! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`]
19514 //! will check *at compile time* that you are not sending [`Rc`]s between
19520 //! to an [`Rc`], but this will return [`None`] if the value stored in the allocation has
19525 //! A cycle between [`Rc`] pointers will never be deallocated. For this reason,
19527 //! [`Rc`] pointers from parent nodes to children, and [`Weak`] pointers from
19530 //! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
19531 //! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name
19532 //! clashes with `T`'s methods, the methods of [`Rc<T>`][`Rc`] itself are associated
19536 //! use std::rc::Rc;
19538 //! let my_rc = Rc::new(());
19539 //! Rc::downgrade(&my_rc);
19542 //! `Rc<T>`'s implementations of traits like `Clone` may also be called using
19547 //! use std::rc::Rc;
19549 //! let rc = Rc::new(());
19553 //! let rc3 = Rc::clone(&rc);
19562 //! is done using the `Clone` trait implemented for [`Rc<T>`][`Rc`] and [`Weak<T>`][`Weak`].
19565 //! use std::rc::Rc;
19567 //! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
19570 //! let b = Rc::clone(&foo);
19574 //! The `Rc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
19583 //! `Owner`. [`Rc`] allows us to share an `Owner` between multiple `Gadget`s,
19587 //! use std::rc::Rc;
19596 //! owner: Rc<Owner>,
19602 //! let gadget_owner: Rc<Owner> = Rc::new(
19608 //! // Create `Gadget`s belonging to `gadget_owner`. Cloning the `Rc<Owner>`
19613 //! owner: Rc::clone(&gadget_owner),
19617 //! owner: Rc::clone(&gadget_owner),
19625 //! // single `Rc<Owner>`, not the `Owner` it points to. As long as there are
19626 //! // other `Rc<Owner>` pointing at the same `Owner` allocation, it will remain
19628 //! // `Rc<Owner>` automatically dereferences to `Owner`.
19639 //! `Owner` to `Gadget`, we will run into problems. An [`Rc`] pointer from `Owner`
19647 //! them needs to be mutable. This is difficult because [`Rc`] enforces
19655 //! use std::rc::Rc;
19667 //! owner: Rc<Owner>,
19675 //! let gadget_owner: Rc<Owner> = Rc::new(
19683 //! let gadget1 = Rc::new(
19686 //! owner: Rc::clone(&gadget_owner),
19689 //! let gadget2 = Rc::new(
19692 //! owner: Rc::clone(&gadget_owner),
19699 //! gadgets.push(Rc::downgrade(&gadget1));
19700 //! gadgets.push(Rc::downgrade(&gadget2));
19710 //! // `upgrade`, which returns an `Option<Rc<Gadget>>`.
19721 //! // are destroyed. There are now no strong (`Rc`) pointers to the
19733 //! [downgrade]: Rc::downgrade
19781 /// A single-threaded reference-counting pointer. 'Rc' stands for 'Reference
19786 /// The inherent methods of `Rc` are all associated functions, which means
19787 /// that you have to call them as e.g., [`Rc::get_mut(&mut value)`][get_mut] instead of
19790 /// [get_mut]: Rc::get_mut
19791 #[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
19793 pub struct Rc<T: ?Sized> {
19799 impl<T: ?Sized> !marker::Send for Rc<T> {}
19801 impl<T: ?Sized> !marker::Sync for Rc<T> {}
19804 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
19807 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Rc<U>> for Rc<T> {}
19809 impl<T: ?Sized> Rc<T> {
19812 // This unsafety is ok because while this Rc is alive we're guaranteed
19826 impl<T> Rc<T> {
19827 /// Constructs a new `Rc<T>`.
19832 /// use std::rc::Rc;
19834 /// let five = Rc::new(5);
19837 pub fn new(value: T) -> Rc<T> {
19847 /// Constructs a new `Rc<T>` using a weak reference to itself. Attempting
19857 /// use std::rc::{Rc, Weak};
19864 /// pub fn new() -> Rc<Self> {
19865 /// Rc::new_cyclic(|self_weak| {
19872 pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Rc<T> {
19903 let strong = Rc::from_inner(init_ptr);
19911 /// Constructs a new `Rc` with uninitialized contents.
19919 /// use std::rc::Rc;
19921 /// let mut five = Rc::<u32>::new_uninit();
19925 /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
19933 pub fn new_uninit() -> Rc<mem::MaybeUninit<T>> {
19935 Rc::from_ptr(Rc::allocate_for_layout(
19943 /// Constructs a new `Rc` with uninitialized contents, with the memory
19954 /// use std::rc::Rc;
19956 /// let zero = Rc::<u32>::new_zeroed();
19964 pub fn new_zeroed() -> Rc<mem::MaybeUninit<T>> {
19966 Rc::from_ptr(Rc::allocate_for_layout(
19974 /// Constructs a new `Rc<T>`, returning an error if the allocation fails
19980 /// use std::rc::Rc;
19982 /// let five = Rc::try_new(5);
19986 pub fn try_new(value: T) -> Result<Rc<T>, AllocError> {
19997 /// Constructs a new `Rc` with uninitialized contents, returning an error if the allocation fails
20005 /// use std::rc::Rc;
20007 /// let mut five = Rc::<u32>::try_new_uninit()?;
20011 /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
20021 pub fn try_new_uninit() -> Result<Rc<mem::MaybeUninit<T>>, AllocError> {
20023 Ok(Rc::from_ptr(Rc::try_allocate_for_layout(
20031 /// Constructs a new `Rc` with uninitialized contents, with the memory
20042 /// use std::rc::Rc;
20044 /// let zero = Rc::<u32>::try_new_zeroed()?;
20054 pub fn try_new_zeroed() -> Result<Rc<mem::MaybeUninit<T>>, AllocError> {
20056 Ok(Rc::from_ptr(Rc::try_allocate_for_layout(
20063 /// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
20066 pub fn pin(value: T) -> Pin<Rc<T>> {
20067 unsafe { Pin::new_unchecked(Rc::new(value)) }
20070 /// Returns the inner value, if the `Rc` has exactly one strong reference.
20072 /// Otherwise, an [`Err`] is returned with the same `Rc` that was
20080 /// use std::rc::Rc;
20082 /// let x = Rc::new(3);
20083 /// assert_eq!(Rc::try_unwrap(x), Ok(3));
20085 /// let x = Rc::new(4);
20086 /// let _y = Rc::clone(&x);
20087 /// assert_eq!(*Rc::try_unwrap(x).unwrap_err(), 4);
20092 if Rc::strong_count(&this) == 1 {
20111 impl<T> Rc<[T]> {
20120 /// use std::rc::Rc;
20122 /// let mut values = Rc::<[u32]>::new_uninit_slice(3);
20126 /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
20127 /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
20128 /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
20136 pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
20137 unsafe { Rc::from_ptr(Rc::allocate_for_slice(len)) }
20151 /// use std::rc::Rc;
20153 /// let values = Rc::<[u32]>::new_zeroed_slice(3);
20161 pub fn new_zeroed_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
20163 Rc::from_ptr(Rc::allocate_for_layout(
20175 impl<T> Rc<mem::MaybeUninit<T>> {
20176 /// Converts to `Rc<T>`.
20194 /// use std::rc::Rc;
20196 /// let mut five = Rc::<u32>::new_uninit();
20200 /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);
20209 pub unsafe fn assume_init(self) -> Rc<T> {
20210 Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
20214 impl<T> Rc<[mem::MaybeUninit<T>]> {
20215 /// Converts to `Rc<[T]>`.
20233 /// use std::rc::Rc;
20235 /// let mut values = Rc::<[u32]>::new_uninit_slice(3);
20239 /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1);
20240 /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2);
20241 /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3);
20250 pub unsafe fn assume_init(self) -> Rc<[T]> {
20251 unsafe { Rc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _) }
20255 impl<T: ?Sized> Rc<T> {
20256 /// Consumes the `Rc`, returning the wrapped pointer.
20258 /// To avoid a memory leak the pointer must be converted back to an `Rc` using
20259 /// [`Rc::from_raw`][from_raw].
20261 /// [from_raw]: Rc::from_raw
20266 /// use std::rc::Rc;
20268 /// let x = Rc::new("hello".to_owned());
20269 /// let x_ptr = Rc::into_raw(x);
20281 /// The counts are not affected in any way and the `Rc` is not consumed. The pointer is valid
20282 /// for as long there are strong counts in the `Rc`.
20287 /// use std::rc::Rc;
20289 /// let x = Rc::new("hello".to_owned());
20290 /// let y = Rc::clone(&x);
20291 /// let x_ptr = Rc::as_ptr(&x);
20292 /// assert_eq!(x_ptr, Rc::as_ptr(&y));
20299 // SAFETY: This cannot go through Deref::deref or Rc::inner because
20301 // write through the pointer after the Rc is recovered through `from_raw`.
20305 /// Constructs an `Rc<T>` from a raw pointer.
20308 /// [`Rc<U>::into_raw`][into_raw] where `U` must have the same size
20319 /// even if the returned `Rc<T>` is never accessed.
20321 /// [into_raw]: Rc::into_raw
20327 /// use std::rc::Rc;
20329 /// let x = Rc::new("hello".to_owned());
20330 /// let x_ptr = Rc::into_raw(x);
20333 /// // Convert back to an `Rc` to prevent leak.
20334 /// let x = Rc::from_raw(x_ptr);
20337 /// // Further calls to `Rc::from_raw(x_ptr)` would be memory-unsafe.
20358 /// use std::rc::Rc;
20360 /// let five = Rc::new(5);
20362 /// let weak_five = Rc::downgrade(&five);
20377 /// use std::rc::Rc;
20379 /// let five = Rc::new(5);
20380 /// let _weak_five = Rc::downgrade(&five);
20382 /// assert_eq!(1, Rc::weak_count(&five));
20390 /// Gets the number of strong (`Rc`) pointers to this allocation.
20395 /// use std::rc::Rc;
20397 /// let five = Rc::new(5);
20398 /// let _also_five = Rc::clone(&five);
20400 /// assert_eq!(2, Rc::strong_count(&five));
20408 /// Increments the strong reference count on the `Rc<T>` associated with the
20413 /// The pointer must have been obtained through `Rc::into_raw`, and the
20414 /// associated `Rc` instance must be valid (i.e. the strong count must be at
20420 /// use std::rc::Rc;
20422 /// let five = Rc::new(5);
20425 /// let ptr = Rc::into_raw(five);
20426 /// Rc::increment_strong_count(ptr);
20428 /// let five = Rc::from_raw(ptr);
20429 /// assert_eq!(2, Rc::strong_count(&five));
20435 // Retain Rc, but don't touch refcount by wrapping in ManuallyDrop
20436 let rc = unsafe { mem::ManuallyDrop::new(Rc::<T>::from_raw(ptr)) };
20441 /// Decrements the strong reference count on the `Rc<T>` associated with the
20446 /// The pointer must have been obtained through `Rc::into_raw`, and the
20447 /// associated `Rc` instance must be valid (i.e. the strong count must be at
20449 /// the final `Rc` and backing storage, but **should not** be called after
20450 /// the final `Rc` has been released.
20455 /// use std::rc::Rc;
20457 /// let five = Rc::new(5);
20460 /// let ptr = Rc::into_raw(five);
20461 /// Rc::increment_strong_count(ptr);
20463 /// let five = Rc::from_raw(ptr);
20464 /// assert_eq!(2, Rc::strong_count(&five));
20465 /// Rc::decrement_strong_count(ptr);
20466 /// assert_eq!(1, Rc::strong_count(&five));
20472 unsafe { mem::drop(Rc::from_raw(ptr)) };
20475 /// Returns `true` if there are no other `Rc` or [`Weak`] pointers to
20479 Rc::weak_count(this) == 0 && Rc::strong_count(this) == 1
20482 /// Returns a mutable reference into the given `Rc`, if there are
20483 /// no other `Rc` or [`Weak`] pointers to the same allocation.
20491 /// [make_mut]: Rc::make_mut
20497 /// use std::rc::Rc;
20499 /// let mut x = Rc::new(3);
20500 /// *Rc::get_mut(&mut x).unwrap() = 4;
20503 /// let _y = Rc::clone(&x);
20504 /// assert!(Rc::get_mut(&mut x).is_none());
20509 if Rc::is_unique(this) { unsafe { Some(Rc::get_mut_unchecked(this)) } } else { None }
20512 /// Returns a mutable reference into the given `Rc`,
20517 /// [`get_mut`]: Rc::get_mut
20521 /// Any other `Rc` or [`Weak`] pointers to the same allocation must not be dereferenced
20524 /// for example immediately after `Rc::new`.
20531 /// use std::rc::Rc;
20533 /// let mut x = Rc::new(String::new());
20535 /// Rc::get_mut_unchecked(&mut x).push_str("foo")
20549 /// Returns `true` if the two `Rc`s point to the same allocation
20555 /// use std::rc::Rc;
20557 /// let five = Rc::new(5);
20558 /// let same_five = Rc::clone(&five);
20559 /// let other_five = Rc::new(5);
20561 /// assert!(Rc::ptr_eq(&five, &same_five));
20562 /// assert!(!Rc::ptr_eq(&five, &other_five));
20571 impl<T: Clone> Rc<T> {
20572 /// Makes a mutable reference into the given `Rc`.
20574 /// If there are other `Rc` pointers to the same allocation, then `make_mut` will
20578 /// If there are no other `Rc` pointers to this allocation, then [`Weak`]
20584 /// [`get_mut`]: Rc::get_mut
20589 /// use std::rc::Rc;
20591 /// let mut data = Rc::new(5);
20593 /// *Rc::make_mut(&mut data) += 1; // Won't clone anything
20594 /// let mut other_data = Rc::clone(&data); // Won't clone inner data
20595 /// *Rc::make_mut(&mut data) += 1; // Clones inner data
20596 /// *Rc::make_mut(&mut data) += 1; // Won't clone anything
20597 /// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
20607 /// use std::rc::Rc;
20609 /// let mut data = Rc::new(75);
20610 /// let weak = Rc::downgrade(&data);
20615 /// *Rc::make_mut(&mut data) += 1;
20623 if Rc::strong_count(this) != 1 {
20628 let data = Rc::get_mut_unchecked(&mut rc);
20632 } else if Rc::weak_count(this) != 0 {
20636 let data = Rc::get_mut_unchecked(&mut rc);
20649 // the `Rc<T>` itself to be `mut`, so we're returning the only possible
20655 impl Rc<dyn Any> {
20658 /// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
20664 /// use std::rc::Rc;
20666 /// fn print_if_string(value: Rc<dyn Any>) {
20673 /// print_if_string(Rc::new(my_string));
20674 /// print_if_string(Rc::new(0i8));
20676 pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<dyn Any>> {
20680 Ok(Rc::from_inner(ptr))
20687 impl<T: ?Sized> Rc<T> {
20704 Rc::try_allocate_for_layout(value_layout, allocate, mem_to_rcbox)
20754 fn from_box(v: Box<T>) -> Rc<T> {
20777 impl<T> Rc<[T]> {
20789 /// Copy elements from slice into newly allocated Rc<\[T\]>
20792 unsafe fn copy_from_slice(v: &[T]) -> Rc<[T]> {
20800 /// Constructs an `Rc<[T]>` from an iterator known to be of a certain size.
20803 unsafe fn from_iter_exact(iter: impl iter::Iterator<Item = T>, len: usize) -> Rc<[T]> {
20854 impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
20861 impl<T: Copy> RcFromSlice<T> for Rc<[T]> {
20864 unsafe { Rc::copy_from_slice(v) }
20869 impl<T: ?Sized> Deref for Rc<T> {
20879 impl<T: ?Sized> Receiver for Rc<T> {}
20882 unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
20883 /// Drops the `Rc`.
20892 /// use std::rc::Rc;
20902 /// let foo = Rc::new(Foo);
20903 /// let foo2 = Rc::clone(&foo);
20928 impl<T: ?Sized> Clone for Rc<T> {
20929 /// Makes a clone of the `Rc` pointer.
20937 /// use std::rc::Rc;
20939 /// let five = Rc::new(5);
20941 /// let _ = Rc::clone(&five);
20944 fn clone(&self) -> Rc<T> {
20951 impl<T: Default> Default for Rc<T> {
20952 /// Creates a new `Rc<T>`, with the `Default` value for `T`.
20957 /// use std::rc::Rc;
20959 /// let x: Rc<i32> = Default::default();
20963 fn default() -> Rc<T> {
20964 Rc::new(Default::default())
20970 fn eq(&self, other: &Rc<T>) -> bool;
20971 fn ne(&self, other: &Rc<T>) -> bool;
20975 impl<T: ?Sized + PartialEq> RcEqIdent<T> for Rc<T> {
20977 default fn eq(&self, other: &Rc<T>) -> bool {
20982 default fn ne(&self, other: &Rc<T>) -> bool {
20994 /// would otherwise add a cost to all equality checks on refs. We assume that `Rc`s are used to
20996 /// cost to pay off more easily. It's also more likely to have two `Rc` clones, that point to
21001 impl<T: ?Sized + MarkerEq> RcEqIdent<T> for Rc<T> {
21003 fn eq(&self, other: &Rc<T>) -> bool {
21004 Rc::ptr_eq(self, other) || **self == **other
21008 fn ne(&self, other: &Rc<T>) -> bool {
21009 !Rc::ptr_eq(self, other) && **self != **other
21014 impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
21015 /// Equality for two `Rc`s.
21017 /// Two `Rc`s are equal if their inner values are equal, even if they are
21021 /// two `Rc`s that point to the same allocation are
21027 /// use std::rc::Rc;
21029 /// let five = Rc::new(5);
21031 /// assert!(five == Rc::new(5));
21034 fn eq(&self, other: &Rc<T>) -> bool {
21038 /// Inequality for two `Rc`s.
21040 /// Two `Rc`s are unequal if their inner values are unequal.
21043 /// two `Rc`s that point to the same allocation are
21049 /// use std::rc::Rc;
21051 /// let five = Rc::new(5);
21053 /// assert!(five != Rc::new(6));
21056 fn ne(&self, other: &Rc<T>) -> bool {
21062 impl<T: ?Sized + Eq> Eq for Rc<T> {}
21065 impl<T: ?Sized + PartialOrd> PartialOrd for Rc<T> {
21066 /// Partial comparison for two `Rc`s.
21073 /// use std::rc::Rc;
21076 /// let five = Rc::new(5);
21078 /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Rc::new(6)));
21081 fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering> {
21085 /// Less-than comparison for two `Rc`s.
21092 /// use std::rc::Rc;
21094 /// let five = Rc::new(5);
21096 /// assert!(five < Rc::new(6));
21099 fn lt(&self, other: &Rc<T>) -> bool {
21103 /// 'Less than or equal to' comparison for two `Rc`s.
21110 /// use std::rc::Rc;
21112 /// let five = Rc::new(5);
21114 /// assert!(five <= Rc::new(5));
21117 fn le(&self, other: &Rc<T>) -> bool {
21121 /// Greater-than comparison for two `Rc`s.
21128 /// use std::rc::Rc;
21130 /// let five = Rc::new(5);
21132 /// assert!(five > Rc::new(4));
21135 fn gt(&self, other: &Rc<T>) -> bool {
21139 /// 'Greater than or equal to' comparison for two `Rc`s.
21146 /// use std::rc::Rc;
21148 /// let five = Rc::new(5);
21150 /// assert!(five >= Rc::new(5));
21153 fn ge(&self, other: &Rc<T>) -> bool {
21159 impl<T: ?Sized + Ord> Ord for Rc<T> {
21160 /// Comparison for two `Rc`s.
21167 /// use std::rc::Rc;
21170 /// let five = Rc::new(5);
21172 /// assert_eq!(Ordering::Less, five.cmp(&Rc::new(6)));
21175 fn cmp(&self, other: &Rc<T>) -> Ordering {
21181 impl<T: ?Sized + Hash> Hash for Rc<T> {
21188 impl<T: ?Sized + fmt::Display> fmt::Display for Rc<T> {
21195 impl<T: ?Sized + fmt::Debug> fmt::Debug for Rc<T> {
21202 impl<T: ?Sized> fmt::Pointer for Rc<T> {
21209 impl<T> From<T> for Rc<T> {
21211 Rc::new(t)
21216 impl<T: Clone> From<&[T]> for Rc<[T]> {
21222 /// # use std::rc::Rc;
21224 /// let shared: Rc<[i32]> = Rc::from(original);
21228 fn from(v: &[T]) -> Rc<[T]> {
21234 impl From<&str> for Rc<str> {
21240 /// # use std::rc::Rc;
21241 /// let shared: Rc<str> = Rc::from("statue");
21245 fn from(v: &str) -> Rc<str> {
21246 let rc = Rc::<[u8]>::from(v.as_bytes());
21247 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const str) }
21252 impl From<String> for Rc<str> {
21258 /// # use std::rc::Rc;
21260 /// let shared: Rc<str> = Rc::from(original);
21264 fn from(v: String) -> Rc<str> {
21265 Rc::from(&v[..])
21270 impl<T: ?Sized> From<Box<T>> for Rc<T> {
21276 /// # use std::rc::Rc;
21278 /// let shared: Rc<i32> = Rc::from(original);
21282 fn from(v: Box<T>) -> Rc<T> {
21283 Rc::from_box(v)
21288 impl<T> From<Vec<T>> for Rc<[T]> {
21294 /// # use std::rc::Rc;
21296 /// let shared: Rc<Vec<i32>> = Rc::from(original);
21300 fn from(mut v: Vec<T>) -> Rc<[T]> {
21302 let rc = Rc::copy_from_slice(&v);
21313 impl<'a, B> From<Cow<'a, B>> for Rc<B>
21316 Rc<B>: From<&'a B> + From<B::Owned>,
21319 fn from(cow: Cow<'a, B>) -> Rc<B> {
21321 Cow::Borrowed(s) => Rc::from(s),
21322 Cow::Owned(s) => Rc::from(s),
21328 impl<T, const N: usize> TryFrom<Rc<[T]>> for Rc<[T; N]> {
21329 type Error = Rc<[T]>;
21331 fn try_from(boxed_slice: Rc<[T]>) -> Result<Self, Self::Error> {
21333 Ok(unsafe { Rc::from_raw(Rc::into_raw(boxed_slice) as *mut [T; N]) })
21341 impl<T> iter::FromIterator<T> for Rc<[T]> {
21342 /// Takes each element in the `Iterator` and collects it into an `Rc<[T]>`.
21348 /// In the general case, collecting into `Rc<[T]>` is done by first
21352 /// # use std::rc::Rc;
21353 /// let evens: Rc<[u8]> = (0..10).filter(|&x| x % 2 == 0).collect();
21360 /// # use std::rc::Rc;
21361 /// let evens: Rc<[u8]> = (0..10).filter(|&x| x % 2 == 0)
21363 /// .into(); // A second allocation for `Rc<[T]>` happens here.
21368 /// and then it will allocate once for turning the `Vec<T>` into the `Rc<[T]>`.
21373 /// a single allocation will be made for the `Rc<[T]>`. For example:
21376 /// # use std::rc::Rc;
21377 /// let evens: Rc<[u8]> = (0..10).collect(); // Just a single allocation happens here.
21385 /// Specialization trait used for collecting into `Rc<[T]>`.
21387 fn to_rc_slice(self) -> Rc<[T]>;
21391 default fn to_rc_slice(self) -> Rc<[T]> {
21397 fn to_rc_slice(self) -> Rc<[T]> {
21410 Rc::from_iter_exact(self, low)
21422 /// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
21424 /// pointer, which returns an [`Option`]`<`[`Rc`]`<T>>`.
21433 /// managed by [`Rc`] without preventing its inner value from being dropped. It is also used to
21434 /// prevent circular references between [`Rc`] pointers, since mutual owning references
21435 /// would never allow either [`Rc`] to be dropped. For example, a tree could
21436 /// have strong [`Rc`] pointers from parent nodes to children, and `Weak`
21439 /// The typical way to obtain a `Weak` pointer is to call [`Rc::downgrade`].
21505 /// use std::rc::Rc;
21508 /// let strong = Rc::new("hello".to_owned());
21509 /// let weak = Rc::downgrade(&strong);
21550 /// use std::rc::{Rc, Weak};
21552 /// let strong = Rc::new("hello".to_owned());
21553 /// let weak = Rc::downgrade(&strong);
21556 /// assert_eq!(1, Rc::weak_count(&strong));
21560 /// assert_eq!(0, Rc::weak_count(&strong));
21593 /// use std::rc::{Rc, Weak};
21595 /// let strong = Rc::new("hello".to_owned());
21597 /// let raw_1 = Rc::downgrade(&strong).into_raw();
21598 /// let raw_2 = Rc::downgrade(&strong).into_raw();
21600 /// assert_eq!(2, Rc::weak_count(&strong));
21603 /// assert_eq!(1, Rc::weak_count(&strong));
21634 /// Attempts to upgrade the `Weak` pointer to an [`Rc`], delaying
21642 /// use std::rc::Rc;
21644 /// let five = Rc::new(5);
21646 /// let weak_five = Rc::downgrade(&five);
21648 /// let strong_five: Option<Rc<_>> = weak_five.upgrade();
21658 pub fn upgrade(&self) -> Option<Rc<T>> {
21664 Some(Rc::from_inner(self.ptr))
21668 /// Gets the number of strong (`Rc`) pointers pointing to this allocation.
21700 // the field may be mutated concurrently (for example, if the last `Rc`
21721 /// use std::rc::Rc;
21723 /// let first_rc = Rc::new(5);
21724 /// let first = Rc::downgrade(&first_rc);
21725 /// let second = Rc::downgrade(&first_rc);
21729 /// let third_rc = Rc::new(5);
21730 /// let third = Rc::downgrade(&third_rc);
21738 /// use std::rc::{Rc, Weak};
21744 /// let third_rc = Rc::new(());
21745 /// let third = Rc::downgrade(&third_rc);
21764 /// use std::rc::{Rc, Weak};
21774 /// let foo = Rc::new(Foo);
21775 /// let weak_foo = Rc::downgrade(&foo);
21804 /// use std::rc::{Rc, Weak};
21806 /// let weak_five = Rc::downgrade(&Rc::new(5));
21935 impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
21942 impl<T: ?Sized> AsRef<T> for Rc<T> {
21949 impl<T: ?Sized> Unpin for Rc<T> {}
23348 /// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
23352 /// [`Rc<T>`] for lower overhead. [`Rc<T>`] is a safe default, because the
23353 /// compiler will catch any attempt to send an [`Rc<T>`] between threads.
23430 /// [`Rc<T>`]: crate::rc::Rc
24072 // write through the pointer after the Rc is recovered through `from_raw`.
24151 // into usize::MAX; in general both Rc and Arc need to be adjusted
24588 /// Note that this differs from the behavior of [`Rc::make_mut`] which disassociates
24595 /// [`Rc::make_mut`]: super::rc::Rc::make_mut
26396 let x = Rc::new(RefCell::new(5));
26404 let x = Rc::new(5);
26410 let x = Rc::new(5);
26418 let x: Rc<Box<_>> = Rc::new(box 5);
26424 let x = Rc::new(5);
26425 let y = Rc::downgrade(&x);
26431 let x = Rc::new(5);
26432 let y = Rc::downgrade(&x);
26443 let a = Rc::new(Cycle { x: RefCell::new(None) });
26444 let b = Rc::downgrade(&a.clone());
26452 let x = Rc::new(3);
26453 assert!(Rc::is_unique(&x));
26455 assert!(!Rc::is_unique(&x));
26457 assert!(Rc::is_unique(&x));
26458 let w = Rc::downgrade(&x);
26459 assert!(!Rc::is_unique(&x));
26461 assert!(Rc::is_unique(&x));
26466 let a = Rc::new(0);
26467 assert!(Rc::strong_count(&a) == 1);
26468 let w = Rc::downgrade(&a);
26469 assert!(Rc::strong_count(&a) == 1);
26471 assert!(Rc::strong_count(&b) == 2);
26472 assert!(Rc::strong_count(&a) == 2);
26475 assert!(Rc::strong_count(&b) == 1);
26477 assert!(Rc::strong_count(&b) == 2);
26478 assert!(Rc::strong_count(&c) == 2);
26483 let a = Rc::new(0);
26484 assert!(Rc::strong_count(&a) == 1);
26485 assert!(Rc::weak_count(&a) == 0);
26486 let w = Rc::downgrade(&a);
26487 assert!(Rc::strong_count(&a) == 1);
26488 assert!(Rc::weak_count(&a) == 1);
26490 assert!(Rc::strong_count(&a) == 1);
26491 assert!(Rc::weak_count(&a) == 0);
26493 assert!(Rc::strong_count(&a) == 2);
26494 assert!(Rc::weak_count(&a) == 0);
26503 let a = Rc::new(0);
26504 let w = Rc::downgrade(&a);
26527 let x = Rc::new(3);
26528 assert_eq!(Rc::try_unwrap(x), Ok(3));
26529 let x = Rc::new(4);
26531 assert_eq!(Rc::try_unwrap(x), Err(Rc::new(4)));
26532 let x = Rc::new(5);
26533 let _w = Rc::downgrade(&x);
26534 assert_eq!(Rc::try_unwrap(x), Ok(5));
26539 let x = Rc::new(box "hello");
26542 let x_ptr = Rc::into_raw(x);
26547 let x = Rc::from_raw(x_ptr);
26550 assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
26559 let rc: Rc<str> = Rc::from("foo");
26561 let ptr = Rc::into_raw(rc.clone());
26562 let rc2 = unsafe { Rc::from_raw(ptr) };
26567 let rc: Rc<dyn Display> = Rc::new(123);
26569 let ptr = Rc::into_raw(rc.clone());
26570 let rc2 = unsafe { Rc::from_raw(ptr) };
26578 let x = Rc::new(box "hello");
26579 let y = Rc::downgrade(&x);
26590 assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
26599 let arc: Rc<str> = Rc::from("foo");
26600 let weak: Weak<str> = Rc::downgrade(&arc);
26608 let arc: Rc<dyn Display> = Rc::new(123);
26609 let weak: Weak<dyn Display> = Rc::downgrade(&arc);
26620 let mut x = Rc::new(3);
26621 *Rc::get_mut(&mut x).unwrap() = 4;
26624 assert!(Rc::get_mut(&mut x).is_none());
26626 assert!(Rc::get_mut(&mut x).is_some());
26627 let _w = Rc::downgrade(&x);
26628 assert!(Rc::get_mut(&mut x).is_none());
26633 let mut cow0 = Rc::new(75);
26637 assert!(75 == *Rc::make_mut(&mut cow0));
26638 assert!(75 == *Rc::make_mut(&mut cow1));
26639 assert!(75 == *Rc::make_mut(&mut cow2));
26641 *Rc::make_mut(&mut cow0) += 1;
26642 *Rc::make_mut(&mut cow1) += 2;
26643 *Rc::make_mut(&mut cow2) += 3;
26657 let mut cow0 = Rc::new(75);
26665 *Rc::make_mut(&mut cow0) += 1;
26680 let mut cow0 = Rc::new(75);
26681 let cow1_weak = Rc::downgrade(&cow0);
26686 *Rc::make_mut(&mut cow0) += 1;
26694 let foo = Rc::new(75);
26700 let foo: Rc<[i32]> = Rc::new([1, 2, 3]);
26709 let x: Rc<CStr> = Rc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
26711 let y: Weak<CStr> = Rc::downgrade(&x);
26724 let foo_rc = Rc::from(foo);
26736 let five = Rc::new(5);
26738 let other_five = Rc::new(5);
26740 assert!(Rc::ptr_eq(&five, &same_five));
26741 assert!(!Rc::ptr_eq(&five, &other_five));
26746 let r: Rc<str> = Rc::from("foo");
26754 let r: Rc<[u32]> = Rc::from(s);
26765 let r: Rc<[X]> = Rc::from(s);
26790 let _r: Rc<[Fail]> = Rc::from(s);
26796 let r: Rc<u32> = Rc::from(b);
26806 let r: Rc<str> = Rc::from(s);
26814 let r: Rc<[u32]> = Rc::from(s);
26825 let r: Rc<dyn Display> = Rc::from(b);
26835 let r: Rc<dyn Debug> = Rc::from(b);
26843 let r: Rc<[u32]> = Rc::from(v);
26852 let r1: Rc<dyn Any> = Rc::new(i32::MAX);
26853 let r2: Rc<dyn Any> = Rc::new("abc");
26859 assert_eq!(r1i32.unwrap(), Rc::new(i32::MAX));
26865 assert_eq!(r2str.unwrap(), Rc::new("abc"));
26871 let r: Rc<[u32]> = Rc::from(v);
26873 let a: Result<Rc<[u32; 3]>, _> = r.clone().try_into();
26876 let a: Result<Rc<[u32; 2]>, _> = r.clone().try_into();
26886 let zero_refs = Rc::new_cyclic(|inner| {
26892 assert_eq!(Rc::strong_count(&zero_refs), 1);
26893 assert_eq!(Rc::weak_count(&zero_refs), 0);
26904 let one_ref = Rc::new_cyclic(|inner| {
26910 assert_eq!(Rc::strong_count(&one_ref), 1);
26911 assert_eq!(Rc::weak_count(&one_ref), 1);
26914 assert!(Rc::ptr_eq(&one_ref, &one_ref2));
26927 let two_refs = Rc::new_cyclic(|inner| {
26933 assert_eq!(Rc::strong_count(&two_refs), 1);
26934 assert_eq!(Rc::weak_count(&two_refs), 2);
26937 assert!(Rc::ptr_eq(&two_refs, &two_ref3));
26940 assert!(Rc::ptr_eq(&two_refs, &two_ref2));
26942 assert_eq!(Rc::strong_count(&two_refs), 3);
26943 assert_eq!(Rc::weak_count(&two_refs), 2);
35565 use crate::rc::Rc;
36793 map.insert(Rc::new(0), 1);