Skip to content

Commit a545f52

Browse files
committed
Overhaul docs a bit, and hide all implementation details
1 parent fae1989 commit a545f52

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

src/core_lazy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl<T: Sync> Lazy<T> {
2020

2121
#[macro_export]
2222
#[allow_internal_unstable]
23+
#[doc(hidden)]
2324
macro_rules! __lazy_static_create {
2425
($NAME:ident, $T:ty) => {
2526
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();

src/lazy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl<T: Sync> Lazy<T> {
2424
unsafe impl<T: Sync> Sync for Lazy<T> {}
2525

2626
#[macro_export]
27+
#[doc(hidden)]
2728
macro_rules! __lazy_static_create {
2829
($NAME:ident, $T:ty) => {
2930
use std::sync::ONCE_INIT;

src/lib.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2435
For 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
2637
attaching to this type.)
2738
2839
On 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)]
7789
pub mod lazy;
7890

7991
#[cfg(all(feature="nightly", not(feature="spin_no_std")))]
8092
#[path="nightly_lazy.rs"]
93+
#[doc(hidden)]
8194
pub mod lazy;
8295

8396
#[cfg(all(feature="nightly", feature="spin_no_std"))]
8497
#[path="core_lazy.rs"]
98+
#[doc(hidden)]
8599
pub mod lazy;
86100

101+
#[doc(hidden)]
87102
pub 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+
*/

src/nightly_lazy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ unsafe impl<T: Sync> Sync for Lazy<T> {}
3333

3434
#[macro_export]
3535
#[allow_internal_unstable]
36+
#[doc(hidden)]
3637
macro_rules! __lazy_static_create {
3738
($NAME:ident, $T:ty) => {
3839
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();

0 commit comments

Comments
 (0)