lazy_static/
inline_lazy.rs
1extern crate core;
9extern crate std;
10
11use self::std::prelude::v1::*;
12use self::std::cell::Cell;
13use self::std::hint::unreachable_unchecked;
14use self::std::sync::Once;
15#[allow(deprecated)]
16pub use self::std::sync::ONCE_INIT;
17
18pub struct Lazy<T: Sync>(Cell<Option<T>>, Once);
20
21impl<T: Sync> Lazy<T> {
22 #[allow(deprecated)]
23 pub const INIT: Self = Lazy(Cell::new(None), ONCE_INIT);
24
25 #[inline(always)]
26 pub fn get<F>(&'static self, f: F) -> &T
27 where
28 F: FnOnce() -> T,
29 {
30 self.1.call_once(|| {
31 self.0.set(Some(f()));
32 });
33
34 unsafe {
37 match *self.0.as_ptr() {
38 Some(ref x) => x,
39 None => {
40 debug_assert!(false, "attempted to derefence an uninitialized lazy static. This is a bug");
41
42 unreachable_unchecked()
43 },
44 }
45 }
46 }
47}
48
49unsafe impl<T: Sync> Sync for Lazy<T> {}
50
51#[macro_export]
52#[doc(hidden)]
53macro_rules! __lazy_static_create {
54 ($NAME:ident, $T:ty) => {
55 static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::INIT;
56 };
57}