Skip to content

Commit

Permalink
Add DerefMut for Lazy[Cell/Lock] that delegates to the unstable `…
Browse files Browse the repository at this point in the history
…force_mut()`
  • Loading branch information
ChayimFriedman2 committed Nov 16, 2024
1 parent 46e8d20 commit b208706
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion library/core/src/cell/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::UnsafeCell;
use crate::hint::unreachable_unchecked;
use crate::ops::Deref;
use crate::ops::{Deref, DerefMut};
use crate::{fmt, mem};

enum State<T, F> {
Expand Down Expand Up @@ -284,6 +284,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
}
}

#[stable(feature = "lazy_deref_mut", since = "CURRENT_RUSTC_VERSION")]
impl<T, F: FnOnce() -> T> DerefMut for LazyCell<T, F> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
LazyCell::force_mut(self)
}
}

#[stable(feature = "lazy_cell", since = "1.80.0")]
impl<T: Default> Default for LazyCell<T> {
/// Creates a new lazy value using `Default` as the initializing function.
Expand Down
10 changes: 9 additions & 1 deletion library/std/src/sync/lazy_lock.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -312,6 +312,14 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
}
}

#[stable(feature = "lazy_deref_mut", since = "CURRENT_RUSTC_VERSION")]
impl<T, F: FnOnce() -> T> DerefMut for LazyLock<T, F> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
LazyLock::force_mut(self)
}
}

#[stable(feature = "lazy_cell", since = "1.80.0")]
impl<T: Default> Default for LazyLock<T> {
/// Creates a new lazy value using `Default` as the initializing function.
Expand Down

0 comments on commit b208706

Please sign in to comment.