Skip to content

Commit 60db339

Browse files
committed
Use feature cfg outside macro
cfg(feature="nightly") was being evaluated within the crate using the lazy_static! macro. Instead we generate different macros depending on the cfg. Signed-off-by: Dan Schatzberg <[email protected]>
1 parent bc740c8 commit 60db339

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/lib.rs

+54-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trait.
3535
3636
Using the macro:
3737
38-
```rust
38+
```ignore
3939
#[macro_use]
4040
extern crate lazy_static;
4141
@@ -71,6 +71,7 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at
7171
#![cfg_attr(feature="nightly", feature(const_fn, core_intrinsics))]
7272
#![crate_type = "dylib"]
7373

74+
#[cfg(feature="nightly")]
7475
#[macro_export]
7576
macro_rules! lazy_static {
7677
($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
@@ -94,7 +95,6 @@ macro_rules! lazy_static {
9495
fn require_sync<T: Sync>(_: &T) { }
9596

9697
#[inline(always)]
97-
#[cfg(feature="nightly")]
9898
unsafe fn __stability() -> &'static $T {
9999
use std::cell::UnsafeCell;
100100

@@ -112,8 +112,59 @@ macro_rules! lazy_static {
112112
}
113113
}
114114

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+
115167
#[inline(always)]
116-
#[cfg(not(feature="nightly"))]
117168
unsafe fn __stability() -> &'static $T {
118169
use std::mem::transmute;
119170

0 commit comments

Comments
 (0)