Skip to content

Commit b208706

Browse files
Add DerefMut for Lazy[Cell/Lock] that delegates to the unstable force_mut()
1 parent 46e8d20 commit b208706

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

library/core/src/cell/lazy.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::UnsafeCell;
22
use crate::hint::unreachable_unchecked;
3-
use crate::ops::Deref;
3+
use crate::ops::{Deref, DerefMut};
44
use crate::{fmt, mem};
55

66
enum State<T, F> {
@@ -284,6 +284,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
284284
}
285285
}
286286

287+
#[stable(feature = "lazy_deref_mut", since = "CURRENT_RUSTC_VERSION")]
288+
impl<T, F: FnOnce() -> T> DerefMut for LazyCell<T, F> {
289+
#[inline]
290+
fn deref_mut(&mut self) -> &mut T {
291+
LazyCell::force_mut(self)
292+
}
293+
}
294+
287295
#[stable(feature = "lazy_cell", since = "1.80.0")]
288296
impl<T: Default> Default for LazyCell<T> {
289297
/// Creates a new lazy value using `Default` as the initializing function.

library/std/src/sync/lazy_lock.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::once::ExclusiveState;
22
use crate::cell::UnsafeCell;
33
use crate::mem::ManuallyDrop;
4-
use crate::ops::Deref;
4+
use crate::ops::{Deref, DerefMut};
55
use crate::panic::{RefUnwindSafe, UnwindSafe};
66
use crate::sync::Once;
77
use crate::{fmt, ptr};
@@ -312,6 +312,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
312312
}
313313
}
314314

315+
#[stable(feature = "lazy_deref_mut", since = "CURRENT_RUSTC_VERSION")]
316+
impl<T, F: FnOnce() -> T> DerefMut for LazyLock<T, F> {
317+
#[inline]
318+
fn deref_mut(&mut self) -> &mut T {
319+
LazyLock::force_mut(self)
320+
}
321+
}
322+
315323
#[stable(feature = "lazy_cell", since = "1.80.0")]
316324
impl<T: Default> Default for LazyLock<T> {
317325
/// Creates a new lazy value using `Default` as the initializing function.

0 commit comments

Comments
 (0)