From b208706b6e2d7b2a218347e4311a05fe3a022d52 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Sat, 16 Nov 2024 22:23:19 +0200 Subject: Add `DerefMut` for `Lazy[Cell/Lock]` that delegates to the unstable `force_mut()` --- library/std/src/sync/lazy_lock.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'library/std/src') diff --git a/library/std/src/sync/lazy_lock.rs b/library/std/src/sync/lazy_lock.rs index 40510f56134..4379a1519c6 100644 --- a/library/std/src/sync/lazy_lock.rs +++ b/library/std/src/sync/lazy_lock.rs @@ -1,7 +1,7 @@ use super::once::ExclusiveState; use crate::cell::UnsafeCell; use crate::mem::ManuallyDrop; -use crate::ops::Deref; +use crate::ops::{Deref, DerefMut}; use crate::panic::{RefUnwindSafe, UnwindSafe}; use crate::sync::Once; use crate::{fmt, ptr}; @@ -312,6 +312,14 @@ impl T> Deref for LazyLock { } } +#[stable(feature = "lazy_deref_mut", since = "CURRENT_RUSTC_VERSION")] +impl T> DerefMut for LazyLock { + #[inline] + fn deref_mut(&mut self) -> &mut T { + LazyLock::force_mut(self) + } +} + #[stable(feature = "lazy_cell", since = "1.80.0")] impl Default for LazyLock { /// Creates a new lazy value using `Default` as the initializing function. -- cgit 1.4.1-3-g733a5 From ae25c39f2216ea1f6f062faac71761cd3f1bdd16 Mon Sep 17 00:00:00 2001 From: Luca Versari Date: Thu, 8 May 2025 09:33:38 +0200 Subject: Update documentation of OnceLock::get_or_init. Explicitly point out that if the function panics the init function might be called multiple times. --- library/std/src/sync/once_lock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'library/std/src') diff --git a/library/std/src/sync/once_lock.rs b/library/std/src/sync/once_lock.rs index ffb90b14695..324b5451873 100644 --- a/library/std/src/sync/once_lock.rs +++ b/library/std/src/sync/once_lock.rs @@ -279,7 +279,7 @@ impl OnceLock { /// /// Many threads may call `get_or_init` concurrently with different /// initializing functions, but it is guaranteed that only one function - /// will be executed. + /// will be executed if the function doesn't panic. /// /// # Panics /// -- cgit 1.4.1-3-g733a5