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<RefCell<Option<T>>> = Rc::new(RefCell::new(None)))
The old `local_data` module inherently contained the `Rc<RefCell<Option<T>>>` as
an implementation detail which must now be explicitly stated by users.
[rfc]: https://github.com/rust-lang/rfcs/pull/461
[breaking-change]
This commit is contained in:
@@ -25,21 +25,20 @@ use parse::token::InternedString;
|
||||
use parse::token;
|
||||
use ptr::P;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::cell::{RefCell, Cell};
|
||||
use std::collections::BitvSet;
|
||||
use std::collections::HashSet;
|
||||
|
||||
local_data_key!(used_attrs: BitvSet)
|
||||
thread_local!(static USED_ATTRS: RefCell<BitvSet> = RefCell::new(BitvSet::new()))
|
||||
|
||||
pub fn mark_used(attr: &Attribute) {
|
||||
let mut used = used_attrs.replace(None).unwrap_or_else(|| BitvSet::new());
|
||||
let AttrId(id) = attr.node.id;
|
||||
used.insert(id);
|
||||
used_attrs.replace(Some(used));
|
||||
USED_ATTRS.with(|slot| slot.borrow_mut().insert(id));
|
||||
}
|
||||
|
||||
pub fn is_used(attr: &Attribute) -> bool {
|
||||
let AttrId(id) = attr.node.id;
|
||||
used_attrs.get().map_or(false, |used| used.contains(&id))
|
||||
USED_ATTRS.with(|slot| slot.borrow().contains(&id))
|
||||
}
|
||||
|
||||
pub trait AttrMetaMethods {
|
||||
@@ -167,11 +166,14 @@ pub fn mk_word_item(name: InternedString) -> P<MetaItem> {
|
||||
P(dummy_spanned(MetaWord(name)))
|
||||
}
|
||||
|
||||
local_data_key!(next_attr_id: uint)
|
||||
thread_local!(static NEXT_ATTR_ID: Cell<uint> = Cell::new(0))
|
||||
|
||||
pub fn mk_attr_id() -> AttrId {
|
||||
let id = next_attr_id.replace(None).unwrap_or(0);
|
||||
next_attr_id.replace(Some(id + 1));
|
||||
let id = NEXT_ATTR_ID.with(|slot| {
|
||||
let r = slot.get();
|
||||
slot.set(r + 1);
|
||||
r
|
||||
});
|
||||
AttrId(id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user