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 /*!
9 A macro for declaring lazily evaluated statics.
10
11 Using this macro, it is possible to have `static`s that require code to be
12 executed at runtime in order to be initialized.
13 This includes anything requiring heap allocations, like vectors or hash maps,
14 as well as anything that requires function calls to be computed.
15
16 # Syntax
17
18 ```ignore
19 lazy_static! {
20 [pub] static ref NAME_1: TYPE_1 = EXPR_1;
21 [pub] static ref NAME_2: TYPE_2 = EXPR_2;
22 ...
23 [pub] static ref NAME_N: TYPE_N = EXPR_N;
24 }
25 ```
26
27 Attributes (including doc comments) are supported as well:
28
29 ```rust
30 # #[macro_use]
31 # extern crate lazy_static;
32 # fn main() {
33 lazy_static! {
34 /// This is an example for using doc comment attributes
35 static ref EXAMPLE: u8 = 42;
36 }
37 # }
38 ```
39
40 # Semantics
41
42 For a given `static ref NAME: TYPE = EXPR;`, the macro generates a unique type that
43 implements `Deref<TYPE>` and stores it in a static with name `NAME`. (Attributes end up
44 attaching to this type.)
45
46 On first deref, `EXPR` gets evaluated and stored internally, such that all further derefs
47 can return a reference to the same object. Note that this can lead to deadlocks
48 if you have multiple lazy statics that depend on each other in their initialization.
49
50 Apart from the lazy initialization, the resulting "static ref" variables
51 have generally the same properties as regular "static" variables:
52
53 - Any type in them needs to fulfill the `Sync` trait.
54 - If the type has a destructor, then it will not run when the process exits.
55
56 # Example
57
58 Using the macro:
59
60 ```rust
61 #[macro_use]
62 extern crate lazy_static;
63
64 use std::collections::HashMap;
65
66 lazy_static! {
67 static ref HASHMAP: HashMap<u32, &'static str> = {
68 let mut m = HashMap::new();
69 m.insert(0, "foo");
70 m.insert(1, "bar");
71 m.insert(2, "baz");
72 m
73 };
74 static ref COUNT: usize = HASHMAP.len();
75 static ref NUMBER: u32 = times_two(21);
76 }
77
78 fn times_two(n: u32) -> u32 { n * 2 }
79
80 fn main() {
81 println!("The map has {} entries.", *COUNT);
82 println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
83 println!("A expensive calculation on a static results in: {}.", *NUMBER);
84 }
85 ```
86
87 # Implementation details
88
89 The `Deref` implementation uses a hidden static variable that is guarded by an atomic check on each access.
90
91 # Cargo features
92
93 This crate provides one cargo feature:
94
95 - `spin_no_std`: This allows using this crate in a no-std environment, by depending on the standalone `spin` crate.
96
97 */
98
99 #![doc(html_root_url = "https://docs.rs/lazy_static/1.4.0")]
100 #![no_std]
101
102 #[cfg(not(feature = "spin_no_std"))]
103 #[path="inline_lazy.rs"]
104 #[doc(hidden)]
105 pub mod lazy;
106
107 // ANDROID: disable tests that require doc_comment crate.
108 //#[cfg(test)]
109 //#[macro_use]
110 //extern crate doc_comment;
111 //
112 //#[cfg(test)]
113 //`doctest!("../README.md");
114
115 #[cfg(feature = "spin_no_std")]
116 #[path="core_lazy.rs"]
117 #[doc(hidden)]
118 pub mod lazy;
119
120 #[doc(hidden)]
121 pub use core::ops::Deref as __Deref;
122
123 #[macro_export(local_inner_macros)]
124 #[doc(hidden)]
125 macro_rules! __lazy_static_internal {
126 // optional visibility restrictions are wrapped in `()` to allow for
127 // explicitly passing otherwise implicit information about private items
128 ($(#[$attr:meta])* ($($vis:tt)*) static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
129 __lazy_static_internal!(@MAKE TY, $(#[$attr])*, ($($vis)*), $N);
130 __lazy_static_internal!(@TAIL, $N : $T = $e);
131 lazy_static!($($t)*);
132 };
133 (@TAIL, $N:ident : $T:ty = $e:expr) => {
134 impl $crate::__Deref for $N {
135 type Target = $T;
136 fn deref(&self) -> &$T {
137 #[inline(always)]
138 fn __static_ref_initialize() -> $T { $e }
139
140 #[inline(always)]
141 fn __stability() -> &'static $T {
142 __lazy_static_create!(LAZY, $T);
143 LAZY.get(__static_ref_initialize)
144 }
145 __stability()
146 }
147 }
148 impl $crate::LazyStatic for $N {
149 fn initialize(lazy: &Self) {
150 let _ = &**lazy;
151 }
152 }
153 };
154 // `vis` is wrapped in `()` to prevent parsing ambiguity
155 (@MAKE TY, $(#[$attr:meta])*, ($($vis:tt)*), $N:ident) => {
156 #[allow(missing_copy_implementations)]
157 #[allow(non_camel_case_types)]
158 #[allow(dead_code)]
159 $(#[$attr])*
160 $($vis)* struct $N {__private_field: ()}
161 #[doc(hidden)]
162 $($vis)* static $N: $N = $N {__private_field: ()};
163 };
164 () => ()
165 }
166
167 #[macro_export(local_inner_macros)]
168 macro_rules! lazy_static {
169 ($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
170 // use `()` to explicitly forward the information about private items
171 __lazy_static_internal!($(#[$attr])* () static ref $N : $T = $e; $($t)*);
172 };
173 ($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
174 __lazy_static_internal!($(#[$attr])* (pub) static ref $N : $T = $e; $($t)*);
175 };
176 ($(#[$attr:meta])* pub ($($vis:tt)+) static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
177 __lazy_static_internal!($(#[$attr])* (pub ($($vis)+)) static ref $N : $T = $e; $($t)*);
178 };
179 () => ()
180 }
181
182 /// Support trait for enabling a few common operation on lazy static values.
183 ///
184 /// This is implemented by each defined lazy static, and
185 /// used by the free functions in this crate.
186 pub trait LazyStatic {
187 #[doc(hidden)]
initialize(lazy: &Self)188 fn initialize(lazy: &Self);
189 }
190
191 /// Takes a shared reference to a lazy static and initializes
192 /// it if it has not been already.
193 ///
194 /// This can be used to control the initialization point of a lazy static.
195 ///
196 /// Example:
197 ///
198 /// ```rust
199 /// #[macro_use]
200 /// extern crate lazy_static;
201 ///
202 /// lazy_static! {
203 /// static ref BUFFER: Vec<u8> = (0..255).collect();
204 /// }
205 ///
206 /// fn main() {
207 /// lazy_static::initialize(&BUFFER);
208 ///
209 /// // ...
210 /// work_with_initialized_data(&BUFFER);
211 /// }
212 /// # fn work_with_initialized_data(_: &[u8]) {}
213 /// ```
initialize<T: LazyStatic>(lazy: &T)214 pub fn initialize<T: LazyStatic>(lazy: &T) {
215 LazyStatic::initialize(lazy);
216 }
217