5
5
// http://opensource.org/licenses/MIT>, at your option. This file may not be
6
6
// copied, modified, or distributed except according to those terms.
7
7
8
- extern crate std;
9
8
extern crate core;
9
+ extern crate std;
10
10
11
11
use self :: std:: prelude:: v1:: * ;
12
12
use self :: std:: sync:: Once ;
13
13
pub use self :: std:: sync:: ONCE_INIT ;
14
14
15
- pub struct Lazy < T : Sync > ( pub Option < T > , pub Once ) ;
15
+ pub struct Lazy < T : Sync > ( Option < T > , Once ) ;
16
16
17
17
impl < T : Sync > Lazy < T > {
18
+ pub const INIT : Self = Lazy ( None , ONCE_INIT ) ;
19
+
18
20
#[ inline( always) ]
19
21
pub fn get < F > ( & ' static mut self , f : F ) -> & T
20
- where F : FnOnce ( ) -> T
22
+ where
23
+ F : FnOnce ( ) -> T ,
21
24
{
22
25
{
23
26
let r = & mut self . 0 ;
@@ -28,7 +31,7 @@ impl<T: Sync> Lazy<T> {
28
31
unsafe {
29
32
match self . 0 {
30
33
Some ( ref x) => x,
31
- None => core :: hint :: unreachable_unchecked ( ) ,
34
+ None => unreachable_unchecked ( ) ,
32
35
}
33
36
}
34
37
}
@@ -40,6 +43,17 @@ unsafe impl<T: Sync> Sync for Lazy<T> {}
40
43
#[ doc( hidden) ]
41
44
macro_rules! __lazy_static_create {
42
45
( $NAME: ident, $T: ty) => {
43
- static mut $NAME: $crate:: lazy:: Lazy <$T> = $crate:: lazy:: Lazy ( None , $crate:: lazy:: ONCE_INIT ) ;
44
- }
46
+ static mut $NAME: $crate:: lazy:: Lazy <$T> = $crate:: lazy:: Lazy :: INIT ;
47
+ } ;
48
+ }
49
+
50
+ /// Polyfill for std::hint::unreachable_unchecked. There currently exists a
51
+ /// [crate](https://docs.rs/unreachable) for an equivalent to std::hint::unreachable_unchecked, but
52
+ /// lazy_static currently doesn't include any runtime dependencies and we've chosen to include this
53
+ /// short polyfill rather than include a new crate in every consumer's build.
54
+ ///
55
+ /// This should be replaced by std's version when lazy_static starts to require at least Rust 1.27.
56
+ unsafe fn unreachable_unchecked ( ) -> ! {
57
+ enum Void { }
58
+ match std:: mem:: uninitialized :: < Void > ( ) { }
45
59
}
0 commit comments