From 200d742984ddd7eb1c96db09882217fcb1a1a146 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Tue, 6 May 2025 21:49:56 +0200 Subject: [PATCH] Clarify &mut-methods' docs on sync::OnceLock --- library/std/src/sync/once_lock.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/library/std/src/sync/once_lock.rs b/library/std/src/sync/once_lock.rs index 324b5451873bf..a5c3a6c46a433 100644 --- a/library/std/src/sync/once_lock.rs +++ b/library/std/src/sync/once_lock.rs @@ -159,8 +159,11 @@ impl OnceLock { /// Gets the mutable reference to the underlying value. /// - /// Returns `None` if the cell is uninitialized, or being initialized. - /// This method never blocks. + /// Returns `None` if the cell is uninitialized. + /// + /// This method never blocks. Since it borrows the `OnceLock` mutably, + /// it is statically guaranteed that no active borrows to the `OnceLock` + /// exist, including from other threads. #[inline] #[stable(feature = "once_cell", since = "1.70.0")] pub fn get_mut(&mut self) -> Option<&mut T> { @@ -315,7 +318,9 @@ impl OnceLock { /// Gets the mutable reference of the contents of the cell, initializing /// it to `f()` if the cell was uninitialized. /// - /// This method never blocks. + /// This method never blocks. Since it borrows the `OnceLock` mutably, + /// it is statically guaranteed that no active borrows to the `OnceLock` + /// exist, including from other threads. /// /// # Panics /// @@ -405,7 +410,9 @@ impl OnceLock { /// it to `f()` if the cell was uninitialized. If the cell was uninitialized /// and `f()` failed, an error is returned. /// - /// This method never blocks. + /// This method never blocks. Since it borrows the `OnceLock` mutably, + /// it is statically guaranteed that no active borrows to the `OnceLock` + /// exist, including from other threads. /// /// # Panics /// @@ -469,7 +476,8 @@ impl OnceLock { /// /// Has no effect and returns `None` if the `OnceLock` was uninitialized. /// - /// Safety is guaranteed by requiring a mutable reference. + /// Since this method borrows the `OnceLock` mutably, it is statically guaranteed that + /// no active borrows to the `OnceLock` exist, including from other threads. /// /// # Examples ///