From 7bcfe2ee1067d1304c9a2813c111f10a89984e45 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 28 Mar 2014 10:29:55 -0700 Subject: std: Remove `RefCell::get()` It's surprising that `RefCell::get()` is implicitly doing a clone on a value. This patch removes it and replaces all users with either `.borrow()` when we can autoderef, or `.borrow().clone()` when we cannot. --- src/libstd/cell.rs | 20 ++++---------------- src/libstd/option.rs | 5 +++-- 2 files changed, 7 insertions(+), 18 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index eb114e89510..ec6994728f7 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -176,21 +176,9 @@ impl RefCell { } } -impl RefCell { - /// Returns a copy of the contained value. - /// - /// # Failure - /// - /// Fails if the value is currently mutably borrowed. - #[inline] - pub fn get(&self) -> T { - (*self.borrow()).clone() - } -} - impl Clone for RefCell { fn clone(&self) -> RefCell { - RefCell::new(self.get()) + RefCell::new(self.borrow().clone()) } } @@ -216,7 +204,7 @@ impl<'b, T> Drop for Ref<'b, T> { impl<'b, T> Deref for Ref<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe{ &*self.parent.value.get() } + unsafe { &*self.parent.value.get() } } } @@ -236,14 +224,14 @@ impl<'b, T> Drop for RefMut<'b, T> { impl<'b, T> Deref for RefMut<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe{ &*self.parent.value.get() } + unsafe { &*self.parent.value.get() } } } impl<'b, T> DerefMut for RefMut<'b, T> { #[inline] fn deref_mut<'a>(&'a mut self) -> &'a mut T { - unsafe{ &mut *self.parent.value.get() } + unsafe { &mut *self.parent.value.get() } } } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index be1c87ba788..ce2c1378fc1 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -651,7 +651,8 @@ mod tests { impl ::ops::Drop for R { fn drop(&mut self) { let ii = &*self.i; - ii.set(ii.get() + 1); + let i = ii.borrow().clone(); + ii.set(i + 1); } } @@ -667,7 +668,7 @@ mod tests { let opt = Some(x); let _y = opt.unwrap(); } - assert_eq!(i.get(), 1); + assert_eq!(*i.borrow(), 1); } #[test] -- cgit 1.4.1-3-g733a5