Skip to content

Commit a308da1

Browse files
committed
Switch lazy_static over to mutable statics instead of UnsafeCell on nightly
1 parent 22f755a commit a308da1

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/nightly_lazy.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@
88
extern crate std;
99

1010
use self::std::prelude::v1::*;
11-
use self::std::cell::UnsafeCell;
1211
use self::std::sync::{Once, ONCE_INIT};
1312

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

1615
impl<T: Sync> Lazy<T> {
1716
#[inline(always)]
1817
pub const fn new() -> Self {
19-
Lazy(UnsafeCell::new(None), ONCE_INIT)
18+
Lazy(None, ONCE_INIT)
2019
}
2120

2221
#[inline(always)]
23-
pub fn get<F>(&'static self, f: F) -> &T
22+
pub fn get<F>(&'static mut self, f: F) -> &T
2423
where F: FnOnce() -> T
2524
{
26-
unsafe {
25+
{
26+
let r = &mut self.0;
2727
self.1.call_once(|| {
28-
*self.0.get() = Some(f());
28+
*r = Some(f());
2929
});
30-
31-
match *self.0.get() {
30+
}
31+
unsafe {
32+
match self.0 {
3233
Some(ref x) => x,
3334
None => std::intrinsics::unreachable(),
3435
}
@@ -43,6 +44,6 @@ unsafe impl<T: Sync> Sync for Lazy<T> {}
4344
#[doc(hidden)]
4445
macro_rules! __lazy_static_create {
4546
($NAME:ident, $T:ty) => {
46-
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
47+
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
4748
}
4849
}

0 commit comments

Comments
 (0)