| 1 |
#![deny(warnings)] |
| 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 |
|
| 32 |
lazy_static! { |
| 33 |
static ref S1: &'static str = "a"; |
| 34 |
static ref S2: &'static str = "b"; |
| 35 |
} |
| 36 |
lazy_static! { |
| 37 |
static ref S3: String = [*S1, *S2].join(""); |
| 38 |
} |
| 39 |
|
| 40 |
lazy_static! { |
| 41 |
#[allow(non_upper_case_globals)] |
| 42 |
pub static ref string: String = "hello".to_string(); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn s3() { |
| 47 |
assert_eq!(&*S3, "ab"); |
| 48 |
} |
| 49 |
|
| 50 |
fn times_two(n: u32) -> u32 { |
| 51 |
n * 2 |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn test_basic() { |
| 56 |
assert_eq!(&**STRING, "hello"); |
| 57 |
assert_eq!(*NUMBER, 6); |
| 58 |
assert!(HASHMAP.get(&1).is_some()); |
| 59 |
assert!(HASHMAP.get(&3).is_none()); |
| 60 |
assert_eq!(&*ARRAY_BOXES, &[Box::new(1), Box::new(2), Box::new(3)]); |
| 61 |
assert_eq!(*UNSAFE, std::u32::MAX); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn test_repeat() { |
| 66 |
assert_eq!(*NUMBER, 6); |
| 67 |
assert_eq!(*NUMBER, 6); |
| 68 |
assert_eq!(*NUMBER, 6); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_meta() { |
| 73 |
// this would not compile if STRING were not marked #[derive(Copy, Clone)] |
| 74 |
let copy_of_string = STRING; |
| 75 |
// just to make sure it was copied |
| 76 |
assert!(&STRING as *const _ != ©_of_string as *const _); |
| 77 |
|
| 78 |
// this would not compile if STRING were not marked #[derive(Debug)] |
| 79 |
assert_eq!( |
| 80 |
format!("{:?}", STRING), |
| 81 |
"STRING { __private_field: () }".to_string() |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
mod visibility { |
| 86 |
lazy_static! { |
| 87 |
pub static ref FOO: Box<u32> = Box::new(0); |
| 88 |
static ref BAR: Box<u32> = Box::new(98); |
| 89 |
} |
| 90 |
|
| 91 |
pub mod inner { |
| 92 |
lazy_static! { |
| 93 |
pub(in visibility) static ref BAZ: Box<u32> = Box::new(42); |
| 94 |
pub(crate) static ref BAG: Box<u32> = Box::new(37); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn sub_test() { |
| 100 |
assert_eq!(**FOO, 0); |
| 101 |
assert_eq!(**BAR, 98); |
| 102 |
assert_eq!(**inner::BAZ, 42); |
| 103 |
assert_eq!(**inner::BAG, 37); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_visibility() { |
| 109 |
assert_eq!(*visibility::FOO, Box::new(0)); |
| 110 |
assert_eq!(*visibility::inner::BAG, Box::new(37)); |
| 111 |
} |
| 112 |
|
| 113 |
// This should not cause a warning about a missing Copy implementation |
| 114 |
lazy_static! { |
| 115 |
pub static ref VAR: i32 = 0; |
| 116 |
} |
| 117 |
|
| 118 |
#[derive(Copy, Clone, Debug, PartialEq)] |
| 119 |
struct X; |
| 120 |
struct Once(X); |
| 121 |
const ONCE_INIT: Once = Once(X); |
| 122 |
static DATA: X = X; |
| 123 |
static ONCE: X = X; |
| 124 |
fn require_sync() -> X { |
| 125 |
X |
| 126 |
} |
| 127 |
fn transmute() -> X { |
| 128 |
X |
| 129 |
} |
| 130 |
fn __static_ref_initialize() -> X { |
| 131 |
X |
| 132 |
} |
| 133 |
fn test(_: Vec<X>) -> X { |
| 134 |
X |
| 135 |
} |
| 136 |
|
| 137 |
// All these names should not be shadowed |
| 138 |
lazy_static! { |
| 139 |
static ref ITEM_NAME_TEST: X = { |
| 140 |
test(vec![X, Once(X).0, ONCE_INIT.0, DATA, ONCE, |
| 141 |
require_sync(), transmute(), |
| 142 |
// Except this, which will sadly be shadowed by internals: |
| 143 |
// __static_ref_initialize() |
| 144 |
]) |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn item_name_shadowing() { |
| 150 |
assert_eq!(*ITEM_NAME_TEST, X); |
| 151 |
} |
| 152 |
|
| 153 |
use std::sync::atomic::AtomicBool; |
| 154 |
use std::sync::atomic::Ordering::SeqCst; |
| 155 |
#[allow(deprecated)] |
| 156 |
use std::sync::atomic::ATOMIC_BOOL_INIT; |
| 157 |
|
| 158 |
#[allow(deprecated)] |
| 159 |
static PRE_INIT_FLAG: AtomicBool = ATOMIC_BOOL_INIT; |
| 160 |
|
| 161 |
lazy_static! { |
| 162 |
static ref PRE_INIT: () = { |
| 163 |
PRE_INIT_FLAG.store(true, SeqCst); |
| 164 |
() |
| 165 |
}; |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn pre_init() { |
| 170 |
assert_eq!(PRE_INIT_FLAG.load(SeqCst), false); |
| 171 |
lazy_static::initialize(&PRE_INIT); |
| 172 |
assert_eq!(PRE_INIT_FLAG.load(SeqCst), true); |
| 173 |
} |
| 174 |
|
| 175 |
lazy_static! { |
| 176 |
static ref LIFETIME_NAME: for<'a> fn(&'a u8) = { |
| 177 |
fn f(_: &u8) {} |
| 178 |
f |
| 179 |
}; |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn lifetime_name() { |
| 184 |
let _ = LIFETIME_NAME; |
| 185 |
} |