From c516eee503ae643ead9553fed70528230feb2b1f Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 28 May 2015 22:58:04 +0200 Subject: [PATCH] Move std::cell::clone_ref to a clone associated function on std::cell::Ref ... and generalize the bounds on the value type. --- src/libcore/cell.rs | 23 ++++++++++++++++++++--- src/libcoretest/cell.rs | 4 ++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 4c9f16fdaeeb5..dbb0db3336632 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -545,13 +545,30 @@ impl<'b, T: ?Sized> Deref for Ref<'b, T> { /// /// A `Clone` implementation would interfere with the widespread /// use of `r.borrow().clone()` to clone the contents of a `RefCell`. +#[deprecated(since = "1.2.0", reason = "moved to a `Ref::clone` associated function")] #[unstable(feature = "core", reason = "likely to be moved to a method, pending language changes")] #[inline] pub fn clone_ref<'b, T:Clone>(orig: &Ref<'b, T>) -> Ref<'b, T> { - Ref { - _value: orig._value, - _borrow: orig._borrow.clone(), + Ref::clone(orig) +} + +impl<'b, T: ?Sized> Ref<'b, T> { + /// Copies a `Ref`. + /// + /// The `RefCell` is already immutably borrowed, so this cannot fail. + /// + /// This is an associated function that needs to be used as `Ref::clone(...)`. + /// A `Clone` implementation or a method would interfere with the widespread + /// use of `r.borrow().clone()` to clone the contents of a `RefCell`. + #[unstable(feature = "cell_extras", + reason = "likely to be moved to a method, pending language changes")] + #[inline] + pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> { + Ref { + _value: orig._value, + _borrow: orig._borrow.clone(), + } } } diff --git a/src/libcoretest/cell.rs b/src/libcoretest/cell.rs index f02312b8641e1..eae8cd2c0f3a1 100644 --- a/src/libcoretest/cell.rs +++ b/src/libcoretest/cell.rs @@ -115,13 +115,13 @@ fn discard_doesnt_unborrow() { } #[test] -fn clone_ref_updates_flag() { +fn ref_clone_updates_flag() { let x = RefCell::new(0); { let b1 = x.borrow(); assert_eq!(x.borrow_state(), BorrowState::Reading); { - let _b2 = clone_ref(&b1); + let _b2 = Ref::clone(&b1); assert_eq!(x.borrow_state(), BorrowState::Reading); } assert_eq!(x.borrow_state(), BorrowState::Reading);