55// http://opensource.org/licenses/MIT>, at your option. This file may not be
66// copied, modified, or distributed except according to those terms.
77
8- extern crate std;
98extern crate core;
9+ extern crate std;
1010
1111use self :: std:: prelude:: v1:: * ;
1212use self :: std:: sync:: Once ;
1313pub use self :: std:: sync:: ONCE_INIT ;
1414
15- pub struct Lazy < T : Sync > ( pub Option < T > , pub Once ) ;
15+ pub struct Lazy < T : Sync > ( Option < T > , Once ) ;
1616
1717impl < T : Sync > Lazy < T > {
18+ pub const INIT : Self = Lazy ( None , ONCE_INIT ) ;
19+
1820 #[ inline( always) ]
1921 pub fn get < F > ( & ' static mut self , f : F ) -> & T
20- where F : FnOnce ( ) -> T
22+ where
23+ F : FnOnce ( ) -> T ,
2124 {
2225 {
2326 let r = & mut self . 0 ;
@@ -28,7 +31,7 @@ impl<T: Sync> Lazy<T> {
2831 unsafe {
2932 match self . 0 {
3033 Some ( ref x) => x,
31- None => core :: hint :: unreachable_unchecked ( ) ,
34+ None => unreachable_unchecked ( ) ,
3235 }
3336 }
3437 }
@@ -40,6 +43,17 @@ unsafe impl<T: Sync> Sync for Lazy<T> {}
4043#[ doc( hidden) ]
4144macro_rules! __lazy_static_create {
4245 ( $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 > ( ) { }
4559}
0 commit comments