From 5903ed4aa3ddb825f8b9b3412b3240f07193b711 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Sat, 18 Mar 2017 15:07:02 +0100 Subject: [PATCH] Implement Rc::new_ref. --- src/liballoc/rc.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index eb449b2660679..ed174d7c32991 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -443,6 +443,25 @@ impl Rc { } impl Rc { + /// Creates another pointer to the same inner value, increasing the + /// strong reference count. + /// + /// # Examples + /// + /// ``` + /// use std::rc::Rc; + /// + /// let five = Rc::new(5); + /// + /// let _other_five = five.clone(); + /// // _other_five and five point to the same value. + /// ``` + #[inline] + fn new_ref(&self) -> Rc { + self.inc_strong(); + Rc { ptr: self.ptr } + } + /// Creates a new [`Weak`][weak] pointer to this value. /// /// [weak]: struct.Weak.html @@ -702,6 +721,8 @@ impl Clone for Rc { /// /// This creates another pointer to the same inner value, increasing the /// strong reference count. + /// This is equivallent to calling `new_ref` with the added genericity of being callable + /// through the `Clone` trait. /// /// # Examples /// @@ -714,8 +735,7 @@ impl Clone for Rc { /// ``` #[inline] fn clone(&self) -> Rc { - self.inc_strong(); - Rc { ptr: self.ptr } + self.new_ref() } }