33//!
44//! The type [`Rc<T>`][`Rc`] provides shared ownership of a value of type `T`,
55//! allocated in the heap. Invoking [`clone`][clone] on [`Rc`] produces a new
6- //! pointer to the same value in the heap. When the last [`Rc`] pointer to a
7- //! given value is destroyed, the pointed-to value is also destroyed.
6+ //! pointer to the same allocation in the heap. When the last [`Rc`] pointer to a
7+ //! given allocation is destroyed, the pointed-to value is also destroyed.
88//!
99//! Shared references in Rust disallow mutation by default, and [`Rc`]
1010//! is no exception: you cannot generally obtain a mutable reference to
2121//!
2222//! The [`downgrade`][downgrade] method can be used to create a non-owning
2323//! [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
24- //! to an [`Rc`], but this will return [`None`] if the value has
24+ //! to an [`Rc`], but this will return [`None`] if the allocation has
2525//! already been dropped.
2626//!
2727//! A cycle between [`Rc`] pointers will never be deallocated. For this reason,
4141//! Rc::downgrade(&my_rc);
4242//! ```
4343//!
44- //! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the value may have
44+ //! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the allocation may have
4545//! already been destroyed.
4646//!
4747//! # Cloning references
9393//! );
9494//!
9595//! // Create `Gadget`s belonging to `gadget_owner`. Cloning the `Rc<Owner>`
96- //! // value gives us a new pointer to the same `Owner` value , incrementing
96+ //! // gives us a new pointer to the same `Owner` allocation , incrementing
9797//! // the reference count in the process.
9898//! let gadget1 = Gadget {
9999//! id: 1,
110110//! // Despite dropping `gadget_owner`, we're still able to print out the name
111111//! // of the `Owner` of the `Gadget`s. This is because we've only dropped a
112112//! // single `Rc<Owner>`, not the `Owner` it points to. As long as there are
113- //! // other `Rc<Owner>` values pointing at the same `Owner`, it will remain
113+ //! // other `Rc<Owner>` pointing at the same `Owner`, it will remain
114114//! // allocated. The field projection `gadget1.owner.name` works because
115115//! // `Rc<Owner>` automatically dereferences to `Owner`.
116116//! println!("Gadget {} owned by {}", gadget1.id, gadget1.owner.name);
124124//!
125125//! If our requirements change, and we also need to be able to traverse from
126126//! `Owner` to `Gadget`, we will run into problems. An [`Rc`] pointer from `Owner`
127- //! to `Gadget` introduces a cycle between the values . This means that their
128- //! reference counts can never reach 0, and the values will remain allocated
129- //! forever: a memory leak. In order to get around this, we can use [`Weak`]
127+ //! to `Gadget` introduces a cycle. This means that their
128+ //! reference counts can never reach 0, and the allocation will never be destroyed:
129+ //! a memory leak. In order to get around this, we can use [`Weak`]
130130//! pointers.
131131//!
132132//! Rust actually makes it somewhat difficult to produce this loop in the first
193193//! for gadget_weak in gadget_owner.gadgets.borrow().iter() {
194194//!
195195//! // `gadget_weak` is a `Weak<Gadget>`. Since `Weak` pointers can't
196- //! // guarantee the value is still allocated , we need to call
196+ //! // guarantee the allocation still exists , we need to call
197197//! // `upgrade`, which returns an `Option<Rc<Gadget>>`.
198198//! //
199- //! // In this case we know the value still exists, so we simply
199+ //! // In this case we know the allocation still exists, so we simply
200200//! // `unwrap` the `Option`. In a more complicated program, you might
201201//! // need graceful error handling for a `None` result.
202202//!
@@ -604,7 +604,7 @@ impl<T: ?Sized> Rc<T> {
604604 unsafe { NonNull :: new_unchecked ( Rc :: into_raw ( this) as * mut _ ) }
605605 }
606606
607- /// Creates a new [`Weak`][weak] pointer to this value .
607+ /// Creates a new [`Weak`][weak] pointer to this allocation .
608608 ///
609609 /// [weak]: struct.Weak.html
610610 ///
@@ -625,7 +625,7 @@ impl<T: ?Sized> Rc<T> {
625625 Weak { ptr : this. ptr }
626626 }
627627
628- /// Gets the number of [`Weak`][weak] pointers to this value .
628+ /// Gets the number of [`Weak`][weak] pointers to this allocation .
629629 ///
630630 /// [weak]: struct.Weak.html
631631 ///
@@ -645,7 +645,7 @@ impl<T: ?Sized> Rc<T> {
645645 this. weak ( ) - 1
646646 }
647647
648- /// Gets the number of strong (`Rc`) pointers to this value .
648+ /// Gets the number of strong (`Rc`) pointers to this allocation .
649649 ///
650650 /// # Examples
651651 ///
@@ -664,16 +664,16 @@ impl<T: ?Sized> Rc<T> {
664664 }
665665
666666 /// Returns `true` if there are no other `Rc` or [`Weak`][weak] pointers to
667- /// this inner value .
667+ /// this allocation .
668668 ///
669669 /// [weak]: struct.Weak.html
670670 #[ inline]
671671 fn is_unique ( this : & Self ) -> bool {
672672 Rc :: weak_count ( this) == 0 && Rc :: strong_count ( this) == 1
673673 }
674674
675- /// Returns a mutable reference to the inner value , if there are
676- /// no other `Rc` or [`Weak`][weak] pointers to the same value .
675+ /// Returns a mutable reference into the given `Rc` , if there are
676+ /// no other `Rc` or [`Weak`][weak] pointers to the same allocation .
677677 ///
678678 /// Returns [`None`] otherwise, because it is not safe to
679679 /// mutate a shared value.
@@ -710,7 +710,7 @@ impl<T: ?Sized> Rc<T> {
710710 }
711711 }
712712
713- /// Returns a mutable reference to the inner value ,
713+ /// Returns a mutable reference into the given `Rc` ,
714714 /// without any check.
715715 ///
716716 /// See also [`get_mut`], which is safe and does appropriate checks.
@@ -719,7 +719,7 @@ impl<T: ?Sized> Rc<T> {
719719 ///
720720 /// # Safety
721721 ///
722- /// Any other `Rc` or [`Weak`] pointers to the same value must not be dereferenced
722+ /// Any other `Rc` or [`Weak`] pointers to the same allocation must not be dereferenced
723723 /// for the duration of the returned borrow.
724724 /// This is trivially the case if no such pointers exist,
725725 /// for example immediately after `Rc::new`.
@@ -745,8 +745,8 @@ impl<T: ?Sized> Rc<T> {
745745
746746 #[ inline]
747747 #[ stable( feature = "ptr_eq" , since = "1.17.0" ) ]
748- /// Returns `true` if the two `Rc`s point to the same value (not
749- /// just values that compare as equal ).
748+ /// Returns `true` if the two `Rc`s point to the same allocation
749+ /// (in a vein similar to [`ptr::eq`] ).
750750 ///
751751 /// # Examples
752752 ///
@@ -760,6 +760,8 @@ impl<T: ?Sized> Rc<T> {
760760 /// assert!(Rc::ptr_eq(&five, &same_five));
761761 /// assert!(!Rc::ptr_eq(&five, &other_five));
762762 /// ```
763+ ///
764+ /// [`ptr::eq`]: ../../std/ptr/fn.eq.html
763765 pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
764766 this. ptr . as_ptr ( ) == other. ptr . as_ptr ( )
765767 }
@@ -768,12 +770,12 @@ impl<T: ?Sized> Rc<T> {
768770impl < T : Clone > Rc < T > {
769771 /// Makes a mutable reference into the given `Rc`.
770772 ///
771- /// If there are other `Rc` pointers to the same value , then `make_mut` will
772- /// [`clone`] the inner value to ensure unique ownership. This is also
773+ /// If there are other `Rc` pointers to the same allocation , then `make_mut` will
774+ /// [`clone`] the inner value to a new allocation to ensure unique ownership. This is also
773775 /// referred to as clone-on-write.
774776 ///
775- /// If there are no other `Rc` pointers to this value , then [`Weak`]
776- /// pointers to this value will be disassociated.
777+ /// If there are no other `Rc` pointers to this allocation , then [`Weak`]
778+ /// pointers to this allocation will be disassociated.
777779 ///
778780 /// See also [`get_mut`], which will fail rather than cloning.
779781 ///
@@ -794,7 +796,7 @@ impl<T: Clone> Rc<T> {
794796 /// *Rc::make_mut(&mut data) += 1; // Won't clone anything
795797 /// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
796798 ///
797- /// // Now `data` and `other_data` point to different values .
799+ /// // Now `data` and `other_data` point to different allocations .
798800 /// assert_eq!(*data, 8);
799801 /// assert_eq!(*other_data, 12);
800802 /// ```
@@ -837,7 +839,7 @@ impl<T: Clone> Rc<T> {
837839 // returned is the *only* pointer that will ever be returned to T. Our
838840 // reference count is guaranteed to be 1 at this point, and we required
839841 // the `Rc<T>` itself to be `mut`, so we're returning the only possible
840- // reference to the inner value .
842+ // reference to the allocation .
841843 unsafe {
842844 & mut this. ptr . as_mut ( ) . value
843845 }
@@ -1111,7 +1113,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
11111113impl < T : ?Sized > Clone for Rc < T > {
11121114 /// Makes a clone of the `Rc` pointer.
11131115 ///
1114- /// This creates another pointer to the same inner value , increasing the
1116+ /// This creates another pointer to the same allocation , increasing the
11151117 /// strong reference count.
11161118 ///
11171119 /// # Examples
@@ -1189,9 +1191,11 @@ impl<T: ?Sized + Eq> RcEqIdent<T> for Rc<T> {
11891191impl < T : ?Sized + PartialEq > PartialEq for Rc < T > {
11901192 /// Equality for two `Rc`s.
11911193 ///
1192- /// Two `Rc`s are equal if their inner values are equal.
1194+ /// Two `Rc`s are equal if their inner values are equal, even if they are
1195+ /// stored in different allocation.
11931196 ///
1194- /// If `T` also implements `Eq`, two `Rc`s that point to the same value are
1197+ /// If `T` also implements `Eq` (implying reflexivity of equality),
1198+ /// two `Rc`s that point to the same allocation are
11951199 /// always equal.
11961200 ///
11971201 /// # Examples
@@ -1212,7 +1216,8 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
12121216 ///
12131217 /// Two `Rc`s are unequal if their inner values are unequal.
12141218 ///
1215- /// If `T` also implements `Eq`, two `Rc`s that point to the same value are
1219+ /// If `T` also implements `Eq` (implying reflexivity of equality),
1220+ /// two `Rc`s that point to the same allocation are
12161221 /// never unequal.
12171222 ///
12181223 /// # Examples
@@ -1541,16 +1546,16 @@ impl<'a, T: 'a + Clone> RcFromIter<&'a T, slice::Iter<'a, T>> for Rc<[T]> {
15411546}
15421547
15431548/// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
1544- /// managed value . The value is accessed by calling [`upgrade`] on the `Weak`
1549+ /// managed allocation . The allocation is accessed by calling [`upgrade`] on the `Weak`
15451550/// pointer, which returns an [`Option`]`<`[`Rc`]`<T>>`.
15461551///
15471552/// Since a `Weak` reference does not count towards ownership, it will not
1548- /// prevent the inner value from being dropped, and `Weak` itself makes no
1553+ /// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
15491554/// guarantees about the value still being present and may return [`None`]
15501555/// when [`upgrade`]d.
15511556///
1552- /// A `Weak` pointer is useful for keeping a temporary reference to the value
1553- /// within [`Rc`] without extending its lifetime. It is also used to prevent
1557+ /// A `Weak` pointer is useful for keeping a temporary reference to the allocation
1558+ /// managed by [`Rc`] without extending its lifetime. It is also used to prevent
15541559/// circular references between [`Rc`] pointers, since mutual owning references
15551560/// would never allow either [`Rc`] to be dropped. For example, a tree could
15561561/// have strong [`Rc`] pointers from parent nodes to children, and `Weak`
@@ -1751,9 +1756,9 @@ pub(crate) fn is_dangling<T: ?Sized>(ptr: NonNull<T>) -> bool {
17511756
17521757impl < T : ?Sized > Weak < T > {
17531758 /// Attempts to upgrade the `Weak` pointer to an [`Rc`], extending
1754- /// the lifetime of the value if successful.
1759+ /// the lifetime of the allocation if successful.
17551760 ///
1756- /// Returns [`None`] if the value has since been dropped.
1761+ /// Returns [`None`] if the value stored in the allocation has since been dropped.
17571762 ///
17581763 /// [`Rc`]: struct.Rc.html
17591764 /// [`None`]: ../../std/option/enum.Option.html
@@ -1787,7 +1792,7 @@ impl<T: ?Sized> Weak<T> {
17871792 }
17881793 }
17891794
1790- /// Gets the number of strong (`Rc`) pointers pointing to this value .
1795+ /// Gets the number of strong (`Rc`) pointers pointing to this allocation .
17911796 ///
17921797 /// If `self` was created using [`Weak::new`], this will return 0.
17931798 ///
@@ -1801,11 +1806,11 @@ impl<T: ?Sized> Weak<T> {
18011806 }
18021807 }
18031808
1804- /// Gets the number of `Weak` pointers pointing to this value .
1809+ /// Gets the number of `Weak` pointers pointing to this allocation .
18051810 ///
18061811 /// If `self` was created using [`Weak::new`], this will return `None`. If
18071812 /// not, the returned value is at least 1, since `self` still points to the
1808- /// value .
1813+ /// allocation .
18091814 ///
18101815 /// [`Weak::new`]: #method.new
18111816 #[ unstable( feature = "weak_counts" , issue = "57977" ) ]
@@ -1830,14 +1835,14 @@ impl<T: ?Sized> Weak<T> {
18301835 }
18311836 }
18321837
1833- /// Returns `true` if the two `Weak`s point to the same value (not just
1834- /// values that compare as equal ), or if both don't point to any value
1838+ /// Returns `true` if the two `Weak`s point to the same allocation (similar to
1839+ /// [`ptr::eq`] ), or if both don't point to any allocation
18351840 /// (because they were created with `Weak::new()`).
18361841 ///
18371842 /// # Notes
18381843 ///
18391844 /// Since this compares pointers it means that `Weak::new()` will equal each
1840- /// other, even though they don't point to any value .
1845+ /// other, even though they don't point to any allocation .
18411846 ///
18421847 /// # Examples
18431848 ///
@@ -1869,6 +1874,8 @@ impl<T: ?Sized> Weak<T> {
18691874 /// let third = Rc::downgrade(&third_rc);
18701875 /// assert!(!first.ptr_eq(&third));
18711876 /// ```
1877+ ///
1878+ /// [`ptr::eq`]: ../../std/ptr/fn.eq.html
18721879 #[ inline]
18731880 #[ stable( feature = "weak_ptr_eq" , since = "1.39.0" ) ]
18741881 pub fn ptr_eq ( & self , other : & Self ) -> bool {
@@ -1918,7 +1925,7 @@ impl<T: ?Sized> Drop for Weak<T> {
19181925
19191926#[ stable( feature = "rc_weak" , since = "1.4.0" ) ]
19201927impl < T : ?Sized > Clone for Weak < T > {
1921- /// Makes a clone of the `Weak` pointer that points to the same value .
1928+ /// Makes a clone of the `Weak` pointer that points to the same allocation .
19221929 ///
19231930 /// # Examples
19241931 ///
0 commit comments