@@ -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" ) ]
0 commit comments