@@ -111,7 +111,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
111
111
/// # Syntax
112
112
///
113
113
/// The macro wraps any number of static declarations and makes them thread local.
114
- /// Each static may be public or private, and attributes are allowed. Example:
114
+ /// Publicity and attributes for each static are allowed. Example:
115
115
///
116
116
/// ```
117
117
/// use std::cell::RefCell;
@@ -130,31 +130,40 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
130
130
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
131
131
#[ allow_internal_unstable]
132
132
macro_rules! thread_local {
133
- // rule 0: empty (base case for the recursion)
133
+ // empty (base case for the recursion)
134
134
( ) => { } ;
135
135
136
- // rule 1: process multiple declarations where the first one is private
136
+ // process multiple declarations where the first one is private
137
137
( $( #[ $attr: meta] ) * static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
138
- thread_local !( $( #[ $attr] ) * static $name: $t = $init) ; // go to rule 2
138
+ __thread_local_inner !( $( #[ $attr] ) * [ ] $name, $t, $init) ;
139
139
thread_local!( $( $rest) * ) ;
140
140
) ;
141
141
142
- // rule 2: handle a single private declaration
142
+ // handle a single private declaration
143
143
( $( #[ $attr: meta] ) * static $name: ident: $t: ty = $init: expr) => (
144
- $( #[ $attr] ) * static $name: $crate:: thread:: LocalKey <$t> =
145
- __thread_local_inner!( $t, $init) ;
144
+ __thread_local_inner!( $( #[ $attr] ) * [ ] $name, $t, $init) ;
146
145
) ;
147
146
148
- // rule 3: handle multiple declarations where the first one is public
147
+ // handle multiple declarations where the first one is public
149
148
( $( #[ $attr: meta] ) * pub static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
150
- thread_local !( $( #[ $attr] ) * pub static $name: $t = $init) ; // go to rule 4
149
+ __thread_local_inner !( $( #[ $attr] ) * [ pub ] $name, $t, $init) ;
151
150
thread_local!( $( $rest) * ) ;
152
151
) ;
153
152
154
- // rule 4: handle a single public declaration
153
+ // handle a single public declaration
155
154
( $( #[ $attr: meta] ) * pub static $name: ident: $t: ty = $init: expr) => (
156
- $( #[ $attr] ) * pub static $name: $crate:: thread:: LocalKey <$t> =
157
- __thread_local_inner!( $t, $init) ;
155
+ __thread_local_inner!( $( #[ $attr] ) * [ pub ] $name, $t, $init) ;
156
+ ) ;
157
+
158
+ // handle multiple declarations where the first one is restricted public
159
+ ( $( #[ $attr: meta] ) * pub $vis: tt static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
160
+ __thread_local_inner!( $( #[ $attr] ) * [ pub $vis] $name, $t, $init) ;
161
+ thread_local!( $( $rest) * ) ;
162
+ ) ;
163
+
164
+ // handle a single restricted public declaration
165
+ ( $( #[ $attr: meta] ) * pub $vis: tt static $name: ident: $t: ty = $init: expr) => (
166
+ __thread_local_inner!( $( #[ $attr] ) * [ pub $vis] $name, $t, $init) ;
158
167
) ;
159
168
}
160
169
@@ -165,27 +174,29 @@ macro_rules! thread_local {
165
174
#[ macro_export]
166
175
#[ allow_internal_unstable]
167
176
macro_rules! __thread_local_inner {
168
- ( $t: ty, $init: expr) => { {
169
- fn __init( ) -> $t { $init }
170
-
171
- fn __getit( ) -> $crate:: option:: Option <
172
- & ' static $crate:: cell:: UnsafeCell <
173
- $crate:: option:: Option <$t>>>
174
- {
175
- #[ thread_local]
176
- #[ cfg( target_thread_local) ]
177
- static __KEY: $crate:: thread:: __FastLocalKeyInner<$t> =
178
- $crate:: thread:: __FastLocalKeyInner:: new( ) ;
179
-
180
- #[ cfg( not( target_thread_local) ) ]
181
- static __KEY: $crate:: thread:: __OsLocalKeyInner<$t> =
182
- $crate:: thread:: __OsLocalKeyInner:: new( ) ;
183
-
184
- __KEY. get( )
185
- }
177
+ ( $( #[ $attr: meta] ) * [ $( $vis: tt) * ] $name: ident, $t: ty, $init: expr) => {
178
+ $( #[ $attr] ) * $( $vis) * static $name: $crate:: thread:: LocalKey <$t> = {
179
+ fn __init( ) -> $t { $init }
180
+
181
+ fn __getit( ) -> $crate:: option:: Option <
182
+ & ' static $crate:: cell:: UnsafeCell <
183
+ $crate:: option:: Option <$t>>>
184
+ {
185
+ #[ thread_local]
186
+ #[ cfg( target_thread_local) ]
187
+ static __KEY: $crate:: thread:: __FastLocalKeyInner<$t> =
188
+ $crate:: thread:: __FastLocalKeyInner:: new( ) ;
189
+
190
+ #[ cfg( not( target_thread_local) ) ]
191
+ static __KEY: $crate:: thread:: __OsLocalKeyInner<$t> =
192
+ $crate:: thread:: __OsLocalKeyInner:: new( ) ;
193
+
194
+ __KEY. get( )
195
+ }
186
196
187
- $crate:: thread:: LocalKey :: new( __getit, __init)
188
- } }
197
+ $crate:: thread:: LocalKey :: new( __getit, __init)
198
+ } ;
199
+ }
189
200
}
190
201
191
202
/// Indicator of the state of a thread local storage key.
0 commit comments