3
3
//!
4
4
//! The type [`Rc<T>`][`Rc`] provides shared ownership of a value of type `T`,
5
5
//! 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.
8
8
//!
9
9
//! Shared references in Rust disallow mutation by default, and [`Rc`]
10
10
//! is no exception: you cannot generally obtain a mutable reference to
21
21
//!
22
22
//! The [`downgrade`][downgrade] method can be used to create a non-owning
23
23
//! [`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
25
25
//! already been dropped.
26
26
//!
27
27
//! A cycle between [`Rc`] pointers will never be deallocated. For this reason,
41
41
//! Rc::downgrade(&my_rc);
42
42
//! ```
43
43
//!
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
45
45
//! already been destroyed.
46
46
//!
47
47
//! # Cloning references
93
93
//! );
94
94
//!
95
95
//! // 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
97
97
//! // the reference count in the process.
98
98
//! let gadget1 = Gadget {
99
99
//! id: 1,
110
110
//! // Despite dropping `gadget_owner`, we're still able to print out the name
111
111
//! // of the `Owner` of the `Gadget`s. This is because we've only dropped a
112
112
//! // 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
114
114
//! // allocated. The field projection `gadget1.owner.name` works because
115
115
//! // `Rc<Owner>` automatically dereferences to `Owner`.
116
116
//! println!("Gadget {} owned by {}", gadget1.id, gadget1.owner.name);
124
124
//!
125
125
//! If our requirements change, and we also need to be able to traverse from
126
126
//! `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`]
130
130
//! pointers.
131
131
//!
132
132
//! Rust actually makes it somewhat difficult to produce this loop in the first
193
193
//! for gadget_weak in gadget_owner.gadgets.borrow().iter() {
194
194
//!
195
195
//! // `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
197
197
//! // `upgrade`, which returns an `Option<Rc<Gadget>>`.
198
198
//! //
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
200
200
//! // `unwrap` the `Option`. In a more complicated program, you might
201
201
//! // need graceful error handling for a `None` result.
202
202
//!
@@ -604,7 +604,7 @@ impl<T: ?Sized> Rc<T> {
604
604
unsafe { NonNull :: new_unchecked ( Rc :: into_raw ( this) as * mut _ ) }
605
605
}
606
606
607
- /// Creates a new [`Weak`][weak] pointer to this value .
607
+ /// Creates a new [`Weak`][weak] pointer to this allocation .
608
608
///
609
609
/// [weak]: struct.Weak.html
610
610
///
@@ -625,7 +625,7 @@ impl<T: ?Sized> Rc<T> {
625
625
Weak { ptr : this. ptr }
626
626
}
627
627
628
- /// Gets the number of [`Weak`][weak] pointers to this value .
628
+ /// Gets the number of [`Weak`][weak] pointers to this allocation .
629
629
///
630
630
/// [weak]: struct.Weak.html
631
631
///
@@ -645,7 +645,7 @@ impl<T: ?Sized> Rc<T> {
645
645
this. weak ( ) - 1
646
646
}
647
647
648
- /// Gets the number of strong (`Rc`) pointers to this value .
648
+ /// Gets the number of strong (`Rc`) pointers to this allocation .
649
649
///
650
650
/// # Examples
651
651
///
@@ -664,16 +664,16 @@ impl<T: ?Sized> Rc<T> {
664
664
}
665
665
666
666
/// Returns `true` if there are no other `Rc` or [`Weak`][weak] pointers to
667
- /// this inner value .
667
+ /// this allocation .
668
668
///
669
669
/// [weak]: struct.Weak.html
670
670
#[ inline]
671
671
fn is_unique ( this : & Self ) -> bool {
672
672
Rc :: weak_count ( this) == 0 && Rc :: strong_count ( this) == 1
673
673
}
674
674
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 .
677
677
///
678
678
/// Returns [`None`] otherwise, because it is not safe to
679
679
/// mutate a shared value.
@@ -710,7 +710,7 @@ impl<T: ?Sized> Rc<T> {
710
710
}
711
711
}
712
712
713
- /// Returns a mutable reference to the inner value ,
713
+ /// Returns a mutable reference into the given `Rc` ,
714
714
/// without any check.
715
715
///
716
716
/// See also [`get_mut`], which is safe and does appropriate checks.
@@ -719,7 +719,7 @@ impl<T: ?Sized> Rc<T> {
719
719
///
720
720
/// # Safety
721
721
///
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
723
723
/// for the duration of the returned borrow.
724
724
/// This is trivially the case if no such pointers exist,
725
725
/// for example immediately after `Rc::new`.
@@ -745,8 +745,8 @@ impl<T: ?Sized> Rc<T> {
745
745
746
746
#[ inline]
747
747
#[ 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`] ).
750
750
///
751
751
/// # Examples
752
752
///
@@ -760,6 +760,8 @@ impl<T: ?Sized> Rc<T> {
760
760
/// assert!(Rc::ptr_eq(&five, &same_five));
761
761
/// assert!(!Rc::ptr_eq(&five, &other_five));
762
762
/// ```
763
+ ///
764
+ /// [`ptr::eq`]: ../../std/ptr/fn.eq.html
763
765
pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
764
766
this. ptr . as_ptr ( ) == other. ptr . as_ptr ( )
765
767
}
@@ -768,12 +770,12 @@ impl<T: ?Sized> Rc<T> {
768
770
impl < T : Clone > Rc < T > {
769
771
/// Makes a mutable reference into the given `Rc`.
770
772
///
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
773
775
/// referred to as clone-on-write.
774
776
///
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.
777
779
///
778
780
/// See also [`get_mut`], which will fail rather than cloning.
779
781
///
@@ -794,7 +796,7 @@ impl<T: Clone> Rc<T> {
794
796
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
795
797
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
796
798
///
797
- /// // Now `data` and `other_data` point to different values .
799
+ /// // Now `data` and `other_data` point to different allocations .
798
800
/// assert_eq!(*data, 8);
799
801
/// assert_eq!(*other_data, 12);
800
802
/// ```
@@ -837,7 +839,7 @@ impl<T: Clone> Rc<T> {
837
839
// returned is the *only* pointer that will ever be returned to T. Our
838
840
// reference count is guaranteed to be 1 at this point, and we required
839
841
// 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 .
841
843
unsafe {
842
844
& mut this. ptr . as_mut ( ) . value
843
845
}
@@ -1111,7 +1113,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
1111
1113
impl < T : ?Sized > Clone for Rc < T > {
1112
1114
/// Makes a clone of the `Rc` pointer.
1113
1115
///
1114
- /// This creates another pointer to the same inner value , increasing the
1116
+ /// This creates another pointer to the same allocation , increasing the
1115
1117
/// strong reference count.
1116
1118
///
1117
1119
/// # Examples
@@ -1189,9 +1191,11 @@ impl<T: ?Sized + Eq> RcEqIdent<T> for Rc<T> {
1189
1191
impl < T : ?Sized + PartialEq > PartialEq for Rc < T > {
1190
1192
/// Equality for two `Rc`s.
1191
1193
///
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.
1193
1196
///
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
1195
1199
/// always equal.
1196
1200
///
1197
1201
/// # Examples
@@ -1212,7 +1216,8 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
1212
1216
///
1213
1217
/// Two `Rc`s are unequal if their inner values are unequal.
1214
1218
///
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
1216
1221
/// never unequal.
1217
1222
///
1218
1223
/// # Examples
@@ -1541,16 +1546,16 @@ impl<'a, T: 'a + Clone> RcFromIter<&'a T, slice::Iter<'a, T>> for Rc<[T]> {
1541
1546
}
1542
1547
1543
1548
/// `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`
1545
1550
/// pointer, which returns an [`Option`]`<`[`Rc`]`<T>>`.
1546
1551
///
1547
1552
/// 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
1549
1554
/// guarantees about the value still being present and may return [`None`]
1550
1555
/// when [`upgrade`]d.
1551
1556
///
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
1554
1559
/// circular references between [`Rc`] pointers, since mutual owning references
1555
1560
/// would never allow either [`Rc`] to be dropped. For example, a tree could
1556
1561
/// 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 {
1751
1756
1752
1757
impl < T : ?Sized > Weak < T > {
1753
1758
/// 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.
1755
1760
///
1756
- /// Returns [`None`] if the value has since been dropped.
1761
+ /// Returns [`None`] if the value stored in the allocation has since been dropped.
1757
1762
///
1758
1763
/// [`Rc`]: struct.Rc.html
1759
1764
/// [`None`]: ../../std/option/enum.Option.html
@@ -1787,7 +1792,7 @@ impl<T: ?Sized> Weak<T> {
1787
1792
}
1788
1793
}
1789
1794
1790
- /// Gets the number of strong (`Rc`) pointers pointing to this value .
1795
+ /// Gets the number of strong (`Rc`) pointers pointing to this allocation .
1791
1796
///
1792
1797
/// If `self` was created using [`Weak::new`], this will return 0.
1793
1798
///
@@ -1801,11 +1806,11 @@ impl<T: ?Sized> Weak<T> {
1801
1806
}
1802
1807
}
1803
1808
1804
- /// Gets the number of `Weak` pointers pointing to this value .
1809
+ /// Gets the number of `Weak` pointers pointing to this allocation .
1805
1810
///
1806
1811
/// If `self` was created using [`Weak::new`], this will return `None`. If
1807
1812
/// not, the returned value is at least 1, since `self` still points to the
1808
- /// value .
1813
+ /// allocation .
1809
1814
///
1810
1815
/// [`Weak::new`]: #method.new
1811
1816
#[ unstable( feature = "weak_counts" , issue = "57977" ) ]
@@ -1830,14 +1835,14 @@ impl<T: ?Sized> Weak<T> {
1830
1835
}
1831
1836
}
1832
1837
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
1835
1840
/// (because they were created with `Weak::new()`).
1836
1841
///
1837
1842
/// # Notes
1838
1843
///
1839
1844
/// 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 .
1841
1846
///
1842
1847
/// # Examples
1843
1848
///
@@ -1869,6 +1874,8 @@ impl<T: ?Sized> Weak<T> {
1869
1874
/// let third = Rc::downgrade(&third_rc);
1870
1875
/// assert!(!first.ptr_eq(&third));
1871
1876
/// ```
1877
+ ///
1878
+ /// [`ptr::eq`]: ../../std/ptr/fn.eq.html
1872
1879
#[ inline]
1873
1880
#[ stable( feature = "weak_ptr_eq" , since = "1.39.0" ) ]
1874
1881
pub fn ptr_eq ( & self , other : & Self ) -> bool {
@@ -1918,7 +1925,7 @@ impl<T: ?Sized> Drop for Weak<T> {
1918
1925
1919
1926
#[ stable( feature = "rc_weak" , since = "1.4.0" ) ]
1920
1927
impl < 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 .
1922
1929
///
1923
1930
/// # Examples
1924
1931
///
0 commit comments