about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-05-05 17:55:28 -0700
committerbors <bors@rust-lang.org>2016-05-05 17:55:28 -0700
commitb071c1feeab98c91dc99e7053e1bfa1579213e01 (patch)
treeb6e7b011d0a4b22b5b5e0d45e555425e19340a9f
parent77987ba89894997c174ac8b01185c0dc12913fa5 (diff)
parent9370d3a05107bdcac6fb62d3bd5380e1d7c79b5b (diff)
downloadrust-b071c1feeab98c91dc99e7053e1bfa1579213e01.tar.gz
rust-b071c1feeab98c91dc99e7053e1bfa1579213e01.zip
Auto merge of #32565 - tbu-:pr_cell_as_mut, r=alexcrichton
Add `as_mut` methods to the `std::cell` structs

This is safe since the borrow checking ensures that we have the only
mutable reference to the struct, thus we can safely borrow its interior.
-rw-r--r--src/libcore/cell.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index 257027dad5e..19c778b0234 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -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")]
@@ -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")]