Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move std::cell::clone_ref to a clone associated function on std::cell…
…::Ref

... and generalize the bounds on the value type.
  • Loading branch information
SimonSapin committed May 28, 2015
1 parent 621a10e commit c516eee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/libcore/cell.rs
Expand Up @@ -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(),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcoretest/cell.rs
Expand Up @@ -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);
Expand Down

0 comments on commit c516eee

Please sign in to comment.