Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
support pub(restricted) in thread_local!
  • Loading branch information
durka committed Jul 11, 2017
1 parent 1475e2c commit b6a2d7e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
75 changes: 43 additions & 32 deletions src/libstd/thread/local.rs
Expand Up @@ -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;
Expand All @@ -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);
);
}

Expand All @@ -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 }

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();

__KEY.get()
}
($(#[$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();

#[cfg(not(target_thread_local))]
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
$crate::thread::__OsLocalKeyInner::new();

__KEY.get()
}

$crate::thread::LocalKey::new(__getit, __init)
}}
$crate::thread::LocalKey::new(__getit, __init)
};
}
}

/// Indicator of the state of a thread local storage key.
Expand Down
22 changes: 15 additions & 7 deletions src/test/run-pass/thread-local-syntax.rs
Expand Up @@ -11,13 +11,21 @@
#![deny(missing_docs)]
//! this tests the syntax of `thread_local!`

thread_local! {
// no docs
#[allow(unused)]
static FOO: i32 = 42;
/// docs
pub static BAR: String = String::from("bar");
mod foo {
mod bar {
thread_local! {
// no docs
#[allow(unused)]
static FOO: i32 = 42;
/// docs
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() {}

0 comments on commit b6a2d7e

Please sign in to comment.