@@ -443,6 +443,18 @@ impl<T: 'static> LocalKey<T> {
443
443
}
444
444
}
445
445
446
+ /// Acquires a reference to the value in this TLS key, initializing it with
447
+ /// `init` if it wasn't already initialized on this thread.
448
+ ///
449
+ /// If `init` was used to initialize the thread local variable, `None` is
450
+ /// passed as the first argument to `f`. If it was already initialized,
451
+ /// `Some(init)` is passed to `f`.
452
+ ///
453
+ /// # Panics
454
+ ///
455
+ /// This function will panic if the key currently has its destructor
456
+ /// running, and it **may** panic if the destructor has previously been run
457
+ /// for this thread.
446
458
fn initialize_with < F , R > ( & ' static self , init : T , f : F ) -> R
447
459
where
448
460
F : FnOnce ( Option < T > , & T ) -> R ,
@@ -488,9 +500,12 @@ impl<T: 'static> LocalKey<Cell<T>> {
488
500
/// ```
489
501
#[ unstable( feature = "local_key_cell_methods" , issue = "92122" ) ]
490
502
pub fn set ( & ' static self , value : T ) {
491
- self . initialize_with ( Cell :: new ( value) , |init, cell| {
492
- if let Some ( init) = init {
493
- cell. set ( init. into_inner ( ) ) ;
503
+ self . initialize_with ( Cell :: new ( value) , |value, cell| {
504
+ if let Some ( value) = value {
505
+ // The cell was already initialized, so `value` wasn't used to
506
+ // initialize it. So we overwrite the current value with the
507
+ // new one instead.
508
+ cell. set ( value. into_inner ( ) ) ;
494
509
}
495
510
} ) ;
496
511
}
@@ -593,7 +608,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
593
608
///
594
609
/// # Panics
595
610
///
596
- /// Panics if the value is currently borrowed.
611
+ /// Panics if the value is currently mutably borrowed.
597
612
///
598
613
/// Panics if the key currently has its destructor running,
599
614
/// and it **may** panic if the destructor has previously been run for this thread.
@@ -660,6 +675,8 @@ impl<T: 'static> LocalKey<RefCell<T>> {
660
675
///
661
676
/// # Panics
662
677
///
678
+ /// Panics if the value is currently borrowed.
679
+ ///
663
680
/// Panics if the key currently has its destructor running,
664
681
/// and it **may** panic if the destructor has previously been run for this thread.
665
682
///
@@ -681,8 +698,11 @@ impl<T: 'static> LocalKey<RefCell<T>> {
681
698
/// ```
682
699
#[ unstable( feature = "local_key_cell_methods" , issue = "92122" ) ]
683
700
pub fn set ( & ' static self , value : T ) {
684
- self . initialize_with ( RefCell :: new ( value) , |init, cell| {
685
- if let Some ( init) = init {
701
+ self . initialize_with ( RefCell :: new ( value) , |value, cell| {
702
+ if let Some ( value) = value {
703
+ // The cell was already initialized, so `value` wasn't used to
704
+ // initialize it. So we overwrite the current value with the
705
+ // new one instead.
686
706
cell. replace ( init. into_inner ( ) ) ;
687
707
}
688
708
} ) ;
0 commit comments