Skip to content

Commit

Permalink
Merge pull request #55 from rust-lang-nursery/destructor_drop
Browse files Browse the repository at this point in the history
Switch lazy_static over to mutable statics instead of UnsafeCell on nightly
  • Loading branch information
Kimundi authored Nov 20, 2017
2 parents 22f755a + 8fff063 commit cd632cb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["macro", "lazy", "static"]
categories = [ "no-std", "rust-patterns", "memory-management" ]

[dependencies.spin]
version = "0.4"
version = "0.4.6"
optional = true

[dependencies.compiletest_rs]
Expand Down
2 changes: 0 additions & 2 deletions src/core_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ impl<T: Sync> Lazy<T> {
}

#[macro_export]
#[allow_internal_unstable]
#[doc(hidden)]
macro_rules! __lazy_static_create {
($NAME:ident, $T:ty) => {
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
Expand Down
5 changes: 2 additions & 3 deletions src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate std;

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

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

Expand All @@ -31,10 +32,8 @@ impl<T: Sync> Lazy<T> {
unsafe impl<T: Sync> Sync for Lazy<T> {}

#[macro_export]
#[doc(hidden)]
macro_rules! __lazy_static_create {
($NAME:ident, $T:ty) => {
use std::sync::ONCE_INIT;
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, ONCE_INIT);
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, $crate::lazy::ONCE_INIT);
}
}
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ 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, const_unsafe_cell_new))]
#![cfg_attr(feature="spin_no_std", feature(const_fn))]
#![cfg_attr(feature="nightly", feature(unreachable))]

#![doc(html_root_url = "https://docs.rs/lazy_static/0.2.8")]
#![doc(html_root_url = "https://docs.rs/lazy_static/0.2.10")]
#![no_std]

#[cfg(not(feature="nightly"))]
Expand All @@ -113,7 +114,6 @@ pub mod lazy;
pub use core::ops::Deref as __Deref;

#[macro_export]
#[cfg_attr(feature="nightly", allow_internal_unstable)]
#[doc(hidden)]
macro_rules! __lazy_static_internal {
// optional visibility restrictions are wrapped in `()` to allow for
Expand Down Expand Up @@ -161,7 +161,6 @@ macro_rules! __lazy_static_internal {
}

#[macro_export]
#[cfg_attr(feature="nightly", allow_internal_unstable)]
macro_rules! lazy_static {
($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
// use `()` to explicitly forward the information about private items
Expand Down
29 changes: 12 additions & 17 deletions src/nightly_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@
extern crate std;

use self::std::prelude::v1::*;
use self::std::cell::UnsafeCell;
use self::std::sync::{Once, ONCE_INIT};
use self::std::sync::Once;
pub use self::std::sync::ONCE_INIT;

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

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

#[inline(always)]
pub fn get<F>(&'static self, f: F) -> &T
pub fn get<F>(&'static mut self, f: F) -> &T
where F: FnOnce() -> T
{
unsafe {
{
let r = &mut self.0;
self.1.call_once(|| {
*self.0.get() = Some(f());
*r = Some(f());
});

match *self.0.get() {
}
unsafe {
match self.0 {
Some(ref x) => x,
None => std::intrinsics::unreachable(),
None => std::mem::unreachable(),
}
}
}
Expand All @@ -39,10 +36,8 @@ impl<T: Sync> Lazy<T> {
unsafe impl<T: Sync> Sync for Lazy<T> {}

#[macro_export]
#[allow_internal_unstable]
#[doc(hidden)]
macro_rules! __lazy_static_create {
($NAME:ident, $T:ty) => {
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(None, $crate::lazy::ONCE_INIT);
}
}
11 changes: 11 additions & 0 deletions tests/compile-fail/static_is_sized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// error-pattern:the trait bound `str: std::marker::Sized` is not satisfied
#[macro_use]
extern crate lazy_static;

lazy_static! {
pub static ref FOO: str = panic!();
}


fn main() {
}

0 comments on commit cd632cb

Please sign in to comment.