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