File tree Expand file tree Collapse file tree 5 files changed +64
-3
lines changed Expand file tree Collapse file tree 5 files changed +64
-3
lines changed Original file line number Diff line number Diff line change @@ -24,8 +24,8 @@ wish to store.
2424~~~{.rust}
2525use std::local_data;
2626
27- static key_int: local_data::Key< int> = &local_data::Key ;
28- static key_vector: local_data::Key< ~[int]> = &local_data::Key ;
27+ local_data_key!( key_int: int) ;
28+ local_data_key!( key_vector: ~[int]) ;
2929
3030local_data::set(key_int, 3);
3131local_data::get(key_int, |opt| assert_eq!(opt, Some(&3)));
Original file line number Diff line number Diff line change @@ -957,6 +957,17 @@ pub fn std_macros() -> @str {
957957 println( fmt!( $( $arg) ,+) )
958958 )
959959 )
960+
961+ // NOTE: use this after a snapshot lands to abstract the details
962+ // of the TLS interface.
963+ macro_rules! local_data_key (
964+ ( $name: ident: $ty: ty) => (
965+ static $name: :: std:: local_data:: Key <$ty> = & :: std:: local_data:: Key ;
966+ ) ;
967+ ( pub $name: ident: $ty: ty) => (
968+ pub static $name: :: std:: local_data:: Key <$ty> = & :: std:: local_data:: Key ;
969+ )
970+ )
960971} ";
961972}
962973
Original file line number Diff line number Diff line change 1212
1313use std:: local_data;
1414
15- static key: local_data :: Key < @& int > = & local_data :: Key ;
15+ local_data_key ! ( key: @& int)
1616//~^ ERROR only 'static is allowed
1717
1818fn main ( ) { }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+ // file at the top-level directory of this distribution and at
3+ // http://rust-lang.org/COPYRIGHT.
4+ //
5+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+ // option. This file may not be copied, modified, or distributed
9+ // except according to those terms.
10+
11+ use std:: local_data;
12+
13+ // check that the local data keys are private by default.
14+
15+ mod bar {
16+ local_data_key ! ( baz: float)
17+ }
18+
19+ fn main ( ) {
20+ local_data:: set ( bar:: baz, -10.0 ) ;
21+ //~^ ERROR unresolved name `bar::baz`
22+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+ // file at the top-level directory of this distribution and at
3+ // http://rust-lang.org/COPYRIGHT.
4+ //
5+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+ // option. This file may not be copied, modified, or distributed
9+ // except according to those terms.
10+
11+ use std:: local_data;
12+
13+ local_data_key ! ( foo: int)
14+
15+ mod bar {
16+ local_data_key ! ( pub baz: float)
17+ }
18+
19+ fn main ( ) {
20+ local_data:: get ( foo, |x| assert ! ( x. is_none( ) ) ) ;
21+ local_data:: get ( bar:: baz, |y| assert ! ( y. is_none( ) ) ) ;
22+
23+ local_data:: set ( foo, 3 ) ;
24+ local_data:: set ( bar:: baz, -10.0 ) ;
25+
26+ local_data:: get ( foo, |x| assert_eq ! ( * x. unwrap( ) , 3 ) ) ;
27+ local_data:: get ( bar:: baz, |y| assert_eq ! ( * y. unwrap( ) , -10.0 ) ) ;
28+ }
You can’t perform that action at this time.
0 commit comments