Skip to content

Commit 4fe25de

Browse files
committed
Switch lazy_static over to mutable statics instead of UnsafeCell on nightly
1 parent f026ac2 commit 4fe25de

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/nightly_lazy.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
extern crate std;
22

33
use self::std::prelude::v1::*;
4-
use self::std::cell::UnsafeCell;
54
use self::std::sync::{Once, ONCE_INIT};
65

7-
pub struct Lazy<T: Sync>(UnsafeCell<Option<T>>, Once);
6+
pub struct Lazy<T: Sync>(Option<T>, Once);
87

98
impl<T: Sync> Lazy<T> {
109
#[inline(always)]
1110
pub const fn new() -> Self {
12-
Lazy(UnsafeCell::new(None), ONCE_INIT)
11+
Lazy(None, ONCE_INIT)
1312
}
1413

1514
#[inline(always)]
16-
pub fn get<F>(&'static self, f: F) -> &T
15+
pub fn get<F>(&'static mut self, f: F) -> &T
1716
where F: FnOnce() -> T
1817
{
19-
unsafe {
18+
{
19+
let r = &mut self.0;
2020
self.1.call_once(|| {
21-
*self.0.get() = Some(f());
21+
*r = Some(f());
2222
});
23-
24-
match *self.0.get() {
23+
}
24+
unsafe {
25+
match self.0 {
2526
Some(ref x) => x,
2627
None => std::intrinsics::unreachable(),
2728
}
@@ -35,6 +36,6 @@ unsafe impl<T: Sync> Sync for Lazy<T> {}
3536
#[allow_internal_unstable]
3637
macro_rules! __lazy_static_create {
3738
($NAME:ident, $T:ty) => {
38-
static $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
39+
static mut $NAME: $crate::lazy::Lazy<$T> = $crate::lazy::Lazy::new();
3940
}
4041
}

0 commit comments

Comments
 (0)