From 528ea6230e322bd8e73270284ff7b97be65c7129 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 19 Apr 2016 16:15:09 -0700 Subject: [PATCH 1/5] Spinning no_std feature Fixes #44 --- Cargo.toml | 5 +++++ src/core_lazy.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 7 ++++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/core_lazy.rs diff --git a/Cargo.toml b/Cargo.toml index 2be780e..c425b1f 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 = [] +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/lib.rs b/src/lib.rs index fc082d8..f77fc95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,14 +69,19 @@ 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))] +#![cfg_attr(feature="no_std", no_std)] #[cfg(not(feature="nightly"))] pub mod lazy; -#[cfg(feature="nightly")] +#[cfg(all(feature="nightly", not(feature="no_std")))] #[path="nightly_lazy.rs"] pub mod lazy; +#[cfg(all(feature="nightly", feature="no_std"))] +#[path="core_lazy.rs"] +pub mod lazy; + #[macro_export] #[cfg_attr(feature="nightly", allow_internal_unstable)] macro_rules! lazy_static { From d37cae0dfbef52021c26f49af47e0a5d11be9049 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 19 Apr 2016 18:34:45 -0700 Subject: [PATCH 2/5] Make sure travis test no_std feature --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2ed2975..2bd8771 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 no_std && + travis-cargo --only nightly test -- --features no_std && + travis-cargo --only nightly bench -- --features no_std && travis-cargo --only stable doc after_success: - travis-cargo --only stable doc-upload From 5c70ffba0c135c4f18fd139fdceb539bdf3cda3f Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 20 Apr 2016 14:14:48 -0700 Subject: [PATCH 3/5] Fix macro, harden by always using #![no_std], add test --- src/lazy.rs | 5 ++++- src/lib.rs | 7 +++++-- src/nightly_lazy.rs | 9 +++++---- tests/no_std.rs | 21 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 tests/no_std.rs 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 f77fc95..aeceb67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,8 @@ 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))] -#![cfg_attr(feature="no_std", no_std)] + +#![no_std] #[cfg(not(feature="nightly"))] pub mod lazy; @@ -82,6 +83,8 @@ pub mod lazy; #[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 { @@ -93,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..16d4679 --- /dev/null +++ b/tests/no_std.rs @@ -0,0 +1,21 @@ +#![cfg(feature="no_std")] +#![feature(const_fn, core_intrinsics)] + +#![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); +} From ecf1de08c1b93cb7ed5a29c41baab67b48383689 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 20 Apr 2016 14:24:11 -0700 Subject: [PATCH 4/5] Don't need the core_intrinsics feature in so many places --- tests/no_std.rs | 2 +- tests/test.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/no_std.rs b/tests/no_std.rs index 16d4679..6b3c3ca 100644 --- a/tests/no_std.rs +++ b/tests/no_std.rs @@ -1,5 +1,5 @@ #![cfg(feature="no_std")] -#![feature(const_fn, core_intrinsics)] +#![feature(const_fn)] #![no_std] 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; From b230b7a945fa627df5cf84b11089cb975dc63e9a Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sun, 24 Apr 2016 15:03:55 -0700 Subject: [PATCH 5/5] Rename no_std feature to spin_no_std, since it changes algorithm --- .travis.yml | 6 +++--- Cargo.toml | 2 +- src/lib.rs | 4 ++-- tests/no_std.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2bd8771..2280263 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,9 @@ script: travis-cargo build && travis-cargo test && travis-cargo bench && - travis-cargo --only nightly build -- --features no_std && - travis-cargo --only nightly test -- --features no_std && - travis-cargo --only nightly bench -- --features no_std && + 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 c425b1f..487121e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,4 @@ optional = true [features] nightly = [] -no_std = ["nightly", "spin"] +spin_no_std = ["nightly", "spin"] diff --git a/src/lib.rs b/src/lib.rs index aeceb67..4837066 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,11 +75,11 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at #[cfg(not(feature="nightly"))] pub mod lazy; -#[cfg(all(feature="nightly", not(feature="no_std")))] +#[cfg(all(feature="nightly", not(feature="spin_no_std")))] #[path="nightly_lazy.rs"] pub mod lazy; -#[cfg(all(feature="nightly", feature="no_std"))] +#[cfg(all(feature="nightly", feature="spin_no_std"))] #[path="core_lazy.rs"] pub mod lazy; diff --git a/tests/no_std.rs b/tests/no_std.rs index 6b3c3ca..b460e79 100644 --- a/tests/no_std.rs +++ b/tests/no_std.rs @@ -1,4 +1,4 @@ -#![cfg(feature="no_std")] +#![cfg(feature="spin_no_std")] #![feature(const_fn)] #![no_std]