]>
Commit | Line | Data |
---|---|---|
ff7c6d11 XL |
1 | // Copyright 2016 lazy-static.rs Developers |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or | |
4 | // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | |
5 | // http://opensource.org/licenses/MIT>, at your option. This file may not be | |
6 | // copied, modified, or distributed except according to those terms. | |
7 | ||
8 | extern crate std; | |
9 | ||
10 | use self::std::prelude::v1::*; | |
11 | use self::std::sync::Once; | |
12 | pub use self::std::sync::ONCE_INIT; | |
13 | ||
14 | pub struct Lazy<T: Sync>(pub *const T, pub Once); | |
15 | ||
16 | impl<T: Sync> Lazy<T> { | |
17 | #[inline(always)] | |
18 | pub fn get<F>(&'static mut self, f: F) -> &T | |
19 | where F: FnOnce() -> T | |
20 | { | |
21 | unsafe { | |
22 | let r = &mut self.0; | |
23 | self.1.call_once(|| { | |
24 | *r = Box::into_raw(Box::new(f())); | |
25 | }); | |
26 | ||
27 | &*self.0 | |
28 | } | |
29 | } | |
30 | } | |
31 | ||
32 | unsafe impl<T: Sync> Sync for Lazy<T> {} | |
33 | ||
34 | #[macro_export] | |
35 | macro_rules! __lazy_static_create { | |
36 | ($NAME:ident, $T:ty) => { | |
37 | static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, $crate::lazy::ONCE_INIT); | |
38 | } | |
39 | } |