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:
parent
ef6daf9935
commit
ab92ea526d
23 changed files with 444 additions and 661 deletions
|
|
@ -15,7 +15,6 @@
|
|||
extern crate syntax;
|
||||
|
||||
use std::any::Any;
|
||||
use std::local_data;
|
||||
use syntax::ast::Name;
|
||||
use syntax::ext::base::SyntaxExtension;
|
||||
|
||||
|
|
@ -30,6 +29,6 @@ impl Drop for Foo {
|
|||
#[macro_registrar]
|
||||
pub fn registrar(_: |Name, SyntaxExtension|) {
|
||||
local_data_key!(foo: Box<Any:Send>);
|
||||
local_data::set(foo, box Foo { foo: 10 } as Box<Any:Send>);
|
||||
foo.replace(Some(box Foo { foo: 10 } as Box<Any:Send>));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,8 +10,6 @@
|
|||
|
||||
// Testing that we can't store a reference it task-local storage
|
||||
|
||||
use std::local_data;
|
||||
|
||||
local_data_key!(key: @&int)
|
||||
//~^ ERROR missing lifetime specifier
|
||||
|
||||
|
|
|
|||
|
|
@ -8,8 +8,6 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::local_data;
|
||||
|
||||
// check that the local data keys are private by default.
|
||||
|
||||
mod bar {
|
||||
|
|
@ -17,6 +15,6 @@ mod bar {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
local_data::set(bar::baz, -10.0);
|
||||
bar::baz.replace(Some(-10.0));
|
||||
//~^ ERROR static `baz` is private
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,6 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::local_data;
|
||||
|
||||
local_data_key!(foo: int)
|
||||
|
||||
mod bar {
|
||||
|
|
@ -17,12 +15,12 @@ mod bar {
|
|||
}
|
||||
|
||||
pub fn main() {
|
||||
local_data::get(foo, |x| assert!(x.is_none()));
|
||||
local_data::get(bar::baz, |y| assert!(y.is_none()));
|
||||
assert!(foo.get().is_none());
|
||||
assert!(bar::baz.get().is_none());
|
||||
|
||||
local_data::set(foo, 3);
|
||||
local_data::set(bar::baz, -10.0);
|
||||
foo.replace(Some(3));
|
||||
bar::baz.replace(Some(-10.0));
|
||||
|
||||
local_data::get(foo, |x| assert_eq!(*x.unwrap(), 3));
|
||||
local_data::get(bar::baz, |y| assert_eq!(*y.unwrap(), -10.0));
|
||||
assert_eq!(*foo.get().unwrap(), 3);
|
||||
assert_eq!(*bar::baz.get().unwrap(), -10.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue