Skip to content

Commit

Permalink
Switch lazy_static over to mutable statics instead of UnsafeCell on n…
Browse files Browse the repository at this point in the history
…ightly
  • Loading branch information
Kimundi committed Nov 13, 2017
1 parent 22f755a commit a308da1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/nightly_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@
extern crate std;

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);
pub struct Lazy<T: Sync>(Option<T>, Once);

impl<T: Sync> Lazy<T> {
#[inline(always)]
pub const fn new() -> Self {
Lazy(UnsafeCell::new(None), ONCE_INIT)
Lazy(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(),
}
Expand All @@ -43,6 +44,6 @@ unsafe impl<T: Sync> Sync for Lazy<T> {}
#[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::new();
}
}

0 comments on commit a308da1

Please sign in to comment.