332332#[ cfg( feature = "alloc" ) ]
333333extern crate alloc;
334334
335- #[ cfg( feature = "std" ) ]
336- #[ cfg( feature = "parking_lot" ) ]
335+ #[ cfg( all( feature = "critical-section" , not( feature = "std" ) ) ) ]
336+ #[ path = "imp_cs.rs" ]
337+ mod imp;
338+
339+ #[ cfg( all( feature = "std" , feature = "parking_lot" ) ) ]
337340#[ path = "imp_pl.rs" ]
338341mod imp;
339342
340- #[ cfg( feature = "std" ) ]
341- #[ cfg( not( feature = "parking_lot" ) ) ]
343+ #[ cfg( all( feature = "std" , not( feature = "parking_lot" ) ) ) ]
342344#[ path = "imp_std.rs" ]
343345mod imp;
344346
345347/// Single-threaded version of `OnceCell`.
346348pub mod unsync {
347349 use core:: {
348350 cell:: { Cell , UnsafeCell } ,
349- fmt, hint , mem,
351+ fmt, mem,
350352 ops:: { Deref , DerefMut } ,
351353 panic:: { RefUnwindSafe , UnwindSafe } ,
352354 } ;
353355
356+ use super :: unwrap_unchecked;
357+
354358 /// A cell which can be written to only once. It is not thread safe.
355359 ///
356360 /// Unlike [`std::cell::RefCell`], a `OnceCell` provides simple `&`
@@ -442,6 +446,7 @@ pub mod unsync {
442446 /// Gets a reference to the underlying value.
443447 ///
444448 /// Returns `None` if the cell is empty.
449+ #[ inline]
445450 pub fn get ( & self ) -> Option < & T > {
446451 // Safe due to `inner`'s invariant
447452 unsafe { & * self . inner . get ( ) } . as_ref ( )
@@ -463,11 +468,24 @@ pub mod unsync {
463468 /// *cell.get_mut().unwrap() = 93;
464469 /// assert_eq!(cell.get(), Some(&93));
465470 /// ```
471+ #[ inline]
466472 pub fn get_mut ( & mut self ) -> Option < & mut T > {
467473 // Safe because we have unique access
468474 unsafe { & mut * self . inner . get ( ) } . as_mut ( )
469475 }
470476
477+ /// Get the reference to the underlying value, without checking if the
478+ /// cell is initialized.
479+ ///
480+ /// # Safety
481+ ///
482+ /// Caller must ensure that the cell is in initialized state.
483+ #[ cfg( feature = "critical-section" ) ]
484+ #[ inline]
485+ pub ( crate ) unsafe fn get_unchecked ( & self ) -> & T {
486+ crate :: unwrap_unchecked ( self . get ( ) )
487+ }
488+
471489 /// Sets the contents of this cell to `value`.
472490 ///
473491 /// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
@@ -510,16 +528,14 @@ pub mod unsync {
510528 if let Some ( old) = self . get ( ) {
511529 return Err ( ( old, value) ) ;
512530 }
531+
513532 let slot = unsafe { & mut * self . inner . get ( ) } ;
514533 // This is the only place where we set the slot, no races
515534 // due to reentrancy/concurrency are possible, and we've
516535 // checked that slot is currently `None`, so this write
517536 // maintains the `inner`'s invariant.
518537 * slot = Some ( value) ;
519- Ok ( match & * slot {
520- Some ( value) => value,
521- None => unsafe { hint:: unreachable_unchecked ( ) } ,
522- } )
538+ Ok ( unsafe { unwrap_unchecked ( slot. as_ref ( ) ) } )
523539 }
524540
525541 /// Gets the contents of the cell, initializing it with `f`
@@ -592,7 +608,7 @@ pub mod unsync {
592608 // `assert`, while keeping `set/get` would be sound, but it seems
593609 // better to panic, rather than to silently use an old value.
594610 assert ! ( self . set( val) . is_ok( ) , "reentrant init" ) ;
595- Ok ( self . get ( ) . unwrap ( ) )
611+ Ok ( unsafe { unwrap_unchecked ( self . get ( ) ) } )
596612 }
597613
598614 /// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
@@ -814,16 +830,16 @@ pub mod unsync {
814830}
815831
816832/// Thread-safe, blocking version of `OnceCell`.
817- #[ cfg( feature = "std " ) ]
833+ #[ cfg( feature = "sync " ) ]
818834pub mod sync {
819- use std :: {
835+ use core :: {
820836 cell:: Cell ,
821837 fmt, mem,
822838 ops:: { Deref , DerefMut } ,
823839 panic:: RefUnwindSafe ,
824840 } ;
825841
826- use crate :: { imp:: OnceCell as Imp , take_unchecked} ;
842+ use super :: { imp:: OnceCell as Imp , take_unchecked} ;
827843
828844 /// A thread-safe cell which can be written to only once.
829845 ///
@@ -942,8 +958,9 @@ pub mod sync {
942958 /// // Will return 92, but might block until the other thread does `.set`.
943959 /// let value: &u32 = cell.wait();
944960 /// assert_eq!(*value, 92);
945- /// t.join().unwrap();;
961+ /// t.join().unwrap();
946962 /// ```
963+ #[ cfg( not( feature = "critical-section" ) ) ]
947964 pub fn wait ( & self ) -> & T {
948965 if !self . 0 . is_initialized ( ) {
949966 self . 0 . wait ( )
@@ -969,6 +986,7 @@ pub mod sync {
969986 /// cell.set(92).unwrap();
970987 /// cell = OnceCell::new();
971988 /// ```
989+ #[ inline]
972990 pub fn get_mut ( & mut self ) -> Option < & mut T > {
973991 self . 0 . get_mut ( )
974992 }
@@ -980,6 +998,7 @@ pub mod sync {
980998 ///
981999 /// Caller must ensure that the cell is in initialized state, and that
9821000 /// the contents are acquired by (synchronized to) this thread.
1001+ #[ inline]
9831002 pub unsafe fn get_unchecked ( & self ) -> & T {
9841003 self . 0 . get_unchecked ( )
9851004 }
@@ -1109,6 +1128,7 @@ pub mod sync {
11091128 if let Some ( value) = self . get ( ) {
11101129 return Ok ( value) ;
11111130 }
1131+
11121132 self . 0 . initialize ( f) ?;
11131133
11141134 // Safe b/c value is initialized.
@@ -1164,6 +1184,7 @@ pub mod sync {
11641184 /// cell.set("hello".to_string()).unwrap();
11651185 /// assert_eq!(cell.into_inner(), Some("hello".to_string()));
11661186 /// ```
1187+ #[ inline]
11671188 pub fn into_inner ( self ) -> Option < T > {
11681189 self . 0 . into_inner ( )
11691190 }
@@ -1356,13 +1377,19 @@ pub mod sync {
13561377#[ cfg( feature = "race" ) ]
13571378pub mod race;
13581379
1359- #[ cfg( feature = "std" ) ]
1380+ #[ cfg( feature = "sync" ) ]
1381+ #[ inline]
13601382unsafe fn take_unchecked < T > ( val : & mut Option < T > ) -> T {
1361- match val. take ( ) {
1362- Some ( it) => it,
1383+ unwrap_unchecked ( val. take ( ) )
1384+ }
1385+
1386+ #[ inline]
1387+ unsafe fn unwrap_unchecked < T > ( val : Option < T > ) -> T {
1388+ match val {
1389+ Some ( value) => value,
13631390 None => {
13641391 debug_assert ! ( false ) ;
1365- std :: hint:: unreachable_unchecked ( )
1392+ core :: hint:: unreachable_unchecked ( )
13661393 }
13671394 }
13681395}
0 commit comments