This Gist now has its own repository on GitHub: https://github.com/Kimundi/lazy-static.rs
Last active
June 19, 2022 16:05
-
-
Save Kimundi/8782487 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(macro_rules)] | |
use std::collections::HashMap; | |
macro_rules! lazy_init { | |
($(static ref $N:ident : $T:ty = $e:expr;)*) => { | |
$( | |
#[allow(non_camel_case_types)] | |
struct $N {__unit__: ()} | |
static $N: $N = $N {__unit__: ()}; | |
impl Deref<$T> for $N { | |
#[allow(dead_code)] | |
fn deref<'a>(&'a self) -> &'a $T { | |
use std::sync::{Once, ONCE_INIT}; | |
use std::mem::transmute; | |
#[inline(always)] | |
fn require_share<T: Share>(_: &T) { } | |
unsafe { | |
static mut s: *$T = 0 as *$T; | |
static mut ONCE: Once = ONCE_INIT; | |
ONCE.doit(|| { | |
s = transmute::<Box<$T>, *$T>(box() ($e)); | |
}); | |
let static_ref = &*s; | |
require_share(static_ref); | |
static_ref | |
} | |
} | |
} | |
)* | |
} | |
} | |
lazy_init! { | |
static ref NUMBER: uint = times_two(3); | |
static ref VEC: [Box<uint>, ..3] = [box 1, box 2, box 3]; | |
static ref OWNED_STRING: String = "hello".to_string(); | |
static ref HASHMAP: HashMap<uint, &'static str> = { | |
let mut m = HashMap::new(); | |
m.insert(0u, "abc"); | |
m.insert(1, "def"); | |
m.insert(2, "ghi"); | |
m | |
}; | |
} | |
fn times_two(n: uint) -> uint { | |
n * 2 | |
} | |
#[test] | |
fn test_basic() { | |
assert_eq!(OWNED_STRING.as_slice(), "hello"); | |
assert_eq!(*NUMBER, 6); | |
assert!(HASHMAP.find(&1).is_some()); | |
assert!(HASHMAP.find(&3).is_none()); | |
assert_eq!(VEC.as_slice(), &[box 1, box 2, box 3]); | |
} | |
#[test] | |
fn test_repeat() { | |
assert_eq!(*NUMBER, 6); | |
assert_eq!(*NUMBER, 6); | |
assert_eq!(*NUMBER, 6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This now officially lives in https://github.com/Kimundi/lazy-static.rs