Skip to content

Commit 5c70ffb

Browse files
committed
Fix macro, harden by always using #![no_std], add test
1 parent d37cae0 commit 5c70ffb

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ 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)]
72+
73+
#![no_std]
7374

7475
#[cfg(not(feature="nightly"))]
7576
pub mod lazy;
@@ -82,6 +83,8 @@ pub mod lazy;
8283
#[path="core_lazy.rs"]
8384
pub mod lazy;
8485

86+
pub use core::ops::Deref as __Deref;
87+
8588
#[macro_export]
8689
#[cfg_attr(feature="nightly", allow_internal_unstable)]
8790
macro_rules! lazy_static {
@@ -93,7 +96,7 @@ macro_rules! lazy_static {
9396
};
9497
(@$VIS:ident, $(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
9598
lazy_static!(@MAKE TY, $VIS, $(#[$attr])*, $N);
96-
impl ::std::ops::Deref for $N {
99+
impl $crate::__Deref for $N {
97100
type Target = $T;
98101
#[allow(unsafe_code)]
99102
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="no_std")]
2+
#![feature(const_fn, core_intrinsics)]
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+
}

0 commit comments

Comments
 (0)