Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add get_mut methods to the RefCell and Cell
This is safe since the borrow checker ensures that we have the only
mutable reference to the struct, thus we can safely borrow its interior.

Tracking issue is #33444.
  • Loading branch information
tbu- committed May 5, 2016
1 parent 413bafd commit 9370d3a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/libcore/cell.rs
Expand Up @@ -232,6 +232,18 @@ impl<T:Copy> Cell<T> {
pub fn as_unsafe_cell(&self) -> &UnsafeCell<T> {
&self.value
}

/// Returns a mutable reference to the underlying data.
///
/// This call borrows `Cell` mutably (at compile-time) which guarantees
/// that we possess the only reference.
#[inline]
#[unstable(feature = "cell_get_mut", issue = "33444")]
pub fn get_mut(&mut self) -> &mut T {
unsafe {
&mut *self.value.get()
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -455,6 +467,18 @@ impl<T: ?Sized> RefCell<T> {
pub unsafe fn as_unsafe_cell(&self) -> &UnsafeCell<T> {
&self.value
}

/// Returns a mutable reference to the underlying data.
///
/// This call borrows `RefCell` mutably (at compile-time) so there is no
/// need for dynamic checks.
#[inline]
#[unstable(feature = "cell_get_mut", issue="33444")]
pub fn get_mut(&mut self) -> &mut T {
unsafe {
&mut *self.value.get()
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit 9370d3a

Please sign in to comment.