Skip to content

Commit 684d17f

Browse files
authored
Rollup merge of #163018 - malezjaa:once_lazy_lock_get_unchecked, r=LawnGnome
implement (OnceLock,LazyLock)::(get_unchecked,get_unchecked_mut) Tracking issue: #162716 Based on the original work of @HomelikeBrick42 from #138914.
2 parents 81f6a6a + 85110fb commit 684d17f

3 files changed

Lines changed: 108 additions & 9 deletions

File tree

‎library/std/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@
368368
#![feature(maybe_dangling)]
369369
#![feature(maybe_uninit_array_assume_init)]
370370
#![feature(maybe_uninit_fill)]
371+
#![feature(once_lazy_lock_get_unchecked)]
371372
#![feature(panic_can_unwind)]
372373
#![feature(panic_internals)]
373374
#![feature(pin_coerce_unsized_trait)]

‎library/std/src/sync/lazy_lock.rs‎

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
197197
guard.0.once.set_state(OnceExclusiveState::Complete);
198198
core::mem::forget(guard);
199199
// SAFETY: We put the value there above.
200-
unsafe { &mut this.data.get_mut().value }
200+
unsafe { LazyLock::get_unchecked_mut(this) }
201201
}
202202

203203
let state = this.once.state();
204204
match state {
205205
OnceExclusiveState::Poisoned => panic_poisoned(),
206206
// SAFETY: The `Once` states we completed the initialization.
207-
OnceExclusiveState::Complete => unsafe { &mut this.data.get_mut().value },
207+
OnceExclusiveState::Complete => unsafe { LazyLock::get_unchecked_mut(this) },
208208
// SAFETY: The state is `Incomplete`.
209209
OnceExclusiveState::Incomplete => unsafe { really_init_mut(this) },
210210
}
@@ -258,7 +258,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
258258
// * the closure was not called, but a previous call initialized `value`.
259259
// * the closure was not called because the Once is poisoned, which we handled above.
260260
// So `value` has definitely been initialized and will not be modified again.
261-
unsafe { &(*this.data.get()).value }
261+
unsafe { LazyLock::get_unchecked(this) }
262262
}
263263
}
264264

@@ -286,7 +286,7 @@ impl<T, F> LazyLock<T, F> {
286286
match state {
287287
// SAFETY:
288288
// The closure has been run successfully, so `value` has been initialized.
289-
OnceExclusiveState::Complete => Some(unsafe { &mut this.data.get_mut().value }),
289+
OnceExclusiveState::Complete => Some(unsafe { LazyLock::get_unchecked_mut(this) }),
290290
_ => None,
291291
}
292292
}
@@ -313,11 +313,71 @@ impl<T, F> LazyLock<T, F> {
313313
// SAFETY:
314314
// The closure has been run successfully, so `value` has been initialized
315315
// and will not be modified again.
316-
Some(unsafe { &(*this.data.get()).value })
316+
Some(unsafe { LazyLock::get_unchecked(this) })
317317
} else {
318318
None
319319
}
320320
}
321+
322+
/// Returns a shared reference to the value stored in the `LazyLock` without
323+
/// checking whether it has been initialized.
324+
///
325+
/// # Safety
326+
///
327+
/// The lazy value must be initialized before calling this function.
328+
///
329+
/// # Examples
330+
///
331+
/// ```
332+
/// #![feature(once_lazy_lock_get_unchecked)]
333+
///
334+
/// use std::sync::LazyLock;
335+
///
336+
/// let lazy = LazyLock::new(|| 42);
337+
///
338+
/// // Initialize the lazy.
339+
/// let _ = &*lazy;
340+
///
341+
/// let value = unsafe { LazyLock::get_unchecked(&lazy) };
342+
/// assert_eq!(*value, 42);
343+
/// ```
344+
#[inline]
345+
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "162716")]
346+
pub unsafe fn get_unchecked(this: &LazyLock<T, F>) -> &T {
347+
debug_assert!(this.once.is_completed());
348+
unsafe { &(*this.data.get()).value }
349+
}
350+
351+
/// Returns a mutable reference to the value stored in the `LazyLock` without
352+
/// checking whether it has been initialized.
353+
///
354+
/// # Safety
355+
///
356+
/// The lazy value must be initialized before calling this function.
357+
///
358+
/// # Examples
359+
///
360+
/// ```
361+
/// #![feature(once_lazy_lock_get_unchecked)]
362+
///
363+
/// use std::sync::LazyLock;
364+
///
365+
/// let mut lazy = LazyLock::new(|| 42);
366+
///
367+
/// // Initialize the lazy.
368+
/// let _ = &*lazy;
369+
///
370+
/// let value = unsafe { LazyLock::get_unchecked_mut(&mut lazy) };
371+
/// *value = 100;
372+
///
373+
/// assert_eq!(*lazy, 100);
374+
/// ```
375+
#[inline]
376+
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "162716")]
377+
pub unsafe fn get_unchecked_mut(this: &mut LazyLock<T, F>) -> &mut T {
378+
debug_assert!(this.once.is_completed());
379+
unsafe { &mut this.data.get_mut().value }
380+
}
321381
}
322382

323383
#[stable(feature = "lazy_cell", since = "1.80.0")]

‎library/std/src/sync/once_lock.rs‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,20 +585,58 @@ impl<T> OnceLock<T> {
585585
res
586586
}
587587

588+
/// Returns a reference to the value in the `OnceLock` without checking
589+
/// whether it has been initialized.
590+
///
588591
/// # Safety
589592
///
590-
/// The cell must be initialized
593+
/// The `OnceLock` must be initialized before calling this function.
594+
///
595+
/// # Examples
596+
///
597+
/// ```
598+
/// #![feature(once_lazy_lock_get_unchecked)]
599+
///
600+
/// use std::sync::OnceLock;
601+
///
602+
/// let cell = OnceLock::new();
603+
/// cell.set(42).unwrap();
604+
///
605+
/// let value = unsafe { cell.get_unchecked() };
606+
/// assert_eq!(*value, 42);
607+
/// ```
591608
#[inline]
592-
unsafe fn get_unchecked(&self) -> &T {
609+
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "162716")]
610+
pub unsafe fn get_unchecked(&self) -> &T {
593611
debug_assert!(self.initialized());
594612
unsafe { (&*self.value.get()).assume_init_ref() }
595613
}
596614

615+
/// Returns a mutable reference to the value in the `OnceLock` without
616+
/// checking whether it has been initialized.
617+
///
597618
/// # Safety
598619
///
599-
/// The cell must be initialized
620+
/// The `OnceLock` must be initialized before calling this function.
621+
///
622+
/// # Examples
623+
///
624+
/// ```
625+
/// #![feature(once_lazy_lock_get_unchecked)]
626+
///
627+
/// use std::sync::OnceLock;
628+
///
629+
/// let mut cell = OnceLock::new();
630+
/// cell.set(42).unwrap();
631+
///
632+
/// let value = unsafe { cell.get_unchecked_mut() };
633+
/// *value = 100;
634+
///
635+
/// assert_eq!(*cell.get().unwrap(), 100);
636+
/// ```
600637
#[inline]
601-
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
638+
#[unstable(feature = "once_lazy_lock_get_unchecked", issue = "162716")]
639+
pub unsafe fn get_unchecked_mut(&mut self) -> &mut T {
602640
debug_assert!(self.initialized_mut());
603641
unsafe { self.value.get_mut().assume_init_mut() }
604642
}

0 commit comments

Comments
 (0)