Skip to content

Commit 9370d3a

Browse files
committed
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.
1 parent 413bafd commit 9370d3a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libcore/cell.rs

+24
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ impl<T:Copy> Cell<T> {
232232
pub fn as_unsafe_cell(&self) -> &UnsafeCell<T> {
233233
&self.value
234234
}
235+
236+
/// Returns a mutable reference to the underlying data.
237+
///
238+
/// This call borrows `Cell` mutably (at compile-time) which guarantees
239+
/// that we possess the only reference.
240+
#[inline]
241+
#[unstable(feature = "cell_get_mut", issue = "33444")]
242+
pub fn get_mut(&mut self) -> &mut T {
243+
unsafe {
244+
&mut *self.value.get()
245+
}
246+
}
235247
}
236248

237249
#[stable(feature = "rust1", since = "1.0.0")]
@@ -455,6 +467,18 @@ impl<T: ?Sized> RefCell<T> {
455467
pub unsafe fn as_unsafe_cell(&self) -> &UnsafeCell<T> {
456468
&self.value
457469
}
470+
471+
/// Returns a mutable reference to the underlying data.
472+
///
473+
/// This call borrows `RefCell` mutably (at compile-time) so there is no
474+
/// need for dynamic checks.
475+
#[inline]
476+
#[unstable(feature = "cell_get_mut", issue="33444")]
477+
pub fn get_mut(&mut self) -> &mut T {
478+
unsafe {
479+
&mut *self.value.get()
480+
}
481+
}
458482
}
459483

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

0 commit comments

Comments
 (0)