@@ -17,12 +17,23 @@ lazy_static! {
1717}
1818```
1919
20- Metadata (such as doc comments) is allowed on each ref.
20+ Attributes (including doc comments) are supported as well:
21+
22+ ```rust
23+ # #[macro_use]
24+ # extern crate lazy_static;
25+ # fn main() {
26+ lazy_static! {
27+ /// This is an example for using doc comment attributes
28+ static ref EXAMPLE: u8 = 42;
29+ }
30+ # }
31+ ```
2132
2233# Semantic
2334
2435For a given `static ref NAME: TYPE = EXPR;`, the macro generates a unique type that
25- implements `Deref<TYPE>` and stores it in a static with name `NAME`. (Metadata ends up
36+ implements `Deref<TYPE>` and stores it in a static with name `NAME`. (Attributes end up
2637attaching to this type.)
2738
2839On first deref, `EXPR` gets evaluated and stored internally, such that all further derefs
@@ -74,29 +85,34 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at
7485#![ no_std]
7586
7687#[ cfg( not( feature="nightly" ) ) ]
88+ #[ doc( hidden) ]
7789pub mod lazy;
7890
7991#[ cfg( all( feature="nightly" , not( feature="spin_no_std" ) ) ) ]
8092#[ path="nightly_lazy.rs" ]
93+ #[ doc( hidden) ]
8194pub mod lazy;
8295
8396#[ cfg( all( feature="nightly" , feature="spin_no_std" ) ) ]
8497#[ path="core_lazy.rs" ]
98+ #[ doc( hidden) ]
8599pub mod lazy;
86100
101+ #[ doc( hidden) ]
87102pub use core:: ops:: Deref as __Deref;
88103
89104#[ macro_export]
90105#[ cfg_attr( feature="nightly" , allow_internal_unstable) ]
91- macro_rules! lazy_static {
106+ #[ doc( hidden) ]
107+ macro_rules! __lazy_static_internal {
92108 ( $( #[ $attr: meta] ) * static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
93- lazy_static !( @PRIV , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
109+ __lazy_static_internal !( @PRIV , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
94110 } ;
95111 ( $( #[ $attr: meta] ) * pub static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
96- lazy_static !( @PUB , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
112+ __lazy_static_internal !( @PUB , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
97113 } ;
98114 ( @$VIS: ident, $( #[ $attr: meta] ) * static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
99- lazy_static !( @MAKE TY , $VIS, $( #[ $attr] ) * , $N) ;
115+ __lazy_static_internal !( @MAKE TY , $VIS, $( #[ $attr] ) * , $N) ;
100116 impl $crate:: __Deref for $N {
101117 type Target = $T;
102118 #[ allow( unsafe_code) ]
@@ -114,7 +130,7 @@ macro_rules! lazy_static {
114130 }
115131 }
116132 }
117- lazy_static !( $( $t) * ) ;
133+ __lazy_static_internal !( $( $t) * ) ;
118134 } ;
119135 ( @MAKE TY , PUB , $( #[ $attr: meta] ) * , $N: ident) => {
120136 #[ allow( missing_copy_implementations) ]
@@ -136,3 +152,26 @@ macro_rules! lazy_static {
136152 } ;
137153 ( ) => ( )
138154}
155+
156+ #[ macro_export]
157+ #[ cfg_attr( feature="nightly" , allow_internal_unstable) ]
158+ macro_rules! lazy_static {
159+ ( $( #[ $attr: meta] ) * static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
160+ __lazy_static_internal!( @PRIV , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
161+ } ;
162+ ( $( #[ $attr: meta] ) * pub static ref $N: ident : $T: ty = $e: expr; $( $t: tt) * ) => {
163+ __lazy_static_internal!( @PUB , $( #[ $attr] ) * static ref $N : $T = $e; $( $t) * ) ;
164+ } ;
165+ ( ) => ( )
166+ }
167+
168+ /*
169+ trait LazyStatic<T>: Deref<Target=T> {
170+
171+ }
172+
173+ ///
174+ pub fn initialize<T>(lazy: &lazy::Lazy<T>) {
175+
176+ }
177+ */
0 commit comments