Skip to content

Commit cedd2eb

Browse files
Rollup merge of rust-lang#42137 - nical:doc-clone, r=BurntSushi
Update to Rc and Arc documentation to favor the Rc::clone(&ptr) syntax. This is a followup of the discussion in rust-lang/rfcs#1954. The solution chosen by the core team to address the problem tackled by the [the RFC](rust-lang/rfcs#1954) was to make the function call syntax Rc::clone(&foo) the idiomatic way to clone a reference counted pointer (over the method call syntax foo.clone()). This change updates the documentation of Rc, Arc and their respective Weak pointers to reflect this decision and bring more exposure to the existence of the function call syntax.
2 parents f476dbf + dec23d4 commit cedd2eb

File tree

2 files changed

+64
-28
lines changed

2 files changed

+64
-28
lines changed

src/liballoc/arc.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
9191
/// strong `Arc` pointers from parent nodes to children, and [`Weak`][weak]
9292
/// pointers from children back to their parents.
9393
///
94+
/// # Cloning references
95+
///
96+
/// Creating a new reference from an existing reference counted pointer is done using the
97+
/// `Clone` trait implemented for [`Arc<T>`][`arc`] and [`Weak<T>`][`weak`].
98+
///
99+
/// ```
100+
/// use std::sync::Arc;
101+
/// let foo = Arc::new(vec![1.0, 2.0, 3.0]);
102+
/// // The two syntaxes below are equivalent.
103+
/// let a = foo.clone();
104+
/// let b = Arc::clone(&foo);
105+
/// // a and b both point to the same memory location as foo.
106+
/// ```
107+
///
108+
/// The `Arc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
109+
/// the meaning of the code. In the example above, this syntax makes it easier to see that
110+
/// this code is creating a new reference rather than copying the whole content of foo.
111+
///
94112
/// ## `Deref` behavior
95113
///
96114
/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
@@ -138,7 +156,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
138156
/// let five = Arc::new(5);
139157
///
140158
/// for _ in 0..10 {
141-
/// let five = five.clone();
159+
/// let five = Arc::clone(&five);
142160
///
143161
/// thread::spawn(move || {
144162
/// println!("{:?}", five);
@@ -158,7 +176,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
158176
/// let val = Arc::new(AtomicUsize::new(5));
159177
///
160178
/// for _ in 0..10 {
161-
/// let val = val.clone();
179+
/// let val = Arc::clone(&val);
162180
///
163181
/// thread::spawn(move || {
164182
/// let v = val.fetch_add(1, Ordering::SeqCst);
@@ -282,7 +300,7 @@ impl<T> Arc<T> {
282300
/// assert_eq!(Arc::try_unwrap(x), Ok(3));
283301
///
284302
/// let x = Arc::new(4);
285-
/// let _y = x.clone();
303+
/// let _y = Arc::clone(&x);
286304
/// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
287305
/// ```
288306
#[inline]
@@ -451,7 +469,7 @@ impl<T: ?Sized> Arc<T> {
451469
/// use std::sync::Arc;
452470
///
453471
/// let five = Arc::new(5);
454-
/// let _also_five = five.clone();
472+
/// let _also_five = Arc::clone(&five);
455473
///
456474
/// // This assertion is deterministic because we haven't shared
457475
/// // the `Arc` between threads.
@@ -499,7 +517,7 @@ impl<T: ?Sized> Arc<T> {
499517
/// use std::sync::Arc;
500518
///
501519
/// let five = Arc::new(5);
502-
/// let same_five = five.clone();
520+
/// let same_five = Arc::clone(&five);
503521
/// let other_five = Arc::new(5);
504522
///
505523
/// assert!(Arc::ptr_eq(&five, &same_five));
@@ -524,7 +542,7 @@ impl<T: ?Sized> Clone for Arc<T> {
524542
///
525543
/// let five = Arc::new(5);
526544
///
527-
/// five.clone();
545+
/// Arc::clone(&five);
528546
/// ```
529547
#[inline]
530548
fn clone(&self) -> Arc<T> {
@@ -591,7 +609,7 @@ impl<T: Clone> Arc<T> {
591609
/// let mut data = Arc::new(5);
592610
///
593611
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
594-
/// let mut other_data = data.clone(); // Won't clone inner data
612+
/// let mut other_data = Arc::clone(&data); // Won't clone inner data
595613
/// *Arc::make_mut(&mut data) += 1; // Clones inner data
596614
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
597615
/// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
@@ -679,7 +697,7 @@ impl<T: ?Sized> Arc<T> {
679697
/// *Arc::get_mut(&mut x).unwrap() = 4;
680698
/// assert_eq!(*x, 4);
681699
///
682-
/// let _y = x.clone();
700+
/// let _y = Arc::clone(&x);
683701
/// assert!(Arc::get_mut(&mut x).is_none());
684702
/// ```
685703
#[inline]
@@ -751,7 +769,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
751769
/// }
752770
///
753771
/// let foo = Arc::new(Foo);
754-
/// let foo2 = foo.clone();
772+
/// let foo2 = Arc::clone(&foo);
755773
///
756774
/// drop(foo); // Doesn't print anything
757775
/// drop(foo2); // Prints "dropped!"
@@ -903,11 +921,11 @@ impl<T: ?Sized> Clone for Weak<T> {
903921
/// # Examples
904922
///
905923
/// ```
906-
/// use std::sync::Arc;
924+
/// use std::sync::{Arc, Weak};
907925
///
908926
/// let weak_five = Arc::downgrade(&Arc::new(5));
909927
///
910-
/// weak_five.clone();
928+
/// Weak::clone(&weak_five);
911929
/// ```
912930
#[inline]
913931
fn clone(&self) -> Weak<T> {
@@ -956,7 +974,7 @@ impl<T: ?Sized> Drop for Weak<T> {
956974
/// # Examples
957975
///
958976
/// ```
959-
/// use std::sync::Arc;
977+
/// use std::sync::{Arc, Weak};
960978
///
961979
/// struct Foo;
962980
///
@@ -968,7 +986,7 @@ impl<T: ?Sized> Drop for Weak<T> {
968986
///
969987
/// let foo = Arc::new(Foo);
970988
/// let weak_foo = Arc::downgrade(&foo);
971-
/// let other_weak_foo = weak_foo.clone();
989+
/// let other_weak_foo = Weak::clone(&weak_foo);
972990
///
973991
/// drop(weak_foo); // Doesn't print anything
974992
/// drop(foo); // Prints "dropped!"

src/liballoc/rc.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@
5555
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the value may have
5656
//! already been destroyed.
5757
//!
58+
//! # Cloning references
59+
//!
60+
//! Creating a new reference from an existing reference counted pointer is done using the
61+
//! `Clone` trait implemented for [`Rc<T>`][`Rc`] and [`Weak<T>`][`Weak`].
62+
//!
63+
//! ```
64+
//! use std::rc::Rc;
65+
//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
66+
//! // The two syntaxes below are equivalent.
67+
//! let a = foo.clone();
68+
//! let b = Rc::clone(&foo);
69+
//! // a and b both point to the same memory location as foo.
70+
//! ```
71+
//!
72+
//! The `Rc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
73+
//! the meaning of the code. In the example above, this syntax makes it easier to see that
74+
//! this code is creating a new reference rather than copying the whole content of foo.
75+
//!
5876
//! # Examples
5977
//!
6078
//! Consider a scenario where a set of `Gadget`s are owned by a given `Owner`.
@@ -90,11 +108,11 @@
90108
//! // the reference count in the process.
91109
//! let gadget1 = Gadget {
92110
//! id: 1,
93-
//! owner: gadget_owner.clone(),
111+
//! owner: Rc::clone(&gadget_owner),
94112
//! };
95113
//! let gadget2 = Gadget {
96114
//! id: 2,
97-
//! owner: gadget_owner.clone(),
115+
//! owner: Rc::clone(&gadget_owner),
98116
//! };
99117
//!
100118
//! // Dispose of our local variable `gadget_owner`.
@@ -163,13 +181,13 @@
163181
//! let gadget1 = Rc::new(
164182
//! Gadget {
165183
//! id: 1,
166-
//! owner: gadget_owner.clone(),
184+
//! owner: Rc::clone(&gadget_owner),
167185
//! }
168186
//! );
169187
//! let gadget2 = Rc::new(
170188
//! Gadget {
171189
//! id: 2,
172-
//! owner: gadget_owner.clone(),
190+
//! owner: Rc::clone(&gadget_owner),
173191
//! }
174192
//! );
175193
//!
@@ -316,7 +334,7 @@ impl<T> Rc<T> {
316334
/// assert_eq!(Rc::try_unwrap(x), Ok(3));
317335
///
318336
/// let x = Rc::new(4);
319-
/// let _y = x.clone();
337+
/// let _y = Rc::clone(&x);
320338
/// assert_eq!(*Rc::try_unwrap(x).unwrap_err(), 4);
321339
/// ```
322340
#[inline]
@@ -508,7 +526,7 @@ impl<T: ?Sized> Rc<T> {
508526
/// use std::rc::Rc;
509527
///
510528
/// let five = Rc::new(5);
511-
/// let _also_five = five.clone();
529+
/// let _also_five = Rc::clone(&five);
512530
///
513531
/// assert_eq!(2, Rc::strong_count(&five));
514532
/// ```
@@ -550,7 +568,7 @@ impl<T: ?Sized> Rc<T> {
550568
/// *Rc::get_mut(&mut x).unwrap() = 4;
551569
/// assert_eq!(*x, 4);
552570
///
553-
/// let _y = x.clone();
571+
/// let _y = Rc::clone(&x);
554572
/// assert!(Rc::get_mut(&mut x).is_none());
555573
/// ```
556574
#[inline]
@@ -576,7 +594,7 @@ impl<T: ?Sized> Rc<T> {
576594
/// use std::rc::Rc;
577595
///
578596
/// let five = Rc::new(5);
579-
/// let same_five = five.clone();
597+
/// let same_five = Rc::clone(&five);
580598
/// let other_five = Rc::new(5);
581599
///
582600
/// assert!(Rc::ptr_eq(&five, &same_five));
@@ -608,7 +626,7 @@ impl<T: Clone> Rc<T> {
608626
/// let mut data = Rc::new(5);
609627
///
610628
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
611-
/// let mut other_data = data.clone(); // Won't clone inner data
629+
/// let mut other_data = Rc::clone(&data); // Won't clone inner data
612630
/// *Rc::make_mut(&mut data) += 1; // Clones inner data
613631
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
614632
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
@@ -680,7 +698,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
680698
/// }
681699
///
682700
/// let foo = Rc::new(Foo);
683-
/// let foo2 = foo.clone();
701+
/// let foo2 = Rc::clone(&foo);
684702
///
685703
/// drop(foo); // Doesn't print anything
686704
/// drop(foo2); // Prints "dropped!"
@@ -720,7 +738,7 @@ impl<T: ?Sized> Clone for Rc<T> {
720738
///
721739
/// let five = Rc::new(5);
722740
///
723-
/// five.clone();
741+
/// Rc::clone(&five);
724742
/// ```
725743
#[inline]
726744
fn clone(&self) -> Rc<T> {
@@ -1050,7 +1068,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10501068
/// # Examples
10511069
///
10521070
/// ```
1053-
/// use std::rc::Rc;
1071+
/// use std::rc::{Rc, Weak};
10541072
///
10551073
/// struct Foo;
10561074
///
@@ -1062,7 +1080,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10621080
///
10631081
/// let foo = Rc::new(Foo);
10641082
/// let weak_foo = Rc::downgrade(&foo);
1065-
/// let other_weak_foo = weak_foo.clone();
1083+
/// let other_weak_foo = Weak::clone(&weak_foo);
10661084
///
10671085
/// drop(weak_foo); // Doesn't print anything
10681086
/// drop(foo); // Prints "dropped!"
@@ -1090,11 +1108,11 @@ impl<T: ?Sized> Clone for Weak<T> {
10901108
/// # Examples
10911109
///
10921110
/// ```
1093-
/// use std::rc::Rc;
1111+
/// use std::rc::{Rc, Weak};
10941112
///
10951113
/// let weak_five = Rc::downgrade(&Rc::new(5));
10961114
///
1097-
/// weak_five.clone();
1115+
/// Weak::clone(&weak_five);
10981116
/// ```
10991117
#[inline]
11001118
fn clone(&self) -> Weak<T> {

0 commit comments

Comments
 (0)