diff options
author | lurchi <lurchi@strangeplace.net> | 2016-08-14 12:43:05 +0200 |
---|---|---|
committer | lurchi <lurchi@strangeplace.net> | 2016-08-14 12:43:05 +0200 |
commit | 0372b4a9b8c788ace44a52430d29f4e3ece1c119 (patch) | |
tree | edbfff1e6ccb65875d2878652d1c5cb8abb10921 | |
parent | 702d163cf379ea1a3f225b3ac349f73d0abcb903 (diff) | |
download | secushare-0372b4a9b8c788ace44a52430d29f4e3ece1c119.tar.gz secushare-0372b4a9b8c788ace44a52430d29f4e3ece1c119.zip |
add lazy-static subtree
-rw-r--r-- | third_party/lazy-static.rs/.gitignore | 4 | ||||
-rw-r--r-- | third_party/lazy-static.rs/.travis.yml | 24 | ||||
-rw-r--r-- | third_party/lazy-static.rs/Cargo.toml | 20 | ||||
-rw-r--r-- | third_party/lazy-static.rs/LICENSE | 21 | ||||
-rw-r--r-- | third_party/lazy-static.rs/README.md | 81 | ||||
-rw-r--r-- | third_party/lazy-static.rs/src/core_lazy.rs | 27 | ||||
-rw-r--r-- | third_party/lazy-static.rs/src/lazy.rs | 32 | ||||
-rw-r--r-- | third_party/lazy-static.rs/src/lib.rs | 137 | ||||
-rw-r--r-- | third_party/lazy-static.rs/src/nightly_lazy.rs | 40 | ||||
-rw-r--r-- | third_party/lazy-static.rs/tests/no_std.rs | 21 | ||||
-rw-r--r-- | third_party/lazy-static.rs/tests/test.rs | 116 |
11 files changed, 523 insertions, 0 deletions
diff --git a/third_party/lazy-static.rs/.gitignore b/third_party/lazy-static.rs/.gitignore new file mode 100644 index 0000000..bde55ce --- /dev/null +++ b/third_party/lazy-static.rs/.gitignore | |||
@@ -0,0 +1,4 @@ | |||
1 | target | ||
2 | doc | ||
3 | Cargo.lock | ||
4 | .cargo | ||
diff --git a/third_party/lazy-static.rs/.travis.yml b/third_party/lazy-static.rs/.travis.yml new file mode 100644 index 0000000..2280263 --- /dev/null +++ b/third_party/lazy-static.rs/.travis.yml | |||
@@ -0,0 +1,24 @@ | |||
1 | language: rust | ||
2 | rust: | ||
3 | - nightly | ||
4 | - beta | ||
5 | - stable | ||
6 | before_script: | ||
7 | - | | ||
8 | pip install 'travis-cargo<0.2' --user && | ||
9 | export PATH=$HOME/.local/bin:$PATH | ||
10 | script: | ||
11 | - | | ||
12 | travis-cargo build && | ||
13 | travis-cargo test && | ||
14 | travis-cargo bench && | ||
15 | travis-cargo --only nightly build -- --features spin_no_std && | ||
16 | travis-cargo --only nightly test -- --features spin_no_std && | ||
17 | travis-cargo --only nightly bench -- --features spin_no_std && | ||
18 | travis-cargo --only stable doc | ||
19 | after_success: | ||
20 | - travis-cargo --only stable doc-upload | ||
21 | env: | ||
22 | global: | ||
23 | - TRAVIS_CARGO_NIGHTLY_FEATURE=nightly | ||
24 | - secure: YXu24LptjeYirjWYjWGsMT2m3mB7LvQATE6TVo7VEUXv8GYoy2ORIHD83PeImxC93MmZ01QeUezRzuCW51ZcK92VnNSBttlF60SvIX18VsJrV92tsAhievFstqYQ+fB8DIuQ8noU0jPz7GpI+R9dlTRSImAqWOnVIghA+Wzz7Js= | ||
diff --git a/third_party/lazy-static.rs/Cargo.toml b/third_party/lazy-static.rs/Cargo.toml new file mode 100644 index 0000000..2d4c91b --- /dev/null +++ b/third_party/lazy-static.rs/Cargo.toml | |||
@@ -0,0 +1,20 @@ | |||
1 | [package] | ||
2 | name = "lazy_static" | ||
3 | version = "0.2.1" | ||
4 | authors = ["Marvin Löbel <loebel.marvin@gmail.com>"] | ||
5 | license = "MIT" | ||
6 | |||
7 | description = "A macro for declaring lazily evaluated statics in Rust." | ||
8 | readme = "README.md" | ||
9 | documentation = "http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/index.html" | ||
10 | |||
11 | repository = "https://github.com/rust-lang-nursery/lazy-static.rs" | ||
12 | keywords = ["macro", "lazy", "static"] | ||
13 | |||
14 | [dependencies.spin] | ||
15 | version = "0.4" | ||
16 | optional = true | ||
17 | |||
18 | [features] | ||
19 | nightly = [] | ||
20 | spin_no_std = ["nightly", "spin"] | ||
diff --git a/third_party/lazy-static.rs/LICENSE b/third_party/lazy-static.rs/LICENSE new file mode 100644 index 0000000..d442558 --- /dev/null +++ b/third_party/lazy-static.rs/LICENSE | |||
@@ -0,0 +1,21 @@ | |||
1 | The MIT License (MIT) | ||
2 | |||
3 | Copyright (c) 2014 Marvin Löbel | ||
4 | |||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
6 | of this software and associated documentation files (the "Software"), to deal | ||
7 | in the Software without restriction, including without limitation the rights | ||
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
9 | copies of the Software, and to permit persons to whom the Software is | ||
10 | furnished to do so, subject to the following conditions: | ||
11 | |||
12 | The above copyright notice and this permission notice shall be included in all | ||
13 | copies or substantial portions of the Software. | ||
14 | |||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
21 | SOFTWARE. \ No newline at end of file | ||
diff --git a/third_party/lazy-static.rs/README.md b/third_party/lazy-static.rs/README.md new file mode 100644 index 0000000..434589c --- /dev/null +++ b/third_party/lazy-static.rs/README.md | |||
@@ -0,0 +1,81 @@ | |||
1 | lazy-static.rs | ||
2 | ============== | ||
3 | |||
4 | [![Travis-CI Status](https://travis-ci.org/rust-lang-nursery/lazy-static.rs.svg?branch=master)](https://travis-ci.org/rust-lang-nursery/lazy-static.rs) | ||
5 | |||
6 | A macro for declaring lazily evaluated statics in Rust. | ||
7 | |||
8 | Using this macro, it is possible to have `static`s that require code to be | ||
9 | executed at runtime in order to be initialized. | ||
10 | This includes anything requiring heap allocations, like vectors or hash maps, | ||
11 | as well as anything that requires function calls to be computed. | ||
12 | |||
13 | # Syntax | ||
14 | |||
15 | ```rust | ||
16 | lazy_static! { | ||
17 | [pub] static ref NAME_1: TYPE_1 = EXPR_1; | ||
18 | [pub] static ref NAME_2: TYPE_2 = EXPR_2; | ||
19 | ... | ||
20 | [pub] static ref NAME_N: TYPE_N = EXPR_N; | ||
21 | } | ||
22 | ``` | ||
23 | |||
24 | # Semantic | ||
25 | |||
26 | For a given `static ref NAME: TYPE = EXPR;`, the macro generates a | ||
27 | unique type that implements `Deref<TYPE>` and stores it in a static with name `NAME`. | ||
28 | |||
29 | On first deref, `EXPR` gets evaluated and stored internally, such that all further derefs | ||
30 | can return a reference to the same object. | ||
31 | |||
32 | Like regular `static mut`s, this macro only works for types that fulfill the `Sync` | ||
33 | trait. | ||
34 | |||
35 | # Getting Started | ||
36 | |||
37 | [lazy-static.rs is available on crates.io](https://crates.io/crates/lazy_static). | ||
38 | Add the following dependency to your Cargo manifest to get the latest version of the 0.1 branch: | ||
39 | |||
40 | ```toml | ||
41 | [dependencies] | ||
42 | lazy_static = "0.1.*" | ||
43 | ``` | ||
44 | |||
45 | To always get the latest version, add this git repository to your | ||
46 | Cargo manifest: | ||
47 | |||
48 | ```toml | ||
49 | [dependencies.lazy_static] | ||
50 | git = "https://github.com/rust-lang-nursery/lazy-static.rs" | ||
51 | ``` | ||
52 | # Example | ||
53 | |||
54 | Using the macro: | ||
55 | |||
56 | ```rust | ||
57 | #[macro_use] | ||
58 | extern crate lazy_static; | ||
59 | |||
60 | use std::collections::HashMap; | ||
61 | |||
62 | lazy_static! { | ||
63 | static ref HASHMAP: HashMap<u32, &'static str> = { | ||
64 | let mut m = HashMap::new(); | ||
65 | m.insert(0, "foo"); | ||
66 | m.insert(1, "bar"); | ||
67 | m.insert(2, "baz"); | ||
68 | m | ||
69 | }; | ||
70 | static ref COUNT: usize = HASHMAP.len(); | ||
71 | static ref NUMBER: u32 = times_two(21); | ||
72 | } | ||
73 | |||
74 | fn times_two(n: u32) -> u32 { n * 2 } | ||
75 | |||
76 | fn main() { | ||
77 | println!("The map has {} entries.", *COUNT); | ||
78 | println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap()); | ||
79 | println!("An expensive calculation on a static results in: {}.", *NUMBER); | ||
80 | } | ||
81 | ``` | ||
diff --git a/third_party/lazy-static.rs/src/core_lazy.rs b/third_party/lazy-static.rs/src/core_lazy.rs new file mode 100644 index 0000000..20290d9 --- /dev/null +++ b/third_party/lazy-static.rs/src/core_lazy.rs | |||
@@ -0,0 +1,27 @@ | |||
1 | extern crate spin; | ||
2 | |||
3 | use self::spin::Once; | ||
4 | |||
5 | pub struct Lazy<T: Sync>(Once<T>); | ||
6 | |||
7 | impl<T: Sync> Lazy<T> { | ||
8 | #[inline(always)] | ||
9 | pub const fn new() -> Self { | ||
10 | Lazy(Once::new()) | ||
11 | } | ||
12 | |||
13 | #[inline(always)] | ||
14 | pub fn get<F>(&'static self, builder: F) -> &T | ||
15 | where F: FnOnce() -> T | ||
16 | { | ||
17 | self.0.call_once(builder) | ||
18 | } | ||
19 | } | ||
20 | |||
21 | #[macro_export] | ||
22 | #[allow_internal_unstable] | ||
23 | macro_rules! __lazy_static_create { | ||
24 | ($NAME:ident, $T:ty) => { | ||
25 | static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new(); | ||
26 | } | ||
27 | } | ||
diff --git a/third_party/lazy-static.rs/src/lazy.rs b/third_party/lazy-static.rs/src/lazy.rs new file mode 100644 index 0000000..c7e6cef --- /dev/null +++ b/third_party/lazy-static.rs/src/lazy.rs | |||
@@ -0,0 +1,32 @@ | |||
1 | extern crate std; | ||
2 | |||
3 | use self::std::prelude::v1::*; | ||
4 | use self::std::sync::Once; | ||
5 | |||
6 | pub struct Lazy<T: Sync>(pub *const T, pub Once); | ||
7 | |||
8 | impl<T: Sync> Lazy<T> { | ||
9 | #[inline(always)] | ||
10 | pub fn get<F>(&'static mut self, f: F) -> &T | ||
11 | where F: FnOnce() -> T | ||
12 | { | ||
13 | unsafe { | ||
14 | let r = &mut self.0; | ||
15 | self.1.call_once(|| { | ||
16 | *r = Box::into_raw(Box::new(f())); | ||
17 | }); | ||
18 | |||
19 | &*self.0 | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | |||
24 | unsafe impl<T: Sync> Sync for Lazy<T> {} | ||
25 | |||
26 | #[macro_export] | ||
27 | macro_rules! __lazy_static_create { | ||
28 | ($NAME:ident, $T:ty) => { | ||
29 | use std::sync::ONCE_INIT; | ||
30 | static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, ONCE_INIT); | ||
31 | } | ||
32 | } | ||
diff --git a/third_party/lazy-static.rs/src/lib.rs b/third_party/lazy-static.rs/src/lib.rs new file mode 100644 index 0000000..4837066 --- /dev/null +++ b/third_party/lazy-static.rs/src/lib.rs | |||
@@ -0,0 +1,137 @@ | |||
1 | /*! | ||
2 | A macro for declaring lazily evaluated statics. | ||
3 | |||
4 | Using this macro, it is possible to have `static`s that require code to be | ||
5 | executed at runtime in order to be initialized. | ||
6 | This includes anything requiring heap allocations, like vectors or hash maps, | ||
7 | as well as anything that requires function calls to be computed. | ||
8 | |||
9 | # Syntax | ||
10 | |||
11 | ```ignore | ||
12 | lazy_static! { | ||
13 | [pub] static ref NAME_1: TYPE_1 = EXPR_1; | ||
14 | [pub] static ref NAME_2: TYPE_2 = EXPR_2; | ||
15 | ... | ||
16 | [pub] static ref NAME_N: TYPE_N = EXPR_N; | ||
17 | } | ||
18 | ``` | ||
19 | |||
20 | Metadata (such as doc comments) is allowed on each ref. | ||
21 | |||
22 | # Semantic | ||
23 | |||
24 | For a given `static ref NAME: TYPE = EXPR;`, the macro generates a unique type that | ||
25 | implements `Deref<TYPE>` and stores it in a static with name `NAME`. (Metadata ends up | ||
26 | attaching to this type.) | ||
27 | |||
28 | On first deref, `EXPR` gets evaluated and stored internally, such that all further derefs | ||
29 | can return a reference to the same object. | ||
30 | |||
31 | Like regular `static mut`s, this macro only works for types that fulfill the `Sync` | ||
32 | trait. | ||
33 | |||
34 | # Example | ||
35 | |||
36 | Using the macro: | ||
37 | |||
38 | ```rust | ||
39 | #[macro_use] | ||
40 | extern crate lazy_static; | ||
41 | |||
42 | use std::collections::HashMap; | ||
43 | |||
44 | lazy_static! { | ||
45 | static ref HASHMAP: HashMap<u32, &'static str> = { | ||
46 | let mut m = HashMap::new(); | ||
47 | m.insert(0, "foo"); | ||
48 | m.insert(1, "bar"); | ||
49 | m.insert(2, "baz"); | ||
50 | m | ||
51 | }; | ||
52 | static ref COUNT: usize = HASHMAP.len(); | ||
53 | static ref NUMBER: u32 = times_two(21); | ||
54 | } | ||
55 | |||
56 | fn times_two(n: u32) -> u32 { n * 2 } | ||
57 | |||
58 | fn main() { | ||
59 | println!("The map has {} entries.", *COUNT); | ||
60 | println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap()); | ||
61 | println!("A expensive calculation on a static results in: {}.", *NUMBER); | ||
62 | } | ||
63 | ``` | ||
64 | |||
65 | # Implementation details | ||
66 | |||
67 | The `Deref` implementation uses a hidden static variable that is guarded by a atomic check on each access. On stable Rust, the macro may need to allocate each static on the heap. | ||
68 | |||
69 | */ | ||
70 | |||
71 | #![cfg_attr(feature="nightly", feature(const_fn, allow_internal_unstable, core_intrinsics))] | ||
72 | |||
73 | #![no_std] | ||
74 | |||
75 | #[cfg(not(feature="nightly"))] | ||
76 | pub mod lazy; | ||
77 | |||
78 | #[cfg(all(feature="nightly", not(feature="spin_no_std")))] | ||
79 | #[path="nightly_lazy.rs"] | ||
80 | pub mod lazy; | ||
81 | |||
82 | #[cfg(all(feature="nightly", feature="spin_no_std"))] | ||
83 | #[path="core_lazy.rs"] | ||
84 | pub mod lazy; | ||
85 | |||
86 | pub use core::ops::Deref as __Deref; | ||
87 | |||
88 | #[macro_export] | ||
89 | #[cfg_attr(feature="nightly", allow_internal_unstable)] | ||
90 | macro_rules! lazy_static { | ||
91 | ($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => { | ||
92 | lazy_static!(@PRIV, $(#[$attr])* static ref $N : $T = $e; $($t)*); | ||
93 | }; | ||
94 | ($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => { | ||
95 | lazy_static!(@PUB, $(#[$attr])* static ref $N : $T = $e; $($t)*); | ||
96 | }; | ||
97 | (@$VIS:ident, $(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => { | ||
98 | lazy_static!(@MAKE TY, $VIS, $(#[$attr])*, $N); | ||
99 | impl $crate::__Deref for $N { | ||
100 | type Target = $T; | ||
101 | #[allow(unsafe_code)] | ||
102 | fn deref<'a>(&'a self) -> &'a $T { | ||
103 | unsafe { | ||
104 | #[inline(always)] | ||
105 | fn __static_ref_initialize() -> $T { $e } | ||
106 | |||
107 | #[inline(always)] | ||
108 | unsafe fn __stability() -> &'static $T { | ||
109 | __lazy_static_create!(LAZY, $T); | ||
110 | LAZY.get(__static_ref_initialize) | ||
111 | } | ||
112 | __stability() | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | lazy_static!($($t)*); | ||
117 | }; | ||
118 | (@MAKE TY, PUB, $(#[$attr:meta])*, $N:ident) => { | ||
119 | #[allow(missing_copy_implementations)] | ||
120 | #[allow(non_camel_case_types)] | ||
121 | #[allow(dead_code)] | ||
122 | $(#[$attr])* | ||
123 | pub struct $N {__private_field: ()} | ||
124 | #[doc(hidden)] | ||
125 | pub static $N: $N = $N {__private_field: ()}; | ||
126 | }; | ||
127 | (@MAKE TY, PRIV, $(#[$attr:meta])*, $N:ident) => { | ||
128 | #[allow(missing_copy_implementations)] | ||
129 | #[allow(non_camel_case_types)] | ||
130 | #[allow(dead_code)] | ||
131 | $(#[$attr])* | ||
132 | struct $N {__private_field: ()} | ||
133 | #[doc(hidden)] | ||
134 | static $N: $N = $N {__private_field: ()}; | ||
135 | }; | ||
136 | () => () | ||
137 | } | ||
diff --git a/third_party/lazy-static.rs/src/nightly_lazy.rs b/third_party/lazy-static.rs/src/nightly_lazy.rs new file mode 100644 index 0000000..5f7a110 --- /dev/null +++ b/third_party/lazy-static.rs/src/nightly_lazy.rs | |||
@@ -0,0 +1,40 @@ | |||
1 | extern crate std; | ||
2 | |||
3 | use self::std::prelude::v1::*; | ||
4 | use self::std::cell::UnsafeCell; | ||
5 | use self::std::sync::{Once, ONCE_INIT}; | ||
6 | |||
7 | pub struct Lazy<T: Sync>(UnsafeCell<Option<T>>, Once); | ||
8 | |||
9 | impl<T: Sync> Lazy<T> { | ||
10 | #[inline(always)] | ||
11 | pub const fn new() -> Self { | ||
12 | Lazy(UnsafeCell::new(None), ONCE_INIT) | ||
13 | } | ||
14 | |||
15 | #[inline(always)] | ||
16 | pub fn get<F>(&'static self, f: F) -> &T | ||
17 | where F: FnOnce() -> T | ||
18 | { | ||
19 | unsafe { | ||
20 | self.1.call_once(|| { | ||
21 | *self.0.get() = Some(f()); | ||
22 | }); | ||
23 | |||
24 | match *self.0.get() { | ||
25 | Some(ref x) => x, | ||
26 | None => std::intrinsics::unreachable(), | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | unsafe impl<T: Sync> Sync for Lazy<T> {} | ||
33 | |||
34 | #[macro_export] | ||
35 | #[allow_internal_unstable] | ||
36 | macro_rules! __lazy_static_create { | ||
37 | ($NAME:ident, $T:ty) => { | ||
38 | static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new(); | ||
39 | } | ||
40 | } | ||
diff --git a/third_party/lazy-static.rs/tests/no_std.rs b/third_party/lazy-static.rs/tests/no_std.rs new file mode 100644 index 0000000..b460e79 --- /dev/null +++ b/third_party/lazy-static.rs/tests/no_std.rs | |||
@@ -0,0 +1,21 @@ | |||
1 | #![cfg(feature="spin_no_std")] | ||
2 | #![feature(const_fn)] | ||
3 | |||
4 | #![no_std] | ||
5 | |||
6 | #[macro_use] | ||
7 | extern crate lazy_static; | ||
8 | |||
9 | lazy_static! { | ||
10 | /// Documentation! | ||
11 | pub static ref NUMBER: u32 = times_two(3); | ||
12 | } | ||
13 | |||
14 | fn times_two(n: u32) -> u32 { | ||
15 | n * 2 | ||
16 | } | ||
17 | |||
18 | #[test] | ||
19 | fn test_basic() { | ||
20 | assert_eq!(*NUMBER, 6); | ||
21 | } | ||
diff --git a/third_party/lazy-static.rs/tests/test.rs b/third_party/lazy-static.rs/tests/test.rs new file mode 100644 index 0000000..71d22e3 --- /dev/null +++ b/third_party/lazy-static.rs/tests/test.rs | |||
@@ -0,0 +1,116 @@ | |||
1 | #![cfg_attr(feature="nightly", feature(const_fn))] | ||
2 | |||
3 | #[macro_use] | ||
4 | extern crate lazy_static; | ||
5 | use std::collections::HashMap; | ||
6 | |||
7 | lazy_static! { | ||
8 | /// Documentation! | ||
9 | pub static ref NUMBER: u32 = times_two(3); | ||
10 | |||
11 | static ref ARRAY_BOXES: [Box<u32>; 3] = [Box::new(1), Box::new(2), Box::new(3)]; | ||
12 | |||
13 | /// More documentation! | ||
14 | #[allow(unused_variables)] | ||
15 | #[derive(Copy, Clone, Debug)] | ||
16 | pub static ref STRING: String = "hello".to_string(); | ||
17 | |||
18 | static ref HASHMAP: HashMap<u32, &'static str> = { | ||
19 | let mut m = HashMap::new(); | ||
20 | m.insert(0, "abc"); | ||
21 | m.insert(1, "def"); | ||
22 | m.insert(2, "ghi"); | ||
23 | m | ||
24 | }; | ||
25 | |||
26 | // This should not compile if the unsafe is removed. | ||
27 | static ref UNSAFE: u32 = unsafe { | ||
28 | std::mem::transmute::<i32, u32>(-1) | ||
29 | }; | ||
30 | |||
31 | // This *should* triggger warn(dead_code) by design. | ||
32 | static ref UNUSED: () = (); | ||
33 | |||
34 | } | ||
35 | |||
36 | fn times_two(n: u32) -> u32 { | ||
37 | n * 2 | ||
38 | } | ||
39 | |||
40 | #[test] | ||
41 | fn test_basic() { | ||
42 | assert_eq!(&**STRING, "hello"); | ||
43 | assert_eq!(*NUMBER, 6); | ||
44 | assert!(HASHMAP.get(&1).is_some()); | ||
45 | assert!(HASHMAP.get(&3).is_none()); | ||
46 | assert_eq!(&*ARRAY_BOXES, &[Box::new(1), Box::new(2), Box::new(3)]); | ||
47 | assert_eq!(*UNSAFE, std::u32::MAX); | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn test_repeat() { | ||
52 | assert_eq!(*NUMBER, 6); | ||
53 | assert_eq!(*NUMBER, 6); | ||
54 | assert_eq!(*NUMBER, 6); | ||
55 | } | ||
56 | |||
57 | #[test] | ||
58 | fn test_meta() { | ||
59 | // this would not compile if STRING were not marked #[derive(Copy, Clone)] | ||
60 | let copy_of_string = STRING; | ||
61 | // just to make sure it was copied | ||
62 | assert!(&STRING as *const _ != ©_of_string as *const _); | ||
63 | |||
64 | // this would not compile if STRING were not marked #[derive(Debug)] | ||
65 | assert_eq!(format!("{:?}", STRING), "STRING { __private_field: () }".to_string()); | ||
66 | } | ||
67 | |||
68 | mod visibility { | ||
69 | lazy_static! { | ||
70 | pub static ref FOO: Box<u32> = Box::new(0); | ||
71 | static ref BAR: Box<u32> = Box::new(98); | ||
72 | } | ||
73 | |||
74 | #[test] | ||
75 | fn sub_test() { | ||
76 | assert_eq!(**FOO, 0); | ||
77 | assert_eq!(**BAR, 98); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | #[test] | ||
82 | fn test_visibility() { | ||
83 | assert_eq!(*visibility::FOO, Box::new(0)); | ||
84 | } | ||
85 | |||
86 | // This should not cause a warning about a missing Copy implementation | ||
87 | lazy_static! { | ||
88 | pub static ref VAR: i32 = { 0 }; | ||
89 | } | ||
90 | |||
91 | #[derive(Copy, Clone, Debug, PartialEq)] | ||
92 | struct X; | ||
93 | struct Once(X); | ||
94 | const ONCE_INIT: Once = Once(X); | ||
95 | static DATA: X = X; | ||
96 | static ONCE: X = X; | ||
97 | fn require_sync() -> X { X } | ||
98 | fn transmute() -> X { X } | ||
99 | fn __static_ref_initialize() -> X { X } | ||
100 | fn test(_: Vec<X>) -> X { X } | ||
101 | |||
102 | // All these names should not be shadowed | ||
103 | lazy_static! { | ||
104 | static ref ITEM_NAME_TEST: X = { | ||
105 | test(vec![X, Once(X).0, ONCE_INIT.0, DATA, ONCE, | ||
106 | require_sync(), transmute(), | ||
107 | // Except this, which will sadly be shadowed by internals: | ||
108 | // __static_ref_initialize() | ||
109 | ]) | ||
110 | }; | ||
111 | } | ||
112 | |||
113 | #[test] | ||
114 | fn item_name_shadowing() { | ||
115 | assert_eq!(*ITEM_NAME_TEST, X); | ||
116 | } | ||