From 9370d3a05107bdcac6fb62d3bd5380e1d7c79b5b Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Fri, 6 May 2016 00:39:25 +0200 Subject: [PATCH] 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. --- src/libcore/cell.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 257027dad5ed2..19c778b023490 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -232,6 +232,18 @@ impl Cell { pub fn as_unsafe_cell(&self) -> &UnsafeCell { &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 RefCell { pub unsafe fn as_unsafe_cell(&self) -> &UnsafeCell { &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")]