diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-11-14 14:20:57 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-11-23 23:37:16 -0800 |
| commit | a9c1152c4bf72132806cb76045b3464d59db07da (patch) | |
| tree | 89ba92d5f5788e3323b75ca003bf74661a94e4de /src/libstd/rand | |
| parent | 4e5259503cd8aac9905c7ac6d68d0c4caab1d28c (diff) | |
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]
Diffstat (limited to 'src/libstd/rand')
| -rw-r--r-- | src/libstd/rand/mod.rs | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs index 08eb7350bcf..f9f9147b107 100644 --- a/src/libstd/rand/mod.rs +++ b/src/libstd/rand/mod.rs @@ -226,7 +226,6 @@ use clone::Clone; use io::IoResult; use iter::Iterator; use mem; -use option::{Some, None}; use rc::Rc; use result::{Ok, Err}; use vec::Vec; @@ -337,24 +336,18 @@ pub struct TaskRng { /// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`. pub fn task_rng() -> TaskRng { // used to make space in TLS for a random number generator - local_data_key!(TASK_RNG_KEY: Rc<RefCell<TaskRngInner>>) - - match TASK_RNG_KEY.get() { - None => { - let r = match StdRng::new() { - Ok(r) => r, - Err(e) => panic!("could not initialize task_rng: {}", e) - }; - let rng = reseeding::ReseedingRng::new(r, - TASK_RNG_RESEED_THRESHOLD, - TaskRngReseeder); - let rng = Rc::new(RefCell::new(rng)); - TASK_RNG_KEY.replace(Some(rng.clone())); - - TaskRng { rng: rng } - } - Some(rng) => TaskRng { rng: rng.clone() } - } + thread_local!(static TASK_RNG_KEY: Rc<RefCell<TaskRngInner>> = { + let r = match StdRng::new() { + Ok(r) => r, + Err(e) => panic!("could not initialize task_rng: {}", e) + }; + let rng = reseeding::ReseedingRng::new(r, + TASK_RNG_RESEED_THRESHOLD, + TaskRngReseeder); + Rc::new(RefCell::new(rng)) + }) + + TaskRng { rng: TASK_RNG_KEY.with(|t| t.clone()) } } impl Rng for TaskRng { |
