From a9c1152c4bf72132806cb76045b3464d59db07da Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 14 Nov 2014 14:20:57 -0800 Subject: std: Add a new top-level thread_local module This commit removes the `std::local_data` module in favor of a new `std::thread_local` module providing thread local storage. The module provides two variants of TLS: one which owns its contents and one which is based on scoped references. Each implementation has pros and cons listed in the documentation. Both flavors have accessors through a function called `with` which yield a reference to a closure provided. Both flavors also panic if a reference cannot be yielded and provide a function to test whether an access would panic or not. This is an implementation of [RFC 461][rfc] and full details can be found in that RFC. This is a breaking change due to the removal of the `std::local_data` module. All users can migrate to the new thread local system like so: thread_local!(static FOO: Rc>> = Rc::new(RefCell::new(None))) The old `local_data` module inherently contained the `Rc>>` as an implementation detail which must now be explicitly stated by users. [rfc]: https://github.com/rust-lang/rfcs/pull/461 [breaking-change] --- src/libsyntax/diagnostics/plugin.rs | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src/libsyntax/diagnostics') diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index 281bde3129a..5f4e675aad5 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -18,31 +18,23 @@ use ext::build::AstBuilder; use parse::token; use ptr::P; -local_data_key!(registered_diagnostics: RefCell>>) -local_data_key!(used_diagnostics: RefCell>) +thread_local!(static REGISTERED_DIAGNOSTICS: RefCell>> = { + RefCell::new(HashMap::new()) +}) +thread_local!(static USED_DIAGNOSTICS: RefCell> = { + RefCell::new(HashMap::new()) +}) fn with_registered_diagnostics(f: |&mut HashMap>| -> T) -> T { - match registered_diagnostics.get() { - Some(cell) => f(cell.borrow_mut().deref_mut()), - None => { - let mut map = HashMap::new(); - let value = f(&mut map); - registered_diagnostics.replace(Some(RefCell::new(map))); - value - } - } + REGISTERED_DIAGNOSTICS.with(|slot| { + f(&mut *slot.borrow_mut()) + }) } fn with_used_diagnostics(f: |&mut HashMap| -> T) -> T { - match used_diagnostics.get() { - Some(cell) => f(cell.borrow_mut().deref_mut()), - None => { - let mut map = HashMap::new(); - let value = f(&mut map); - used_diagnostics.replace(Some(RefCell::new(map))); - value - } - } + USED_DIAGNOSTICS.with(|slot| { + f(&mut *slot.borrow_mut()) + }) } pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, -- cgit 1.4.1-3-g733a5