std: Modernize the local_data api

This commit brings the local_data api up to modern rust standards with a few key
improvements:

* The `pop` and `set` methods have been combined into one method, `replace`

* The `get_mut` method has been removed. All interior mutability should be done
  through `RefCell`.

* All functionality is now exposed as a method on the keys themselves. Instead
  of importing std::local_data, you now use "key.replace()" and "key.get()".

* All closures have been removed in favor of RAII functionality. This means that
  get() and get_mut() no long require closures, but rather return
  Option<SmartPointer> where the smart pointer takes care of relinquishing the
  borrow and also implements the necessary Deref traits

* The modify() function was removed to cut the local_data interface down to its
  bare essentials (similarly to how RefCell removed set/get).

[breaking-change]
This commit is contained in:
Alex Crichton 2014-04-28 20:36:08 -07:00
parent ef6daf9935
commit ab92ea526d
23 changed files with 444 additions and 661 deletions

View file

@ -79,7 +79,6 @@ println!("{:?}", tuple_ptr)
use std::cast;
use std::io::IoResult;
use std::kinds::marker;
use std::local_data;
use std::strbuf::StrBuf;
pub use isaac::{IsaacRng, Isaac64Rng};
@ -581,9 +580,6 @@ pub struct TaskRng {
marker: marker::NoSend,
}
// used to make space in TLS for a random number generator
local_data_key!(TASK_RNG_KEY: Box<TaskRngInner>)
/// Retrieve the lazily-initialized task-local random number
/// generator, seeded by the system. Intended to be used in method
/// chaining style, e.g. `task_rng().gen::<int>()`.
@ -596,7 +592,10 @@ local_data_key!(TASK_RNG_KEY: Box<TaskRngInner>)
/// the same sequence always. If absolute consistency is required,
/// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
pub fn task_rng() -> TaskRng {
local_data::get_mut(TASK_RNG_KEY, |rng| match rng {
// used to make space in TLS for a random number generator
local_data_key!(TASK_RNG_KEY: Box<TaskRngInner>)
match TASK_RNG_KEY.get() {
None => {
let r = match StdRng::new() {
Ok(r) => r,
@ -607,12 +606,15 @@ pub fn task_rng() -> TaskRng {
TaskRngReseeder);
let ptr = &mut *rng as *mut TaskRngInner;
local_data::set(TASK_RNG_KEY, rng);
TASK_RNG_KEY.replace(Some(rng));
TaskRng { rng: ptr, marker: marker::NoSend }
}
Some(rng) => TaskRng { rng: &mut **rng, marker: marker::NoSend }
})
Some(rng) => TaskRng {
rng: &**rng as *_ as *mut TaskRngInner,
marker: marker::NoSend
}
}
}
impl Rng for TaskRng {