about summary refs log tree commit diff
path: root/library/std/src/thread/local.rs
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2024-04-08 17:47:09 -0400
committerTrevor Gross <tmgross@umich.edu>2024-04-08 17:47:09 -0400
commit6e68a2f475593f15c9bb3fdbe9b53633a5aa0659 (patch)
tree97a77b51e63e378658d1a6532605b00adb949c4e /library/std/src/thread/local.rs
parent2aec2fe3b8315741432a941f58b087d1311d9a44 (diff)
downloadrust-6e68a2f475593f15c9bb3fdbe9b53633a5aa0659.tar.gz
rust-6e68a2f475593f15c9bb3fdbe9b53633a5aa0659.zip
Add `SAFETY` comments to the thread local implementation
Reduce `unsafe` block scope and add `SAFETY` comments.
Diffstat (limited to 'library/std/src/thread/local.rs')
-rw-r--r--library/std/src/thread/local.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs
index d53abf50eb5..a4acd6c3d06 100644
--- a/library/std/src/thread/local.rs
+++ b/library/std/src/thread/local.rs
@@ -281,10 +281,9 @@ impl<T: 'static> LocalKey<T> {
     where
         F: FnOnce(&T) -> R,
     {
-        unsafe {
-            let thread_local = (self.inner)(None).ok_or(AccessError)?;
-            Ok(f(thread_local))
-        }
+        // SAFETY: `inner` is safe to call within the lifetime of the thread
+        let thread_local = unsafe { (self.inner)(None).ok_or(AccessError)? };
+        Ok(f(thread_local))
     }
 
     /// Acquires a reference to the value in this TLS key, initializing it with
@@ -303,14 +302,17 @@ impl<T: 'static> LocalKey<T> {
     where
         F: FnOnce(Option<T>, &T) -> R,
     {
-        unsafe {
-            let mut init = Some(init);
-            let reference = (self.inner)(Some(&mut init)).expect(
+        let mut init = Some(init);
+
+        // SAFETY: `inner` is safe to call within the lifetime of the thread
+        let reference = unsafe {
+            (self.inner)(Some(&mut init)).expect(
                 "cannot access a Thread Local Storage value \
                  during or after destruction",
-            );
-            f(init, reference)
-        }
+            )
+        };
+
+        f(init, reference)
     }
 }