From ddbd2b5e7e42d6be11194abef4f5d12ec11aa41e Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Sat, 18 Mar 2017 15:30:55 +0100 Subject: [PATCH] Implement rc::Weak::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 ed174d7c32991..825578304e3ed 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -1008,6 +1008,25 @@ impl Weak { } impl Weak { + /// Creates another pointer to the same inner value, increasing the + /// weak reference count. + /// + /// # Examples + /// + /// ``` + /// use std::rc::Rc; + /// + /// let weak_five = Rc::downgrade(&Rc::new(5)); + /// + /// let _other_weak_five = weak_five.new_ref(); + /// // weak_five and _other_weak_five point to the same value. + /// ``` + #[inline] + fn new_ref(&self) -> Weak { + self.inc_weak(); + Weak { ptr: self.ptr } + } + /// Upgrades the `Weak` pointer to an [`Rc`][rc], if possible. /// /// Returns [`None`][option] if the strong count has reached zero and the @@ -1093,6 +1112,8 @@ impl Clone for Weak { /// /// This creates another pointer to the same inner value, increasing the /// weak reference count. + /// This is equivallent to calling `new_ref` with the added genericity of being callable + /// through the `Clone` trait. /// /// # Examples /// @@ -1105,8 +1126,7 @@ impl Clone for Weak { /// ``` #[inline] fn clone(&self) -> Weak { - self.inc_weak(); - Weak { ptr: self.ptr } + self.new_ref() } }