diff --git a/.travis.yml b/.travis.yml index 2ed2975..2280263 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,9 @@ script: travis-cargo build && travis-cargo test && travis-cargo bench && + travis-cargo --only nightly build -- --features spin_no_std && + travis-cargo --only nightly test -- --features spin_no_std && + travis-cargo --only nightly bench -- --features spin_no_std && travis-cargo --only stable doc after_success: - travis-cargo --only stable doc-upload diff --git a/Cargo.toml b/Cargo.toml index 2be780e..487121e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,10 @@ documentation = "http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/i repository = "https://github.com/rust-lang-nursery/lazy-static.rs" keywords = ["macro", "lazy", "static"] +[dependencies.spin] +version = "0.4" +optional = true + [features] nightly = [] +spin_no_std = ["nightly", "spin"] diff --git a/src/core_lazy.rs b/src/core_lazy.rs new file mode 100644 index 0000000..20290d9 --- /dev/null +++ b/src/core_lazy.rs @@ -0,0 +1,27 @@ +extern crate spin; + +use self::spin::Once; + +pub struct Lazy(Once); + +impl Lazy { + #[inline(always)] + pub const fn new() -> Self { + Lazy(Once::new()) + } + + #[inline(always)] + pub fn get(&'static self, builder: F) -> &T + where F: FnOnce() -> T + { + self.0.call_once(builder) + } +} + +#[macro_export] +#[allow_internal_unstable] +macro_rules! __lazy_static_create { + ($NAME:ident, $T:ty) => { + static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new(); + } +} diff --git a/src/lazy.rs b/src/lazy.rs index 9bf8ba8..c7e6cef 100644 --- a/src/lazy.rs +++ b/src/lazy.rs @@ -1,4 +1,7 @@ -use std::sync::Once; +extern crate std; + +use self::std::prelude::v1::*; +use self::std::sync::Once; pub struct Lazy(pub *const T, pub Once); diff --git a/src/lib.rs b/src/lib.rs index fc082d8..4837066 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,13 +70,21 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at #![cfg_attr(feature="nightly", feature(const_fn, allow_internal_unstable, core_intrinsics))] +#![no_std] + #[cfg(not(feature="nightly"))] pub mod lazy; -#[cfg(feature="nightly")] +#[cfg(all(feature="nightly", not(feature="spin_no_std")))] #[path="nightly_lazy.rs"] pub mod lazy; +#[cfg(all(feature="nightly", feature="spin_no_std"))] +#[path="core_lazy.rs"] +pub mod lazy; + +pub use core::ops::Deref as __Deref; + #[macro_export] #[cfg_attr(feature="nightly", allow_internal_unstable)] macro_rules! lazy_static { @@ -88,7 +96,7 @@ macro_rules! lazy_static { }; (@$VIS:ident, $(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => { lazy_static!(@MAKE TY, $VIS, $(#[$attr])*, $N); - impl ::std::ops::Deref for $N { + impl $crate::__Deref for $N { type Target = $T; #[allow(unsafe_code)] fn deref<'a>(&'a self) -> &'a $T { diff --git a/src/nightly_lazy.rs b/src/nightly_lazy.rs index b7c7b7d..5f7a110 100644 --- a/src/nightly_lazy.rs +++ b/src/nightly_lazy.rs @@ -1,7 +1,8 @@ -use std::sync::Once; +extern crate std; -use std::cell::UnsafeCell; -use std::sync::ONCE_INIT; +use self::std::prelude::v1::*; +use self::std::cell::UnsafeCell; +use self::std::sync::{Once, ONCE_INIT}; pub struct Lazy(UnsafeCell>, Once); @@ -22,7 +23,7 @@ impl Lazy { match *self.0.get() { Some(ref x) => x, - None => ::std::intrinsics::unreachable(), + None => std::intrinsics::unreachable(), } } } diff --git a/tests/no_std.rs b/tests/no_std.rs new file mode 100644 index 0000000..b460e79 --- /dev/null +++ b/tests/no_std.rs @@ -0,0 +1,21 @@ +#![cfg(feature="spin_no_std")] +#![feature(const_fn)] + +#![no_std] + +#[macro_use] +extern crate lazy_static; + +lazy_static! { + /// Documentation! + pub static ref NUMBER: u32 = times_two(3); +} + +fn times_two(n: u32) -> u32 { + n * 2 +} + +#[test] +fn test_basic() { + assert_eq!(*NUMBER, 6); +} diff --git a/tests/test.rs b/tests/test.rs index 37a521c..71d22e3 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature="nightly", feature(const_fn, core_intrinsics))] +#![cfg_attr(feature="nightly", feature(const_fn))] #[macro_use] extern crate lazy_static;