diff options
| author | Tobias Bucher <tobiasbucher5991@gmail.com> | 2016-05-06 00:39:25 +0200 |
|---|---|---|
| committer | Tobias Bucher <tobiasbucher5991@gmail.com> | 2016-05-06 00:40:51 +0200 |
| commit | 9370d3a05107bdcac6fb62d3bd5380e1d7c79b5b (patch) | |
| tree | 0e3e17d388edf4a919a963a2da841fb3842c6628 | |
| parent | 413bafdabf5c5716fd823ac9a6232c48e2ba2902 (diff) | |
| download | rust-9370d3a05107bdcac6fb62d3bd5380e1d7c79b5b.tar.gz rust-9370d3a05107bdcac6fb62d3bd5380e1d7c79b5b.zip | |
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.
| -rw-r--r-- | src/libcore/cell.rs | 24 |
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")] |
