@@ -110,6 +110,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
110
110
}
111
111
}
112
112
113
+ #[ cfg( not( stage0) ) ]
113
114
/// Declare a new thread local storage key of type [`std::thread::LocalKey`].
114
115
///
115
116
/// # Syntax
@@ -151,6 +152,7 @@ macro_rules! thread_local {
151
152
) ;
152
153
}
153
154
155
+ #[ cfg( not( stage0) ) ]
154
156
#[ doc( hidden) ]
155
157
#[ unstable( feature = "thread_local_internals" ,
156
158
reason = "should not be necessary" ,
@@ -183,6 +185,71 @@ macro_rules! __thread_local_inner {
183
185
}
184
186
}
185
187
188
+ #[ cfg( stage0) ]
189
+ /// Declare a new thread local storage key of type `std::thread::LocalKey`.
190
+ #[ macro_export]
191
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
192
+ #[ allow_internal_unstable]
193
+ macro_rules! thread_local {
194
+ // rule 0: empty (base case for the recursion)
195
+ ( ) => { } ;
196
+
197
+ // rule 1: process multiple declarations where the first one is private
198
+ ( $( #[ $attr: meta] ) * static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
199
+ thread_local!( $( #[ $attr] ) * static $name: $t = $init) ; // go to rule 2
200
+ thread_local!( $( $rest) * ) ;
201
+ ) ;
202
+
203
+ // rule 2: handle a single private declaration
204
+ ( $( #[ $attr: meta] ) * static $name: ident: $t: ty = $init: expr) => (
205
+ $( #[ $attr] ) * static $name: $crate:: thread:: LocalKey <$t> =
206
+ __thread_local_inner!( $t, $init) ;
207
+ ) ;
208
+
209
+ // rule 3: handle multiple declarations where the first one is public
210
+ ( $( #[ $attr: meta] ) * pub static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
211
+ thread_local!( $( #[ $attr] ) * pub static $name: $t = $init) ; // go to rule 4
212
+ thread_local!( $( $rest) * ) ;
213
+ ) ;
214
+
215
+ // rule 4: handle a single public declaration
216
+ ( $( #[ $attr: meta] ) * pub static $name: ident: $t: ty = $init: expr) => (
217
+ $( #[ $attr] ) * pub static $name: $crate:: thread:: LocalKey <$t> =
218
+ __thread_local_inner!( $t, $init) ;
219
+ ) ;
220
+ }
221
+
222
+ #[ cfg( stage0) ]
223
+ #[ doc( hidden) ]
224
+ #[ unstable( feature = "thread_local_internals" ,
225
+ reason = "should not be necessary" ,
226
+ issue = "0" ) ]
227
+ #[ macro_export]
228
+ #[ allow_internal_unstable]
229
+ macro_rules! __thread_local_inner {
230
+ ( $t: ty, $init: expr) => { {
231
+ fn __init( ) -> $t { $init }
232
+
233
+ fn __getit( ) -> $crate:: option:: Option <
234
+ & ' static $crate:: cell:: UnsafeCell <
235
+ $crate:: option:: Option <$t>>>
236
+ {
237
+ #[ thread_local]
238
+ #[ cfg( target_thread_local) ]
239
+ static __KEY: $crate:: thread:: __FastLocalKeyInner<$t> =
240
+ $crate:: thread:: __FastLocalKeyInner:: new( ) ;
241
+
242
+ #[ cfg( not( target_thread_local) ) ]
243
+ static __KEY: $crate:: thread:: __OsLocalKeyInner<$t> =
244
+ $crate:: thread:: __OsLocalKeyInner:: new( ) ;
245
+
246
+ __KEY. get( )
247
+ }
248
+
249
+ $crate:: thread:: LocalKey :: new( __getit, __init)
250
+ } }
251
+ }
252
+
186
253
/// Indicator of the state of a thread local storage key.
187
254
#[ unstable( feature = "thread_local_state" ,
188
255
reason = "state querying was recently added" ,
0 commit comments