Skip to content

Commit b071c1f

Browse files
committed
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.
2 parents 77987ba + 9370d3a commit b071c1f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: 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)