support pub(restricted) in thread_local!
This commit is contained in:
@@ -115,7 +115,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
|
||||
/// # 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 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
|
||||
#[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 }
|
||||
($(#[$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();
|
||||
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();
|
||||
#[cfg(not(target_thread_local))]
|
||||
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
|
||||
$crate::thread::__OsLocalKeyInner::new();
|
||||
|
||||
__KEY.get()
|
||||
}
|
||||
__KEY.get()
|
||||
}
|
||||
|
||||
$crate::thread::LocalKey::new(__getit, __init)
|
||||
}}
|
||||
$crate::thread::LocalKey::new(__getit, __init)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Indicator of the state of a thread local storage key.
|
||||
|
||||
Reference in New Issue
Block a user