Skip to content

Commit cd632cb

Browse files
authored
Merge pull request #55 from rust-lang-nursery/destructor_drop
Switch lazy_static over to mutable statics instead of UnsafeCell on nightly
2 parents 22f755a + 8fff063 commit cd632cb

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ keywords = ["macro", "lazy", "static"]
1414
categories = [ "no-std", "rust-patterns", "memory-management" ]
1515

1616
[dependencies.spin]
17-
version = "0.4"
17+
version = "0.4.6"
1818
optional = true
1919

2020
[dependencies.compiletest_rs]

src/core_lazy.rs

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ impl<T: Sync> Lazy<T> {
2626
}
2727

2828
#[macro_export]
29-
#[allow_internal_unstable]
30-
#[doc(hidden)]
3129
macro_rules! __lazy_static_create {
3230
($NAME:ident, $T:ty) => {
3331
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();

src/lazy.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern crate std;
99

1010
use self::std::prelude::v1::*;
1111
use self::std::sync::Once;
12+
pub use self::std::sync::ONCE_INIT;
1213

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

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

3334
#[macro_export]
34-
#[doc(hidden)]
3535
macro_rules! __lazy_static_create {
3636
($NAME:ident, $T:ty) => {
37-
use std::sync::ONCE_INIT;
38-
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, ONCE_INIT);
37+
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(0 as *const $T, $crate::lazy::ONCE_INIT);
3938
}
4039
}

src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ The `Deref` implementation uses a hidden static variable that is guarded by a at
9090
9191
*/
9292

93-
#![cfg_attr(feature="nightly", feature(const_fn, allow_internal_unstable, core_intrinsics, const_unsafe_cell_new))]
93+
#![cfg_attr(feature="spin_no_std", feature(const_fn))]
94+
#![cfg_attr(feature="nightly", feature(unreachable))]
9495

95-
#![doc(html_root_url = "https://docs.rs/lazy_static/0.2.8")]
96+
#![doc(html_root_url = "https://docs.rs/lazy_static/0.2.10")]
9697
#![no_std]
9798

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

115116
#[macro_export]
116-
#[cfg_attr(feature="nightly", allow_internal_unstable)]
117117
#[doc(hidden)]
118118
macro_rules! __lazy_static_internal {
119119
// optional visibility restrictions are wrapped in `()` to allow for
@@ -161,7 +161,6 @@ macro_rules! __lazy_static_internal {
161161
}
162162

163163
#[macro_export]
164-
#[cfg_attr(feature="nightly", allow_internal_unstable)]
165164
macro_rules! lazy_static {
166165
($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
167166
// use `()` to explicitly forward the information about private items

src/nightly_lazy.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,26 @@
88
extern crate std;
99

1010
use self::std::prelude::v1::*;
11-
use self::std::cell::UnsafeCell;
12-
use self::std::sync::{Once, ONCE_INIT};
11+
use self::std::sync::Once;
12+
pub use self::std::sync::ONCE_INIT;
1313

14-
pub struct Lazy<T: Sync>(UnsafeCell<Option<T>>, Once);
14+
pub struct Lazy<T: Sync>(pub Option<T>, pub Once);
1515

1616
impl<T: Sync> Lazy<T> {
1717
#[inline(always)]
18-
pub const fn new() -> Self {
19-
Lazy(UnsafeCell::new(None), ONCE_INIT)
20-
}
21-
22-
#[inline(always)]
23-
pub fn get<F>(&'static self, f: F) -> &T
18+
pub fn get<F>(&'static mut self, f: F) -> &T
2419
where F: FnOnce() -> T
2520
{
26-
unsafe {
21+
{
22+
let r = &mut self.0;
2723
self.1.call_once(|| {
28-
*self.0.get() = Some(f());
24+
*r = Some(f());
2925
});
30-
31-
match *self.0.get() {
26+
}
27+
unsafe {
28+
match self.0 {
3229
Some(ref x) => x,
33-
None => std::intrinsics::unreachable(),
30+
None => std::mem::unreachable(),
3431
}
3532
}
3633
}
@@ -39,10 +36,8 @@ impl<T: Sync> Lazy<T> {
3936
unsafe impl<T: Sync> Sync for Lazy<T> {}
4037

4138
#[macro_export]
42-
#[allow_internal_unstable]
43-
#[doc(hidden)]
4439
macro_rules! __lazy_static_create {
4540
($NAME:ident, $T:ty) => {
46-
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
41+
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy(None, $crate::lazy::ONCE_INIT);
4742
}
4843
}

tests/compile-fail/static_is_sized.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// error-pattern:the trait bound `str: std::marker::Sized` is not satisfied
2+
#[macro_use]
3+
extern crate lazy_static;
4+
5+
lazy_static! {
6+
pub static ref FOO: str = panic!();
7+
}
8+
9+
10+
fn main() {
11+
}

0 commit comments

Comments
 (0)