ab92ea526d
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]
27 lines
784 B
Rust
27 lines
784 B
Rust
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
local_data_key!(foo: int)
|
|
|
|
mod bar {
|
|
local_data_key!(pub baz: f64)
|
|
}
|
|
|
|
pub fn main() {
|
|
assert!(foo.get().is_none());
|
|
assert!(bar::baz.get().is_none());
|
|
|
|
foo.replace(Some(3));
|
|
bar::baz.replace(Some(-10.0));
|
|
|
|
assert_eq!(*foo.get().unwrap(), 3);
|
|
assert_eq!(*bar::baz.get().unwrap(), -10.0);
|
|
}
|