Skip to content

Commit a1674b6

Browse files
committed
auto merge of #8534 : huonw/rust/tls-key-macro, r=alexcrichton
This allows the internal implementation details of the TLS keys to be changed without requiring the update of all the users. (Or, applying changes that *have* to be applied for the keys to work correctly, e.g. forcing LLVM to not merge these constants.)
2 parents 680eb71 + 3ad2355 commit a1674b6

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

src/libstd/local_data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ wish to store.
2424
~~~{.rust}
2525
use 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
3030
local_data::set(key_int, 3);
3131
local_data::get(key_int, |opt| assert_eq!(opt, Some(&3)));

src/libsyntax/ext/expand.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/test/compile-fail/core-tls-store-pointer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use 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

1818
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)