Skip to content

Commit

Permalink
publish 1.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Mar 2, 2021
1 parent 7fcca37 commit 3872664
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Changelog

## 1.7.2

- Improve code size when using parking_lot feature.

## 1.7.1

- Fix `race::OnceBox<T>` to also impl `Default` even if `T` doesn't impl `Default`.
- Improve code size.

## 1.7.0

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "once_cell"
version = "1.7.1"
version = "1.7.2"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand Down
4 changes: 3 additions & 1 deletion src/imp_pl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{

use parking_lot::Mutex;

use crate::take_unchecked;

pub(crate) struct OnceCell<T> {
mutex: Mutex<()>,
is_initialized: AtomicBool,
Expand Down Expand Up @@ -58,7 +60,7 @@ impl<T> OnceCell<T> {
// but that is more complicated
// - finally, if it returns Ok, we store the value and store the flag with
// `Release`, which synchronizes with `Acquire`s.
let f = f.take().unwrap_or_else(|| unsafe { hint::unreachable_unchecked() });
let f = unsafe { take_unchecked(&mut f) };
match f() {
Ok(value) => unsafe {
// Safe b/c we have a unique access and no panic may happen
Expand Down
4 changes: 3 additions & 1 deletion src/imp_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::{
thread::{self, Thread},
};

use crate::take_unchecked;

#[derive(Debug)]
pub(crate) struct OnceCell<T> {
// This `state` word is actually an encoded version of just a pointer to a
Expand Down Expand Up @@ -89,7 +91,7 @@ impl<T> OnceCell<T> {
let mut res: Result<(), E> = Ok(());
let slot: *mut Option<T> = self.value.get();
initialize_inner(&self.state_and_queue, &mut || {
let f = f.take().unwrap_or_else(|| unsafe { unreachable_unchecked() });
let f = unsafe { take_unchecked(&mut f) };
match f() {
Ok(value) => {
unsafe { *slot = Some(value) };
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,3 +1067,14 @@ pub mod sync {

#[cfg(feature = "race")]
pub mod race;

#[cfg(feature = "std")]
unsafe fn take_unchecked<T>(val: &mut Option<T>) -> T {
match val.take() {
Some(it) => it,
None => {
debug_assert!(false);
std::hint::unreachable_unchecked()
}
}
}

0 comments on commit 3872664

Please sign in to comment.