Skip to content

Commit e62a653

Browse files
committed
Added support for public lazy statics
Updated travis config
1 parent bd9aadd commit e62a653

File tree

6 files changed

+67
-44
lines changed

6 files changed

+67
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target
22
doc
3+
Cargo.lock

.travis.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1+
language: rust
12
env:
23
global:
34
- secure: M9/ZDZMaLRBlM5uzmbQQEClBzy7aoaiSz6UVjepXsH9Q1+9bIO8WVAS8Pi2ywkSv+W7EUCkQNKx+zNt9jIkNb366dQxf8/cbhQPnJ9xpleo5CkA3Iul1aXGMusQU9IDDtaTuj0k0FUhiLxPaq/T4uogQ/QZGUypmKM8oRe13N1A=
4-
before_install:
5-
- yes | sudo add-apt-repository ppa:hansjorg/rust
6-
- yes | sudo add-apt-repository ppa:cmrx64/cargo
7-
- sudo apt-get update
8-
install:
9-
- sudo apt-get install rust-nightly cargo
105
script:
11-
- cargo build
12-
- cargo test
13-
- rustdoc src/lazy_static.rs
6+
- cargo build --verbose
7+
- cargo test --verbose
8+
- cargo doc
9+
- cp -r ./target/doc ./doc
1410
after_script:
1511
- curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ name = "lazy_static"
44
version = "0.1.0"
55
authors = [ "[email protected]" ]
66

7-
[[lib]]
7+
[lib]
88

99
name = "lazy_static"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ as well as anything that requires function calls to be computed.
1414

1515
```rust
1616
lazy_static! {
17-
static ref NAME_1: TYPE_1 = EXPR_1;
18-
static ref NAME_2: TYPE_2 = EXPR_2;
17+
[pub] static ref NAME_1: TYPE_1 = EXPR_1;
18+
[pub] static ref NAME_2: TYPE_2 = EXPR_2;
1919
...
20-
static ref NAME_N: TYPE_N = EXPR_N;
20+
[pub] static ref NAME_N: TYPE_N = EXPR_N;
2121
}
2222
```
2323

src/lazy_static.rs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ as well as anything that requires function calls to be computed.
88
99
# Syntax
1010
11-
```rust
11+
```ignore
1212
lazy_static! {
13-
static ref NAME_1: TYPE_1 = EXPR_1;
14-
static ref NAME_2: TYPE_2 = EXPR_2;
13+
[pub] static ref NAME_1: TYPE_1 = EXPR_1;
14+
[pub] static ref NAME_2: TYPE_2 = EXPR_2;
1515
...
16-
static ref NAME_N: TYPE_N = EXPR_N;
16+
[pub] static ref NAME_N: TYPE_N = EXPR_N;
1717
}
1818
```
1919
@@ -77,32 +77,47 @@ define uninitialized `static mut` values.
7777

7878
#[macro_export]
7979
macro_rules! lazy_static {
80-
($(static ref $N:ident : $T:ty = $e:expr;)*) => {
81-
$(
82-
#[allow(non_camel_case_types)]
83-
#[allow(dead_code)]
84-
struct $N {__private_field: ()}
85-
static $N: $N = $N {__private_field: ()};
86-
impl Deref<$T> for $N {
87-
fn deref<'a>(&'a self) -> &'a $T {
88-
use std::sync::{Once, ONCE_INIT};
89-
use std::mem::transmute;
90-
91-
#[inline(always)]
92-
fn require_sync<T: Sync>(_: &T) { }
93-
94-
unsafe {
95-
static mut s: *const $T = 0 as *const $T;
96-
static mut ONCE: Once = ONCE_INIT;
97-
ONCE.doit(|| {
98-
s = transmute::<Box<$T>, *const $T>(box() ($e));
99-
});
100-
let static_ref = &*s;
101-
require_sync(static_ref);
102-
static_ref
103-
}
80+
(static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
81+
lazy_static!(PRIV static ref $N : $T = $e; $($t)*)
82+
};
83+
(pub static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
84+
lazy_static!(PUB static ref $N : $T = $e; $($t)*)
85+
};
86+
($VIS:ident static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
87+
lazy_static!(MAKE TY $VIS $N)
88+
impl Deref<$T> for $N {
89+
fn deref<'a>(&'a self) -> &'a $T {
90+
use std::sync::{Once, ONCE_INIT};
91+
use std::mem::transmute;
92+
93+
#[inline(always)]
94+
fn require_sync<T: Sync>(_: &T) { }
95+
96+
unsafe {
97+
static mut s: *const $T = 0 as *const $T;
98+
static mut ONCE: Once = ONCE_INIT;
99+
ONCE.doit(|| {
100+
s = transmute::<Box<$T>, *const $T>(box() ($e));
101+
});
102+
let static_ref = &*s;
103+
require_sync(static_ref);
104+
static_ref
104105
}
105106
}
106-
)*
107-
}
107+
}
108+
lazy_static!($($t)*)
109+
};
110+
(MAKE TY PUB $N:ident) => {
111+
#[allow(non_camel_case_types)]
112+
#[allow(dead_code)]
113+
pub struct $N {__private_field: ()}
114+
pub static $N: $N = $N {__private_field: ()};
115+
};
116+
(MAKE TY PRIV $N:ident) => {
117+
#[allow(non_camel_case_types)]
118+
#[allow(dead_code)]
119+
struct $N {__private_field: ()}
120+
static $N: $N = $N {__private_field: ()};
121+
};
122+
() => ()
108123
}

tests/test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn test_basic() {
2828
assert_eq!(*NUMBER, 6);
2929
assert!(HASHMAP.find(&1).is_some());
3030
assert!(HASHMAP.find(&3).is_none());
31-
assert_eq!(ARRAY_BOXES.as_slice(), &[box 1, box 2, box 3]);
31+
assert_eq!(ARRAY_BOXES.as_slice(), [box 1, box 2, box 3].as_slice());
3232
}
3333

3434
#[test]
@@ -37,3 +37,14 @@ fn test_repeat() {
3737
assert_eq!(*NUMBER, 6);
3838
assert_eq!(*NUMBER, 6);
3939
}
40+
41+
mod visibility {
42+
lazy_static! {
43+
pub static ref FOO: Box<uint> = box 0u;
44+
}
45+
}
46+
47+
#[test]
48+
fn test_visibility() {
49+
assert_eq!(*visibility::FOO, box 0u);
50+
}

0 commit comments

Comments
 (0)