]> git.proxmox.com Git - rustc.git/blame - src/vendor/lazy_static/src/lazy.rs
New upstream version 1.24.1+dfsg1
[rustc.git] / src / vendor / lazy_static / src / lazy.rs
CommitLineData
7cac9316
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
8bb4bdeb
XL
8extern crate std;
9
10use self::std::prelude::v1::*;
11use self::std::sync::Once;
abe05a73 12pub use self::std::sync::ONCE_INIT;
8bb4bdeb
XL
13
14pub struct Lazy<T: Sync>(pub *const T, pub Once);
15
16impl<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
32unsafe impl<T: Sync> Sync for Lazy<T> {}
33
34#[macro_export]
ff7c6d11 35#[doc(hidden)]
8bb4bdeb
XL
36macro_rules! __lazy_static_create {
37 ($NAME:ident, $T:ty) => {
abe05a73 38 static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, $crate::lazy::ONCE_INIT);
8bb4bdeb
XL
39 }
40}