rust_extra/true_immutable_thread_local.rs
1// This file is part of rust-extra. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/rust-extra/master/COPYRIGHT. No part of rust-extra, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2016 The developers of rust-extra. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/rust-extra/master/COPYRIGHT.
3
4
5#[macro_export]
6macro_rules! true_immutable_thread_local
7{
8 ($type: ty, $initializer: block) =>
9 {
10 {
11 // Done this way so that values do not need to implement 'Sync', which is pointless for a thread local static...
12 #[thread_local] static mut THREAD_LOCAL: *const () = ::std::ptr::null_mut();
13 unsafe
14 {
15 if $crate::unlikely(THREAD_LOCAL.is_null())
16 {
17 let value = Box::new($initializer);
18 THREAD_LOCAL = Box::into_raw(value) as *const _;
19 }
20 &*(THREAD_LOCAL as *const $type)
21 }
22 }
23 }
24}