Skip to content

Commit 59f776e

Browse files
authored
Merge pull request #192 from Kixunil/change-option-to-maybeuninit
Replace `Option<T>` with `MaybeUninit<T>`
2 parents 35f7cdb + d545a33 commit 59f776e

File tree

3 files changed

+8
-21
lines changed

3 files changed

+8
-21
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: rust
22
matrix:
33
include:
4-
- rust: 1.27.2
4+
- rust: 1.36.0
55
script:
66
- cargo test --tests
77
- rust: stable

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ as well as anything that requires non-const function calls to be computed.
1515

1616
## Minimum supported `rustc`
1717

18-
`1.27.2+`
18+
`1.36.0+`
1919

2020
This version is explicitly tested in CI and may only be bumped in new minor versions. Any changes to the supported minimum version will be called out in the release notes.
2121

src/inline_lazy.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,30 @@ extern crate core;
99
extern crate std;
1010

1111
use self::std::cell::Cell;
12-
use self::std::hint::unreachable_unchecked;
12+
use self::std::mem::MaybeUninit;
1313
use self::std::prelude::v1::*;
1414
use self::std::sync::Once;
1515
#[allow(deprecated)]
1616
pub use self::std::sync::ONCE_INIT;
1717

18-
// FIXME: Replace Option<T> with MaybeUninit<T> (stable since 1.36.0)
19-
pub struct Lazy<T: Sync>(Cell<Option<T>>, Once);
18+
pub struct Lazy<T: Sync>(Cell<MaybeUninit<T>>, Once);
2019

2120
impl<T: Sync> Lazy<T> {
2221
#[allow(deprecated)]
23-
pub const INIT: Self = Lazy(Cell::new(None), ONCE_INIT);
22+
pub const INIT: Self = Lazy(Cell::new(MaybeUninit::uninit()), ONCE_INIT);
2423

2524
#[inline(always)]
2625
pub fn get<F>(&'static self, f: F) -> &T
2726
where
2827
F: FnOnce() -> T,
2928
{
3029
self.1.call_once(|| {
31-
self.0.set(Some(f()));
30+
self.0.set(MaybeUninit::new(f()));
3231
});
3332

34-
// `self.0` is guaranteed to be `Some` by this point
33+
// `self.0` is guaranteed to be initialized by this point
3534
// The `Once` will catch and propagate panics
36-
unsafe {
37-
match *self.0.as_ptr() {
38-
Some(ref x) => x,
39-
None => {
40-
debug_assert!(
41-
false,
42-
"attempted to dereference an uninitialized lazy static. This is a bug"
43-
);
44-
45-
unreachable_unchecked()
46-
}
47-
}
48-
}
35+
unsafe { &*(*self.0.as_ptr()).as_ptr() }
4936
}
5037
}
5138

0 commit comments

Comments
 (0)