| 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 core; |
| 9 | extern crate std; |
| 10 | |
| 11 | use self::std::prelude::v1::*; |
| 12 | use self::std::cell::Cell; |
| 13 | use self::std::hint::unreachable_unchecked; |
| 14 | use self::std::sync::Once; |
| 15 | #[allow (deprecated)] |
| 16 | pub use self::std::sync::ONCE_INIT; |
| 17 | |
| 18 | // FIXME: Replace Option<T> with MaybeUninit<T> (stable since 1.36.0) |
| 19 | pub struct Lazy<T: Sync>(Cell<Option<T>>, Once); |
| 20 | |
| 21 | impl<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 | // `self.0` is guaranteed to be `Some` by this point |
| 35 | // The `Once` will catch and propagate panics |
| 36 | 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 | |
| 49 | unsafe impl<T: Sync> Sync for Lazy<T> {} |
| 50 | |
| 51 | #[macro_export ] |
| 52 | #[doc (hidden)] |
| 53 | macro_rules! __lazy_static_create { |
| 54 | ($NAME:ident, $T:ty) => { |
| 55 | static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::INIT; |
| 56 | }; |
| 57 | } |
| 58 | |