Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
27 changes: 27 additions & 0 deletions src/core_lazy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
extern crate spin;

use self::spin::Once;

pub struct Lazy<T: Sync>(Once<T>);

impl<T: Sync> Lazy<T> {
#[inline(always)]
pub const fn new() -> Self {
Lazy(Once::new())
}

#[inline(always)]
pub fn get<F>(&'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();
}
}
5 changes: 4 additions & 1 deletion src/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::Once;
extern crate std;

use self::std::prelude::v1::*;
use self::std::sync::Once;

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

Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions src/nightly_lazy.rs
Original file line number Diff line number Diff line change
@@ -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<T: Sync>(UnsafeCell<Option<T>>, Once);

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

match *self.0.get() {
Some(ref x) => x,
None => ::std::intrinsics::unreachable(),
None => std::intrinsics::unreachable(),
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/no_std.rs
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down