Skip to content

Commit 5c6bede

Browse files
authored
Unrolled build for #140715
Rollup merge of #140715 - lukaslueg:oncecellsyncdocs, r=tgross35 Clarify &mut-methods' docs on sync::OnceLock Three small changes to the docs of `sync::OnceLock`: * The docs for `OnceLock::take()` used to [say](https://doc.rust-lang.org/std/sync/struct.OnceLock.html#method.take) "**Safety** is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt `OnceLock`, as there is (currently) no way to interact with a `OnceLock` in an unsafe way; there are no unsafe methods on `OnceLock`, so there is "safety" guarantee required anywhere. What we simply meant to say is "**Synchronization** is guaranteed...". * I've add that phrase to the other methods of `OnceLock` which take a `&mut self`, to highlight the fact that having a `&mut OnceLock` guarantees that synchronization with other threads is not required. This is the same as with [`Mutex::get_mut()`](https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut), [`Cell::get_mut()`](https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get_mut), and others. * In that spirit, the half-sentence "or being initialized" was removed from `get_mut()`, as there is no way that the `OnceLock` is being initialized while we are holding `&mut` to it. Probably a copy&paste from `.get()`
2 parents b17dba4 + 200d742 commit 5c6bede

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

library/std/src/sync/once_lock.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ impl<T> OnceLock<T> {
159159

160160
/// Gets the mutable reference to the underlying value.
161161
///
162-
/// Returns `None` if the cell is uninitialized, or being initialized.
163-
/// This method never blocks.
162+
/// Returns `None` if the cell is uninitialized.
163+
///
164+
/// This method never blocks. Since it borrows the `OnceLock` mutably,
165+
/// it is statically guaranteed that no active borrows to the `OnceLock`
166+
/// exist, including from other threads.
164167
#[inline]
165168
#[stable(feature = "once_cell", since = "1.70.0")]
166169
pub fn get_mut(&mut self) -> Option<&mut T> {
@@ -315,7 +318,9 @@ impl<T> OnceLock<T> {
315318
/// Gets the mutable reference of the contents of the cell, initializing
316319
/// it to `f()` if the cell was uninitialized.
317320
///
318-
/// This method never blocks.
321+
/// This method never blocks. Since it borrows the `OnceLock` mutably,
322+
/// it is statically guaranteed that no active borrows to the `OnceLock`
323+
/// exist, including from other threads.
319324
///
320325
/// # Panics
321326
///
@@ -405,7 +410,9 @@ impl<T> OnceLock<T> {
405410
/// it to `f()` if the cell was uninitialized. If the cell was uninitialized
406411
/// and `f()` failed, an error is returned.
407412
///
408-
/// This method never blocks.
413+
/// This method never blocks. Since it borrows the `OnceLock` mutably,
414+
/// it is statically guaranteed that no active borrows to the `OnceLock`
415+
/// exist, including from other threads.
409416
///
410417
/// # Panics
411418
///
@@ -469,7 +476,8 @@ impl<T> OnceLock<T> {
469476
///
470477
/// Has no effect and returns `None` if the `OnceLock` was uninitialized.
471478
///
472-
/// Safety is guaranteed by requiring a mutable reference.
479+
/// Since this method borrows the `OnceLock` mutably, it is statically guaranteed that
480+
/// no active borrows to the `OnceLock` exist, including from other threads.
473481
///
474482
/// # Examples
475483
///

0 commit comments

Comments
 (0)