]> git.lizzy.rs Git - rust.git/commitdiff
support pub(restricted) in thread_local!
authorAlex Burka <aburka@seas.upenn.edu>
Sat, 1 Apr 2017 03:06:34 +0000 (23:06 -0400)
committerAlex Burka <alex@alexburka.com>
Tue, 11 Jul 2017 20:27:54 +0000 (20:27 +0000)
src/libstd/thread/local.rs
src/test/run-pass/thread-local-syntax.rs

index dad21473eae1e043f61c45aa96efc79b6c450e1a..49ceaff8d3efebcd4167065eda58530850eff038 100644 (file)
@@ -115,7 +115,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 /// # 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 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 #[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.
index a5967249b54543dcd3807544617bf13605470201..373824122fd51d90eca3c39aa387f55e4357afb3 100644 (file)
 #![deny(missing_docs)]
 //! this tests the syntax of `thread_local!`
 
-thread_local! {
-    // no docs
-    #[allow(unused)]
-    static FOO: i32 = 42;
-    /// docs
-    pub static BAR: String = String::from("bar");
+mod foo {
+    mod bar {
+        thread_local! {
+            // no docs
+            #[allow(unused)]
+            static FOO: i32 = 42;
+            /// docs
+            pub static BAR: String = String::from("bar");
+
+            // look at these restrictions!!
+            pub(crate) static BAZ: usize = 0;
+            pub(in foo) static QUUX: usize = 0;
+        }
+        thread_local!(static SPLOK: u32 = 0);
+    }
 }
-thread_local!(static BAZ: u32 = 0);
 
 fn main() {}