about summary refs log tree commit diff
path: root/src/libstd/thread
diff options
context:
space:
mode:
authorAlex Burka <aburka@seas.upenn.edu>2017-03-31 23:06:34 -0400
committerAlex Burka <alex@alexburka.com>2017-07-11 20:27:54 +0000
commitb6a2d7e82249bba271599732e16ffba176ae7de8 (patch)
tree91a59892ecf239db97bd9425abfe229d9c4cf56c /src/libstd/thread
parent1475e2c923746c253da8027079c462d7e3d1022d (diff)
downloadrust-b6a2d7e82249bba271599732e16ffba176ae7de8.tar.gz
rust-b6a2d7e82249bba271599732e16ffba176ae7de8.zip
support pub(restricted) in thread_local!
Diffstat (limited to 'src/libstd/thread')
-rw-r--r--src/libstd/thread/local.rs75
1 files changed, 43 insertions, 32 deletions
diff --git a/src/libstd/thread/local.rs b/src/libstd/thread/local.rs
index dad21473eae..49ceaff8d3e 100644
--- a/src/libstd/thread/local.rs
+++ b/src/libstd/thread/local.rs
@@ -115,7 +115,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
 /// # Syntax
 ///
 /// The macro wraps any number of static declarations and makes them thread local.
-/// Each static may be public or private, and attributes are allowed. Example:
+/// Publicity and attributes for each static are allowed. Example:
 ///
 /// ```
 /// use std::cell::RefCell;
@@ -136,31 +136,40 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
 #[stable(feature = "rust1", since = "1.0.0")]
 #[allow_internal_unstable]
 macro_rules! thread_local {
-    // rule 0: empty (base case for the recursion)
+    // empty (base case for the recursion)
     () => {};
 
-    // rule 1: process multiple declarations where the first one is private
+    // process multiple declarations where the first one is private
     ($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
-        thread_local!($(#[$attr])* static $name: $t = $init); // go to rule 2
+        __thread_local_inner!($(#[$attr])* [] $name, $t, $init);
         thread_local!($($rest)*);
     );
 
-    // rule 2: handle a single private declaration
+    // handle a single private declaration
     ($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
-        $(#[$attr])* static $name: $crate::thread::LocalKey<$t> =
-            __thread_local_inner!($t, $init);
+        __thread_local_inner!($(#[$attr])* [] $name, $t, $init);
     );
 
-    // rule 3: handle multiple declarations where the first one is public
+    // handle multiple declarations where the first one is public
     ($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
-        thread_local!($(#[$attr])* pub static $name: $t = $init); // go to rule 4
+        __thread_local_inner!($(#[$attr])* [pub] $name, $t, $init);
         thread_local!($($rest)*);
     );
 
-    // rule 4: handle a single public declaration
+    // handle a single public declaration
     ($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr) => (
-        $(#[$attr])* pub static $name: $crate::thread::LocalKey<$t> =
-            __thread_local_inner!($t, $init);
+        __thread_local_inner!($(#[$attr])* [pub] $name, $t, $init);
+    );
+
+    // handle multiple declarations where the first one is restricted public
+    ($(#[$attr:meta])* pub $vis:tt static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
+        __thread_local_inner!($(#[$attr])* [pub $vis] $name, $t, $init);
+        thread_local!($($rest)*);
+    );
+
+    // handle a single restricted public declaration
+    ($(#[$attr:meta])* pub $vis:tt static $name:ident: $t:ty = $init:expr) => (
+        __thread_local_inner!($(#[$attr])* [pub $vis] $name, $t, $init);
     );
 }
 
@@ -171,27 +180,29 @@ macro_rules! thread_local {
 #[macro_export]
 #[allow_internal_unstable]
 macro_rules! __thread_local_inner {
-    ($t:ty, $init:expr) => {{
-        fn __init() -> $t { $init }
-
-        fn __getit() -> $crate::option::Option<
-            &'static $crate::cell::UnsafeCell<
-                $crate::option::Option<$t>>>
-        {
-            #[thread_local]
-            #[cfg(target_thread_local)]
-            static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
-                $crate::thread::__FastLocalKeyInner::new();
-
-            #[cfg(not(target_thread_local))]
-            static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
-                $crate::thread::__OsLocalKeyInner::new();
-
-            __KEY.get()
-        }
+    ($(#[$attr:meta])* [$($vis:tt)*] $name:ident, $t:ty, $init:expr) => {
+        $(#[$attr])* $($vis)* static $name: $crate::thread::LocalKey<$t> = {
+            fn __init() -> $t { $init }
+
+            fn __getit() -> $crate::option::Option<
+                &'static $crate::cell::UnsafeCell<
+                    $crate::option::Option<$t>>>
+            {
+                #[thread_local]
+                #[cfg(target_thread_local)]
+                static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
+                    $crate::thread::__FastLocalKeyInner::new();
+
+                #[cfg(not(target_thread_local))]
+                static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
+                    $crate::thread::__OsLocalKeyInner::new();
+
+                __KEY.get()
+            }
 
-        $crate::thread::LocalKey::new(__getit, __init)
-    }}
+            $crate::thread::LocalKey::new(__getit, __init)
+        };
+    }
 }
 
 /// Indicator of the state of a thread local storage key.