Skip to content

Commit 528ea62

Browse files
committed
Spinning no_std feature
Fixes #44
1 parent 2003770 commit 528ea62

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ documentation = "http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/i
1111
repository = "https://github.com/rust-lang-nursery/lazy-static.rs"
1212
keywords = ["macro", "lazy", "static"]
1313

14+
[dependencies.spin]
15+
version = "0.4"
16+
optional = true
17+
1418
[features]
1519
nightly = []
20+
no_std = ["nightly", "spin"]

src/core_lazy.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extern crate spin;
2+
3+
use self::spin::Once;
4+
5+
pub struct Lazy<T: Sync>(Once<T>);
6+
7+
impl<T: Sync> Lazy<T> {
8+
#[inline(always)]
9+
pub const fn new() -> Self {
10+
Lazy(Once::new())
11+
}
12+
13+
#[inline(always)]
14+
pub fn get<F>(&'static self, builder: F) -> &T
15+
where F: FnOnce() -> T
16+
{
17+
self.0.call_once(builder)
18+
}
19+
}
20+
21+
#[macro_export]
22+
#[allow_internal_unstable]
23+
macro_rules! __lazy_static_create {
24+
($NAME:ident, $T:ty) => {
25+
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
26+
}
27+
}

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at
6969
*/
7070

7171
#![cfg_attr(feature="nightly", feature(const_fn, allow_internal_unstable, core_intrinsics))]
72+
#![cfg_attr(feature="no_std", no_std)]
7273

7374
#[cfg(not(feature="nightly"))]
7475
pub mod lazy;
7576

76-
#[cfg(feature="nightly")]
77+
#[cfg(all(feature="nightly", not(feature="no_std")))]
7778
#[path="nightly_lazy.rs"]
7879
pub mod lazy;
7980

81+
#[cfg(all(feature="nightly", feature="no_std"))]
82+
#[path="core_lazy.rs"]
83+
pub mod lazy;
84+
8085
#[macro_export]
8186
#[cfg_attr(feature="nightly", allow_internal_unstable)]
8287
macro_rules! lazy_static {

0 commit comments

Comments
 (0)