35
35
36
36
Using the macro:
37
37
38
- ```rust
38
+ ```ignore
39
39
#[macro_use]
40
40
extern crate lazy_static;
41
41
@@ -71,6 +71,7 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at
71
71
#![ cfg_attr( feature="nightly" , feature( const_fn, core_intrinsics) ) ]
72
72
#![ crate_type = "dylib" ]
73
73
74
+ #[ cfg( feature="nightly" ) ]
74
75
#[ macro_export]
75
76
macro_rules! lazy_static {
76
77
( $( #[ $attr: meta] ) * static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
@@ -94,7 +95,6 @@ macro_rules! lazy_static {
94
95
fn require_sync<T : Sync >( _: & T ) { }
95
96
96
97
#[ inline( always) ]
97
- #[ cfg( feature="nightly" ) ]
98
98
unsafe fn __stability( ) -> & ' static $T {
99
99
use std:: cell:: UnsafeCell ;
100
100
@@ -112,8 +112,59 @@ macro_rules! lazy_static {
112
112
}
113
113
}
114
114
115
+ let static_ref = __stability( ) ;
116
+ require_sync( static_ref) ;
117
+ static_ref
118
+ }
119
+ }
120
+ }
121
+ lazy_static!( $( $t) * ) ;
122
+ } ;
123
+ ( MAKE TY , PUB , $( #[ $attr: meta] ) * , $N: ident) => {
124
+ #[ allow( missing_copy_implementations) ]
125
+ #[ allow( non_camel_case_types) ]
126
+ #[ allow( dead_code) ]
127
+ $( #[ $attr] ) *
128
+ pub struct $N { __private_field: ( ) }
129
+ #[ doc( hidden) ]
130
+ pub static $N: $N = $N { __private_field: ( ) } ;
131
+ } ;
132
+ ( MAKE TY , PRIV , $( #[ $attr: meta] ) * , $N: ident) => {
133
+ #[ allow( missing_copy_implementations) ]
134
+ #[ allow( non_camel_case_types) ]
135
+ #[ allow( dead_code) ]
136
+ $( #[ $attr] ) *
137
+ struct $N { __private_field: ( ) }
138
+ #[ doc( hidden) ]
139
+ static $N: $N = $N { __private_field: ( ) } ;
140
+ } ;
141
+ ( ) => ( )
142
+ }
143
+
144
+ #[ cfg( not( feature="nightly" ) ) ]
145
+ #[ macro_export]
146
+ macro_rules! lazy_static {
147
+ ( $( #[ $attr: meta] ) * static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
148
+ lazy_static!( PRIV , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
149
+ } ;
150
+ ( $( #[ $attr: meta] ) * pub static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
151
+ lazy_static!( PUB , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
152
+ } ;
153
+ ( $VIS: ident, $( #[ $attr: meta] ) * static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
154
+ lazy_static!( MAKE TY , $VIS, $( #[ $attr] ) * , $N) ;
155
+ impl :: std:: ops:: Deref for $N {
156
+ type Target = $T;
157
+ fn deref<' a>( & ' a self ) -> & ' a $T {
158
+ #[ inline( always) ]
159
+ fn __static_ref_initialize( ) -> $T { $e }
160
+
161
+ unsafe {
162
+ use std:: sync:: { Once , ONCE_INIT } ;
163
+
164
+ #[ inline( always) ]
165
+ fn require_sync<T : Sync >( _: & T ) { }
166
+
115
167
#[ inline( always) ]
116
- #[ cfg( not( feature="nightly" ) ) ]
117
168
unsafe fn __stability( ) -> & ' static $T {
118
169
use std:: mem:: transmute;
119
170
0 commit comments