@@ -100,18 +100,52 @@ pub struct LocalKey<T: 'static> {
100100
101101/// Declare a new thread local storage key of type `std::thread::LocalKey`.
102102///
103+ /// # Syntax
104+ ///
105+ /// The macro wraps any number of static declarations and makes them thread local.
106+ /// Each static may be public or private, and attributes are allowed. Example:
107+ ///
108+ /// ```
109+ /// use std::cell::RefCell;
110+ /// thread_local! {
111+ /// pub static FOO: RefCell<u32> = RefCell::new(1);
112+ ///
113+ /// #[allow(unused)]
114+ /// static BAR: RefCell<f32> = RefCell::new(1.0);
115+ /// }
116+ /// # fn main() {}
117+ /// ```
118+ ///
103119/// See [LocalKey documentation](thread/struct.LocalKey.html) for more
104120/// information.
105121#[ macro_export]
106122#[ stable( feature = "rust1" , since = "1.0.0" ) ]
107123#[ allow_internal_unstable]
108124macro_rules! thread_local {
109- ( static $name: ident: $t: ty = $init: expr) => (
110- static $name: $crate:: thread:: LocalKey <$t> =
125+ // rule 0: empty (base case for the recursion)
126+ ( ) => { } ;
127+
128+ // rule 1: process multiple declarations where the first one is private
129+ ( $( #[ $attr: meta] ) * static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
130+ thread_local!( $( #[ $attr] ) * static $name: $t = $init) ; // go to rule 2
131+ thread_local!( $( $rest) * ) ;
132+ ) ;
133+
134+ // rule 2: handle a single private declaration
135+ ( $( #[ $attr: meta] ) * static $name: ident: $t: ty = $init: expr) => (
136+ $( #[ $attr] ) * static $name: $crate:: thread:: LocalKey <$t> =
111137 __thread_local_inner!( $t, $init) ;
112138 ) ;
113- ( pub static $name: ident: $t: ty = $init: expr) => (
114- pub static $name: $crate:: thread:: LocalKey <$t> =
139+
140+ // rule 3: handle multiple declarations where the first one is public
141+ ( $( #[ $attr: meta] ) * pub static $name: ident: $t: ty = $init: expr; $( $rest: tt) * ) => (
142+ thread_local!( $( #[ $attr] ) * pub static $name: $t = $init) ; // go to rule 4
143+ thread_local!( $( $rest) * ) ;
144+ ) ;
145+
146+ // rule 4: handle a single public declaration
147+ ( $( #[ $attr: meta] ) * pub static $name: ident: $t: ty = $init: expr) => (
148+ $( #[ $attr] ) * pub static $name: $crate:: thread:: LocalKey <$t> =
115149 __thread_local_inner!( $t, $init) ;
116150 ) ;
117151}
0 commit comments