Skip to content

Commit 2005102

Browse files
committed
Merge pull request #46 from RustOS-Fork-Holding-Ground/core
Spinning no_std feature
2 parents 2003770 + b230b7a commit 2005102

File tree

8 files changed

+76
-8
lines changed

8 files changed

+76
-8
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ script:
1212
travis-cargo build &&
1313
travis-cargo test &&
1414
travis-cargo bench &&
15+
travis-cargo --only nightly build -- --features spin_no_std &&
16+
travis-cargo --only nightly test -- --features spin_no_std &&
17+
travis-cargo --only nightly bench -- --features spin_no_std &&
1518
travis-cargo --only stable doc
1619
after_success:
1720
- travis-cargo --only stable doc-upload

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+
spin_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/lazy.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::sync::Once;
1+
extern crate std;
2+
3+
use self::std::prelude::v1::*;
4+
use self::std::sync::Once;
25

36
pub struct Lazy<T: Sync>(pub *const T, pub Once);
47

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at
7070

7171
#![cfg_attr(feature="nightly", feature(const_fn, allow_internal_unstable, core_intrinsics))]
7272

73+
#![no_std]
74+
7375
#[cfg(not(feature="nightly"))]
7476
pub mod lazy;
7577

76-
#[cfg(feature="nightly")]
78+
#[cfg(all(feature="nightly", not(feature="spin_no_std")))]
7779
#[path="nightly_lazy.rs"]
7880
pub mod lazy;
7981

82+
#[cfg(all(feature="nightly", feature="spin_no_std"))]
83+
#[path="core_lazy.rs"]
84+
pub mod lazy;
85+
86+
pub use core::ops::Deref as __Deref;
87+
8088
#[macro_export]
8189
#[cfg_attr(feature="nightly", allow_internal_unstable)]
8290
macro_rules! lazy_static {
@@ -88,7 +96,7 @@ macro_rules! lazy_static {
8896
};
8997
(@$VIS:ident, $(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
9098
lazy_static!(@MAKE TY, $VIS, $(#[$attr])*, $N);
91-
impl ::std::ops::Deref for $N {
99+
impl $crate::__Deref for $N {
92100
type Target = $T;
93101
#[allow(unsafe_code)]
94102
fn deref<'a>(&'a self) -> &'a $T {

src/nightly_lazy.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::sync::Once;
1+
extern crate std;
22

3-
use std::cell::UnsafeCell;
4-
use std::sync::ONCE_INIT;
3+
use self::std::prelude::v1::*;
4+
use self::std::cell::UnsafeCell;
5+
use self::std::sync::{Once, ONCE_INIT};
56

67
pub struct Lazy<T: Sync>(UnsafeCell<Option<T>>, Once);
78

@@ -22,7 +23,7 @@ impl<T: Sync> Lazy<T> {
2223

2324
match *self.0.get() {
2425
Some(ref x) => x,
25-
None => ::std::intrinsics::unreachable(),
26+
None => std::intrinsics::unreachable(),
2627
}
2728
}
2829
}

tests/no_std.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![cfg(feature="spin_no_std")]
2+
#![feature(const_fn)]
3+
4+
#![no_std]
5+
6+
#[macro_use]
7+
extern crate lazy_static;
8+
9+
lazy_static! {
10+
/// Documentation!
11+
pub static ref NUMBER: u32 = times_two(3);
12+
}
13+
14+
fn times_two(n: u32) -> u32 {
15+
n * 2
16+
}
17+
18+
#[test]
19+
fn test_basic() {
20+
assert_eq!(*NUMBER, 6);
21+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg_attr(feature="nightly", feature(const_fn, core_intrinsics))]
1+
#![cfg_attr(feature="nightly", feature(const_fn))]
22

33
#[macro_use]
44
extern crate lazy_static;

0 commit comments

Comments
 (0)