Skip to content

Added get_unchecked and get_unchecked_mut to OnceLock and LazyLock #138914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions library/std/src/sync/lazy_lock.rs
Original file line number Diff line number Diff line change
@@ -174,14 +174,14 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
guard.0.once.set_state(ExclusiveState::Complete);
core::mem::forget(guard);
// SAFETY: We put the value there above.
unsafe { &mut this.data.get_mut().value }
unsafe { LazyLock::get_unchecked_mut(this) }
}

let state = this.once.state();
match state {
ExclusiveState::Poisoned => panic_poisoned(),
// SAFETY: The `Once` states we completed the initialization.
ExclusiveState::Complete => unsafe { &mut this.data.get_mut().value },
ExclusiveState::Complete => unsafe { LazyLock::get_unchecked_mut(this) },
// SAFETY: The state is `Incomplete`.
ExclusiveState::Incomplete => unsafe { really_init_mut(this) },
}
@@ -222,7 +222,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
// * the closure was not called because the Once is poisoned, so this point
// is never reached.
// So `value` has definitely been initialized and will not be modified again.
unsafe { &*(*this.data.get()).value }
unsafe { LazyLock::get_unchecked(this) }
}
}

@@ -251,7 +251,7 @@ impl<T, F> LazyLock<T, F> {
match state {
// SAFETY:
// The closure has been run successfully, so `value` has been initialized.
ExclusiveState::Complete => Some(unsafe { &mut this.data.get_mut().value }),
ExclusiveState::Complete => Some(unsafe { LazyLock::get_unchecked_mut(this) }),
_ => None,
}
}
@@ -278,11 +278,31 @@ impl<T, F> LazyLock<T, F> {
// SAFETY:
// The closure has been run successfully, so `value` has been initialized
// and will not be modified again.
Some(unsafe { &(*this.data.get()).value })
Some(unsafe { LazyLock::get_unchecked(this) })
} else {
None
}
}

/// # Safety
///
/// The lazy must be initialized
#[inline]
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "138914")]
pub unsafe fn get_unchecked(this: &LazyLock<T, F>) -> &T {
debug_assert!(this.once.is_completed());
unsafe { &*(*this.data.get()).value }
}

/// # Safety
///
/// The lazy must be initialized
#[inline]
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "138914")]
pub unsafe fn get_unchecked_mut(this: &mut LazyLock<T, F>) -> &mut T {
debug_assert!(this.once.is_completed());
unsafe { &mut *this.data.get_mut().value }
}
}

#[stable(feature = "lazy_cell", since = "1.80.0")]
6 changes: 4 additions & 2 deletions library/std/src/sync/once_lock.rs
Original file line number Diff line number Diff line change
@@ -535,7 +535,8 @@ impl<T> OnceLock<T> {
///
/// The cell must be initialized
#[inline]
unsafe fn get_unchecked(&self) -> &T {
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "138914")]
pub unsafe fn get_unchecked(&self) -> &T {
debug_assert!(self.is_initialized());
unsafe { (&*self.value.get()).assume_init_ref() }
}
@@ -544,7 +545,8 @@ impl<T> OnceLock<T> {
///
/// The cell must be initialized
#[inline]
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "138914")]
pub unsafe fn get_unchecked_mut(&mut self) -> &mut T {
debug_assert!(self.is_initialized());
unsafe { (&mut *self.value.get()).assume_init_mut() }
}