support pub(restricted) in thread_local!
This commit is contained in:
@@ -115,7 +115,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
|
|||||||
/// # Syntax
|
/// # Syntax
|
||||||
///
|
///
|
||||||
/// The macro wraps any number of static declarations and makes them thread local.
|
/// 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;
|
/// use std::cell::RefCell;
|
||||||
@@ -136,31 +136,40 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
|
|||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[allow_internal_unstable]
|
#[allow_internal_unstable]
|
||||||
macro_rules! thread_local {
|
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)*) => (
|
($(#[$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)*);
|
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:meta])* static $name:ident: $t:ty = $init:expr) => (
|
||||||
$(#[$attr])* static $name: $crate::thread::LocalKey<$t> =
|
__thread_local_inner!($(#[$attr])* [] $name, $t, $init);
|
||||||
__thread_local_inner!($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)*) => (
|
($(#[$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)*);
|
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:meta])* pub static $name:ident: $t:ty = $init:expr) => (
|
||||||
$(#[$attr])* pub static $name: $crate::thread::LocalKey<$t> =
|
__thread_local_inner!($(#[$attr])* [pub] $name, $t, $init);
|
||||||
__thread_local_inner!($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,7 +180,8 @@ macro_rules! thread_local {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[allow_internal_unstable]
|
#[allow_internal_unstable]
|
||||||
macro_rules! __thread_local_inner {
|
macro_rules! __thread_local_inner {
|
||||||
($t:ty, $init:expr) => {{
|
($(#[$attr:meta])* [$($vis:tt)*] $name:ident, $t:ty, $init:expr) => {
|
||||||
|
$(#[$attr])* $($vis)* static $name: $crate::thread::LocalKey<$t> = {
|
||||||
fn __init() -> $t { $init }
|
fn __init() -> $t { $init }
|
||||||
|
|
||||||
fn __getit() -> $crate::option::Option<
|
fn __getit() -> $crate::option::Option<
|
||||||
@@ -191,7 +201,8 @@ macro_rules! __thread_local_inner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$crate::thread::LocalKey::new(__getit, __init)
|
$crate::thread::LocalKey::new(__getit, __init)
|
||||||
}}
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Indicator of the state of a thread local storage key.
|
/// Indicator of the state of a thread local storage key.
|
||||||
|
|||||||
@@ -11,13 +11,21 @@
|
|||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
//! this tests the syntax of `thread_local!`
|
//! this tests the syntax of `thread_local!`
|
||||||
|
|
||||||
thread_local! {
|
mod foo {
|
||||||
|
mod bar {
|
||||||
|
thread_local! {
|
||||||
// no docs
|
// no docs
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
static FOO: i32 = 42;
|
static FOO: i32 = 42;
|
||||||
/// docs
|
/// docs
|
||||||
pub static BAR: String = String::from("bar");
|
pub static BAR: String = String::from("bar");
|
||||||
|
|
||||||
|
// look at these restrictions!!
|
||||||
|
pub(crate) static BAZ: usize = 0;
|
||||||
|
pub(in foo) static QUUX: usize = 0;
|
||||||
|
}
|
||||||
|
thread_local!(static SPLOK: u32 = 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
thread_local!(static BAZ: u32 = 0);
|
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|||||||
Reference in New Issue
Block a user