Skip to content

Commit 21001b3

Browse files
SimonSapinXMPPwocky
authored andcommitted
Move std::cell::clone_ref to a clone associated function on std::cell::Ref
... and generalize the bounds on the value type.
1 parent acf36dc commit 21001b3

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/libcore/cell.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,30 @@ impl<'b, T: ?Sized> Deref for Ref<'b, T> {
551551
///
552552
/// A `Clone` implementation would interfere with the widespread
553553
/// use of `r.borrow().clone()` to clone the contents of a `RefCell`.
554+
#[deprecated(since = "1.2.0", reason = "moved to a `Ref::clone` associated function")]
554555
#[unstable(feature = "core",
555556
reason = "likely to be moved to a method, pending language changes")]
556557
#[inline]
557558
pub fn clone_ref<'b, T:Clone>(orig: &Ref<'b, T>) -> Ref<'b, T> {
558-
Ref {
559-
_value: orig._value,
560-
_borrow: orig._borrow.clone(),
559+
Ref::clone(orig)
560+
}
561+
562+
impl<'b, T: ?Sized> Ref<'b, T> {
563+
/// Copies a `Ref`.
564+
///
565+
/// The `RefCell` is already immutably borrowed, so this cannot fail.
566+
///
567+
/// This is an associated function that needs to be used as `Ref::clone(...)`.
568+
/// A `Clone` implementation or a method would interfere with the widespread
569+
/// use of `r.borrow().clone()` to clone the contents of a `RefCell`.
570+
#[unstable(feature = "cell_extras",
571+
reason = "likely to be moved to a method, pending language changes")]
572+
#[inline]
573+
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
574+
Ref {
575+
_value: orig._value,
576+
_borrow: orig._borrow.clone(),
577+
}
561578
}
562579
}
563580

src/libcoretest/cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ fn discard_doesnt_unborrow() {
115115
}
116116

117117
#[test]
118-
fn clone_ref_updates_flag() {
118+
fn ref_clone_updates_flag() {
119119
let x = RefCell::new(0);
120120
{
121121
let b1 = x.borrow();
122122
assert_eq!(x.borrow_state(), BorrowState::Reading);
123123
{
124-
let _b2 = clone_ref(&b1);
124+
let _b2 = Ref::clone(&b1);
125125
assert_eq!(x.borrow_state(), BorrowState::Reading);
126126
}
127127
assert_eq!(x.borrow_state(), BorrowState::Reading);

0 commit comments

Comments
 (0)