summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlurchi <lurchi@strangeplace.net>2016-08-14 12:43:05 +0200
committerlurchi <lurchi@strangeplace.net>2016-08-14 12:43:05 +0200
commit0372b4a9b8c788ace44a52430d29f4e3ece1c119 (patch)
treeedbfff1e6ccb65875d2878652d1c5cb8abb10921
parent702d163cf379ea1a3f225b3ac349f73d0abcb903 (diff)
downloadsecushare-0372b4a9b8c788ace44a52430d29f4e3ece1c119.tar.gz
secushare-0372b4a9b8c788ace44a52430d29f4e3ece1c119.zip
add lazy-static subtree
-rw-r--r--third_party/lazy-static.rs/.gitignore4
-rw-r--r--third_party/lazy-static.rs/.travis.yml24
-rw-r--r--third_party/lazy-static.rs/Cargo.toml20
-rw-r--r--third_party/lazy-static.rs/LICENSE21
-rw-r--r--third_party/lazy-static.rs/README.md81
-rw-r--r--third_party/lazy-static.rs/src/core_lazy.rs27
-rw-r--r--third_party/lazy-static.rs/src/lazy.rs32
-rw-r--r--third_party/lazy-static.rs/src/lib.rs137
-rw-r--r--third_party/lazy-static.rs/src/nightly_lazy.rs40
-rw-r--r--third_party/lazy-static.rs/tests/no_std.rs21
-rw-r--r--third_party/lazy-static.rs/tests/test.rs116
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 @@
1target
2doc
3Cargo.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 @@
1language: rust
2rust:
3- nightly
4- beta
5- stable
6before_script:
7- |
8 pip install 'travis-cargo<0.2' --user &&
9 export PATH=$HOME/.local/bin:$PATH
10script:
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
19after_success:
20- travis-cargo --only stable doc-upload
21env:
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]
2name = "lazy_static"
3version = "0.2.1"
4authors = ["Marvin Löbel <loebel.marvin@gmail.com>"]
5license = "MIT"
6
7description = "A macro for declaring lazily evaluated statics in Rust."
8readme = "README.md"
9documentation = "http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/index.html"
10
11repository = "https://github.com/rust-lang-nursery/lazy-static.rs"
12keywords = ["macro", "lazy", "static"]
13
14[dependencies.spin]
15version = "0.4"
16optional = true
17
18[features]
19nightly = []
20spin_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 @@
1The MIT License (MIT)
2
3Copyright (c) 2014 Marvin Löbel
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all
13copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE. \ 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 @@
1lazy-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
6A macro for declaring lazily evaluated statics in Rust.
7
8Using this macro, it is possible to have `static`s that require code to be
9executed at runtime in order to be initialized.
10This includes anything requiring heap allocations, like vectors or hash maps,
11as well as anything that requires function calls to be computed.
12
13# Syntax
14
15```rust
16lazy_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
26For a given `static ref NAME: TYPE = EXPR;`, the macro generates a
27unique type that implements `Deref<TYPE>` and stores it in a static with name `NAME`.
28
29On first deref, `EXPR` gets evaluated and stored internally, such that all further derefs
30can return a reference to the same object.
31
32Like regular `static mut`s, this macro only works for types that fulfill the `Sync`
33trait.
34
35# Getting Started
36
37[lazy-static.rs is available on crates.io](https://crates.io/crates/lazy_static).
38Add the following dependency to your Cargo manifest to get the latest version of the 0.1 branch:
39
40```toml
41[dependencies]
42lazy_static = "0.1.*"
43```
44
45To always get the latest version, add this git repository to your
46Cargo manifest:
47
48```toml
49[dependencies.lazy_static]
50git = "https://github.com/rust-lang-nursery/lazy-static.rs"
51```
52# Example
53
54Using the macro:
55
56```rust
57#[macro_use]
58extern crate lazy_static;
59
60use std::collections::HashMap;
61
62lazy_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
74fn times_two(n: u32) -> u32 { n * 2 }
75
76fn 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 @@
1extern crate spin;
2
3use self::spin::Once;
4
5pub struct Lazy<T: Sync>(Once<T>);
6
7impl<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]
23macro_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 @@
1extern crate std;
2
3use self::std::prelude::v1::*;
4use self::std::sync::Once;
5
6pub struct Lazy<T: Sync>(pub *const T, pub Once);
7
8impl<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
24unsafe impl<T: Sync> Sync for Lazy<T> {}
25
26#[macro_export]
27macro_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/*!
2A macro for declaring lazily evaluated statics.
3
4Using this macro, it is possible to have `static`s that require code to be
5executed at runtime in order to be initialized.
6This includes anything requiring heap allocations, like vectors or hash maps,
7as well as anything that requires function calls to be computed.
8
9# Syntax
10
11```ignore
12lazy_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
20Metadata (such as doc comments) is allowed on each ref.
21
22# Semantic
23
24For a given `static ref NAME: TYPE = EXPR;`, the macro generates a unique type that
25implements `Deref<TYPE>` and stores it in a static with name `NAME`. (Metadata ends up
26attaching to this type.)
27
28On first deref, `EXPR` gets evaluated and stored internally, such that all further derefs
29can return a reference to the same object.
30
31Like regular `static mut`s, this macro only works for types that fulfill the `Sync`
32trait.
33
34# Example
35
36Using the macro:
37
38```rust
39#[macro_use]
40extern crate lazy_static;
41
42use std::collections::HashMap;
43
44lazy_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
56fn times_two(n: u32) -> u32 { n * 2 }
57
58fn 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
67The `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"))]
76pub mod lazy;
77
78#[cfg(all(feature="nightly", not(feature="spin_no_std")))]
79#[path="nightly_lazy.rs"]
80pub mod lazy;
81
82#[cfg(all(feature="nightly", feature="spin_no_std"))]
83#[path="core_lazy.rs"]
84pub mod lazy;
85
86pub use core::ops::Deref as __Deref;
87
88#[macro_export]
89#[cfg_attr(feature="nightly", allow_internal_unstable)]
90macro_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 @@
1extern crate std;
2
3use self::std::prelude::v1::*;
4use self::std::cell::UnsafeCell;
5use self::std::sync::{Once, ONCE_INIT};
6
7pub struct Lazy<T: Sync>(UnsafeCell<Option<T>>, Once);
8
9impl<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
32unsafe impl<T: Sync> Sync for Lazy<T> {}
33
34#[macro_export]
35#[allow_internal_unstable]
36macro_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]
7extern crate lazy_static;
8
9lazy_static! {
10 /// Documentation!
11 pub static ref NUMBER: u32 = times_two(3);
12}
13
14fn times_two(n: u32) -> u32 {
15 n * 2
16}
17
18#[test]
19fn 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]
4extern crate lazy_static;
5use std::collections::HashMap;
6
7lazy_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
36fn times_two(n: u32) -> u32 {
37 n * 2
38}
39
40#[test]
41fn 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]
51fn test_repeat() {
52 assert_eq!(*NUMBER, 6);
53 assert_eq!(*NUMBER, 6);
54 assert_eq!(*NUMBER, 6);
55}
56
57#[test]
58fn 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 _ != &copy_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
68mod 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]
82fn test_visibility() {
83 assert_eq!(*visibility::FOO, Box::new(0));
84}
85
86// This should not cause a warning about a missing Copy implementation
87lazy_static! {
88 pub static ref VAR: i32 = { 0 };
89}
90
91#[derive(Copy, Clone, Debug, PartialEq)]
92struct X;
93struct Once(X);
94const ONCE_INIT: Once = Once(X);
95static DATA: X = X;
96static ONCE: X = X;
97fn require_sync() -> X { X }
98fn transmute() -> X { X }
99fn __static_ref_initialize() -> X { X }
100fn test(_: Vec<X>) -> X { X }
101
102// All these names should not be shadowed
103lazy_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]
114fn item_name_shadowing() {
115 assert_eq!(*ITEM_NAME_TEST, X);
116}