Skip to content

Commit c516eee

Browse files
committed
Move std::cell::clone_ref to a clone associated function on std::cell::Ref
... and generalize the bounds on the value type.
1 parent 621a10e commit c516eee

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
@@ -545,13 +545,30 @@ impl<'b, T: ?Sized> Deref for Ref<'b, T> {
545545
///
546546
/// A `Clone` implementation would interfere with the widespread
547547
/// use of `r.borrow().clone()` to clone the contents of a `RefCell`.
548+
#[deprecated(since = "1.2.0", reason = "moved to a `Ref::clone` associated function")]
548549
#[unstable(feature = "core",
549550
reason = "likely to be moved to a method, pending language changes")]
550551
#[inline]
551552
pub fn clone_ref<'b, T:Clone>(orig: &Ref<'b, T>) -> Ref<'b, T> {
552-
Ref {
553-
_value: orig._value,
554-
_borrow: orig._borrow.clone(),
553+
Ref::clone(orig)
554+
}
555+
556+
impl<'b, T: ?Sized> Ref<'b, T> {
557+
/// Copies a `Ref`.
558+
///
559+
/// The `RefCell` is already immutably borrowed, so this cannot fail.
560+
///
561+
/// This is an associated function that needs to be used as `Ref::clone(...)`.
562+
/// A `Clone` implementation or a method would interfere with the widespread
563+
/// use of `r.borrow().clone()` to clone the contents of a `RefCell`.
564+
#[unstable(feature = "cell_extras",
565+
reason = "likely to be moved to a method, pending language changes")]
566+
#[inline]
567+
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
568+
Ref {
569+
_value: orig._value,
570+
_borrow: orig._borrow.clone(),
571+
}
555572
}
556573
}
557574

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)