File tree Expand file tree Collapse file tree 1 file changed +20
-15
lines changed
Expand file tree Collapse file tree 1 file changed +20
-15
lines changed Original file line number Diff line number Diff line change @@ -442,22 +442,27 @@ mod once_box {
442442 where
443443 F : FnOnce ( ) -> Result < Box < T > , E > ,
444444 {
445- let mut ptr = self . inner . load ( Ordering :: Acquire ) ;
445+ match self . get ( ) {
446+ Some ( val) => Ok ( val) ,
447+ None => self . init ( f)
448+ }
449+ }
446450
447- if ptr. is_null ( ) {
448- let val = f ( ) ?;
449- ptr = Box :: into_raw ( val) ;
450- let exchange = self . inner . compare_exchange (
451- ptr:: null_mut ( ) ,
452- ptr,
453- Ordering :: Release ,
454- Ordering :: Acquire ,
455- ) ;
456- if let Err ( old) = exchange {
457- drop ( unsafe { Box :: from_raw ( ptr) } ) ;
458- ptr = old;
459- }
460- } ;
451+ #[ cold]
452+ #[ inline( never) ]
453+ fn init < E > ( & self , f : impl FnOnce ( ) -> Result < Box < T > , E > ) -> Result < & T , E > {
454+ let val = f ( ) ?;
455+ let mut ptr = Box :: into_raw ( val) ;
456+ let exchange = self . inner . compare_exchange (
457+ ptr:: null_mut ( ) ,
458+ ptr,
459+ Ordering :: Release ,
460+ Ordering :: Acquire ,
461+ ) ;
462+ if let Err ( old) = exchange {
463+ drop ( unsafe { Box :: from_raw ( ptr) } ) ;
464+ ptr = old;
465+ }
461466 Ok ( unsafe { & * ptr } )
462467 }
463468 }
You can’t perform that action at this time.
0 commit comments