Skip to content

Commit c68c384

Browse files
committed
Update documentation in thread/local.rs.
1 parent 36c9045 commit c68c384

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

library/std/src/thread/local.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,18 @@ impl<T: 'static> LocalKey<T> {
443443
}
444444
}
445445

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.
446458
fn initialize_with<F, R>(&'static self, init: T, f: F) -> R
447459
where
448460
F: FnOnce(Option<T>, &T) -> R,
@@ -488,9 +500,12 @@ impl<T: 'static> LocalKey<Cell<T>> {
488500
/// ```
489501
#[unstable(feature = "local_key_cell_methods", issue = "92122")]
490502
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());
494509
}
495510
});
496511
}
@@ -593,7 +608,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
593608
///
594609
/// # Panics
595610
///
596-
/// Panics if the value is currently borrowed.
611+
/// Panics if the value is currently mutably borrowed.
597612
///
598613
/// Panics if the key currently has its destructor running,
599614
/// and it **may** panic if the destructor has previously been run for this thread.
@@ -660,6 +675,8 @@ impl<T: 'static> LocalKey<RefCell<T>> {
660675
///
661676
/// # Panics
662677
///
678+
/// Panics if the value is currently borrowed.
679+
///
663680
/// Panics if the key currently has its destructor running,
664681
/// and it **may** panic if the destructor has previously been run for this thread.
665682
///
@@ -681,8 +698,11 @@ impl<T: 'static> LocalKey<RefCell<T>> {
681698
/// ```
682699
#[unstable(feature = "local_key_cell_methods", issue = "92122")]
683700
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.
686706
cell.replace(init.into_inner());
687707
}
688708
});

0 commit comments

Comments
 (0)