#[macro_export]
macro_rules! with_thread_local {
(
$(
static $name:ident : $ty:ty = $init:expr;
)+
$block:block
) => {{
thread_local! {
#[allow(unused_parens)]
static _THIS_LOCAL: ($($ty),+) = ($($init),+);
}
_THIS_LOCAL.with(|#[allow(non_snake_case, unused_parens)] ($($name),+)| $block)
}};
(
$(
static $name:ident : $ty:ty = $init:expr;
)+
move $block:block
) => {{
thread_local! {
#[allow(unused_parens)]
static _THIS_LOCAL: ($($ty),+) = ($($init),+);
}
_THIS_LOCAL.with(move |#[allow(non_snake_case, unused_parens)] ($($name),+)| $block)
}};
}
#[cfg(test)]
mod test {
#[test]
fn use_one() {
let res = with_thread_local! {
static ONE: usize = 42;
{
*ONE
}
};
assert_eq!(res, 42);
}
#[test]
fn use_two() {
let res = with_thread_local! {
static ONE: usize = 42;
static TWO: usize = 2;
{
*ONE + *TWO
}
};
assert_eq!(res, 44);
}
}