Skip to content

Commit 27b1b04

Browse files
authored
Rollup merge of #101389 - lukaslueg:rcgetmutdocs, r=m-ou-se
Tone down explanation on RefCell::get_mut The language around `RefCell::get_mut` is remarkably sketchy and especially to the novice seems to quite strongly discourage using the method ("be cautious", "Also, please be aware", "special circumstances", "usually not what you want"). It was added six years ago in #40634 due to confusion about when to use `get_mut` and `borrow_mut`. While its signature limits the use-cases for `get_mut`, there is no chance for a safety footgun, and readers can be made aware of `borrow_mut` more softly. I've also just sent a [PR](rust-lang/rust-clippy#9044) to lint situations where `get_mut` could be used to improve ergonomics and performance. So this PR tones down the language around `get_mut` and also brings it more in line with [`std::sync::Mutex::get_mut()`](https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html#method.get_mut).
2 parents 11bb80a + 2c664bc commit 27b1b04

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

library/core/src/cell.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -1021,15 +1021,18 @@ impl<T: ?Sized> RefCell<T> {
10211021

10221022
/// Returns a mutable reference to the underlying data.
10231023
///
1024-
/// This call borrows `RefCell` mutably (at compile-time) so there is no
1025-
/// need for dynamic checks.
1026-
///
1027-
/// However be cautious: this method expects `self` to be mutable, which is
1028-
/// generally not the case when using a `RefCell`. Take a look at the
1029-
/// [`borrow_mut`] method instead if `self` isn't mutable.
1030-
///
1031-
/// Also, please be aware that this method is only for special circumstances and is usually
1032-
/// not what you want. In case of doubt, use [`borrow_mut`] instead.
1024+
/// Since this method borrows `RefCell` mutably, it is statically guaranteed
1025+
/// that no borrows to the underlying data exist. The dynamic checks inherent
1026+
/// in [`borrow_mut`] and most other methods of `RefCell` are therefor
1027+
/// unnecessary.
1028+
///
1029+
/// This method can only be called if `RefCell` can be mutably borrowed,
1030+
/// which in general is only the case directly after the `RefCell` has
1031+
/// been created. In these situations, skipping the aforementioned dynamic
1032+
/// borrowing checks may yield better ergonomics and runtime-performance.
1033+
///
1034+
/// In most situations where `RefCell` is used, it can't be borrowed mutably.
1035+
/// Use [`borrow_mut`] to get mutable access to the underlying data then.
10331036
///
10341037
/// [`borrow_mut`]: RefCell::borrow_mut()
10351038
///

0 commit comments

Comments
 (0)