Skip to content

Commit cb85e6b

Browse files
committed
publish 1.16.0-pre.1
This also reshuffles features a bit, to clearly separate public API from internal features.
1 parent b56e329 commit cb85e6b

9 files changed

Lines changed: 52 additions & 61 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
-
66

7+
## 1.16.0-pre.1
8+
9+
- Add `no_std` implementation based on `critical-section`,
10+
[#195](https://github.com/matklad/once_cell/pull/195).
11+
- Deprecate `atomic-polyfill` feature (use the new `critical-section` instead)
12+
713
## 1.15.0
814

915
- Increase minimal supported Rust version to 1.56.0.

‎Cargo.toml‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "once_cell"
3-
version = "1.15.0"
3+
version = "1.16.0-pre.1"
44
authors = ["Aleksey Kladov <[email protected]>"]
55
license = "MIT OR Apache-2.0"
66
edition = "2021"
@@ -20,19 +20,10 @@ exclude = ["*.png", "*.svg", "/Cargo.lock.msrv", "rustfmt.toml"]
2020
members = ["xtask"]
2121

2222
[dependencies]
23-
# Uses parking_lot to implement once_cell::sync::OnceCell.
24-
# This makes no speed difference, but makes each OnceCell<T>
25-
# up to 16 bytes smaller, depending on the size of the T.
23+
# These optional dependencies are considered private impl details,
24+
# only features from `[features]` table are a part of semver-guarded API.
2625
parking_lot_core = { version = "0.9.3", optional = true, default_features = false }
27-
28-
# To be used in order to enable the race feature on targets
29-
# that do not have atomics
30-
# *Warning:* This can be unsound. Please read the README of
31-
# [atomic-polyfill](https://github.com/embassy-rs/atomic-polyfill)
32-
# and make sure you understand all the implications
33-
atomic-polyfill = { version = "1", optional = true }
34-
35-
# Uses `critical-section` to implement `once_cell::sync::OnceCell`.
26+
atomic_polyfill = { package = "atomic-polyfill", version = "1", optional = true }
3627
critical_section = { package = "critical-section", version = "1", optional = true }
3728

3829
[dev-dependencies]
@@ -43,21 +34,32 @@ critical_section = { package = "critical-section", version = "1.1.1", features =
4334

4435
[features]
4536
default = ["std"]
37+
4638
# Enables `once_cell::sync` module.
47-
std = ["alloc", "sync"]
39+
std = ["alloc"]
40+
4841
# Enables `once_cell::race::OnceBox` type.
4942
alloc = ["race"]
43+
5044
# Enables `once_cell::race` module.
5145
race = []
46+
47+
# Uses parking_lot to implement once_cell::sync::OnceCell.
48+
# This makes no speed difference, but makes each OnceCell<T>
49+
# up to 16 bytes smaller, depending on the size of the T.
50+
parking_lot = ["parking_lot_core"]
51+
52+
# Uses `critical-section` to implement `sync` and `race` modules. in
53+
# `#![no_std]` mode. Please read `critical-section` docs carefully
54+
# before enabling this feature.
55+
critical-section = ["critical_section", "atomic_polyfill" ]
56+
5257
# Enables semver-exempt APIs of this crate.
5358
# At the moment, this feature is unused.
5459
unstable = []
5560

56-
sync = []
57-
58-
parking_lot = ["parking_lot_core"]
59-
60-
critical-section = ["critical_section", "atomic-polyfill", "sync"]
61+
# Only for backwards compatibility.
62+
atomic-polyfill = ["critical-section"]
6163

6264
[[example]]
6365
name = "bench"

‎src/imp_cs.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<T> OnceCell<T> {
6363
pub(crate) unsafe fn get_unchecked(&self) -> &T {
6464
debug_assert!(self.is_initialized());
6565
// SAFETY: The caller ensures that the value is initialized and access synchronized.
66-
self.value.borrow(CriticalSection::new()).get_unchecked()
66+
crate::unwrap_unchecked(self.value.borrow(CriticalSection::new()).get())
6767
}
6868

6969
#[inline]

‎src/imp_pl.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<T> OnceCell<T> {
5858
// but that is more complicated
5959
// - finally, if it returns Ok, we store the value and store the flag with
6060
// `Release`, which synchronizes with `Acquire`s.
61-
let f = unsafe { crate::take_unchecked(&mut f) };
61+
let f = unsafe { crate::unwrap_unchecked(f.take()) };
6262
match f() {
6363
Ok(value) => unsafe {
6464
// Safe b/c we have a unique access and no panic may happen

‎src/imp_std.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<T> OnceCell<T> {
7878
initialize_or_wait(
7979
&self.queue,
8080
Some(&mut || {
81-
let f = unsafe { crate::take_unchecked(&mut f) };
81+
let f = unsafe { crate::unwrap_unchecked(f.take()) };
8282
match f() {
8383
Ok(value) => {
8484
unsafe { *slot = Some(value) };

‎src/lib.rs‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,6 @@ pub mod unsync {
474474
unsafe { &mut *self.inner.get() }.as_mut()
475475
}
476476

477-
/// Get the reference to the underlying value, without checking if the
478-
/// cell is initialized.
479-
///
480-
/// # Safety
481-
///
482-
/// Caller must ensure that the cell is in initialized state.
483-
#[cfg(feature = "critical-section")]
484-
#[inline]
485-
pub(crate) unsafe fn get_unchecked(&self) -> &T {
486-
crate::unwrap_unchecked(self.get())
487-
}
488-
489477
/// Sets the contents of this cell to `value`.
490478
///
491479
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
@@ -830,7 +818,7 @@ pub mod unsync {
830818
}
831819

832820
/// Thread-safe, blocking version of `OnceCell`.
833-
#[cfg(feature = "sync")]
821+
#[cfg(any(feature = "std", feature = "critical-section"))]
834822
pub mod sync {
835823
use core::{
836824
cell::Cell,
@@ -839,7 +827,7 @@ pub mod sync {
839827
panic::RefUnwindSafe,
840828
};
841829

842-
use super::{imp::OnceCell as Imp, take_unchecked};
830+
use super::{imp::OnceCell as Imp, unwrap_unchecked};
843831

844832
/// A thread-safe cell which can be written to only once.
845833
///
@@ -960,7 +948,7 @@ pub mod sync {
960948
/// assert_eq!(*value, 92);
961949
/// t.join().unwrap();
962950
/// ```
963-
#[cfg(not(feature = "critical-section"))]
951+
#[cfg(feature = "std")]
964952
pub fn wait(&self) -> &T {
965953
if !self.0.is_initialized() {
966954
self.0.wait()
@@ -1050,7 +1038,7 @@ pub mod sync {
10501038
/// ```
10511039
pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)> {
10521040
let mut value = Some(value);
1053-
let res = self.get_or_init(|| unsafe { take_unchecked(&mut value) });
1041+
let res = self.get_or_init(|| unsafe { unwrap_unchecked(value.take()) });
10541042
match value {
10551043
None => Ok(res),
10561044
Some(value) => Err((res, value)),
@@ -1377,12 +1365,7 @@ pub mod sync {
13771365
#[cfg(feature = "race")]
13781366
pub mod race;
13791367

1380-
#[cfg(feature = "sync")]
1381-
#[inline]
1382-
unsafe fn take_unchecked<T>(val: &mut Option<T>) -> T {
1383-
unwrap_unchecked(val.take())
1384-
}
1385-
1368+
// Remove once MSRV is at least 1.58.
13861369
#[inline]
13871370
unsafe fn unwrap_unchecked<T>(val: Option<T>) -> T {
13881371
match val {

‎src/race.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
//! `Acquire` and `Release` have very little performance overhead on most
2020
//! architectures versus `Relaxed`.
2121
22-
#[cfg(feature = "atomic-polyfill")]
22+
#[cfg(feature = "critical-section")]
2323
use atomic_polyfill as atomic;
24-
#[cfg(not(feature = "atomic-polyfill"))]
24+
#[cfg(not(feature = "critical-section"))]
2525
use core::sync::atomic;
2626

2727
use atomic::{AtomicUsize, Ordering};

‎tests/it.rs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ mod unsync {
249249
}
250250
}
251251

252-
#[cfg(feature = "sync")]
252+
#[cfg(any(feature = "std", feature = "critical-section"))]
253253
mod sync {
254254
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
255255

256-
#[cfg(not(feature = "critical-section"))]
256+
#[cfg(feature = "std")]
257257
use std::sync::Barrier;
258258

259-
#[cfg(feature = "critical-section")]
259+
#[cfg(not(feature = "std"))]
260260
use core::cell::Cell;
261261

262262
use crossbeam_utils::thread::scope;
@@ -360,7 +360,7 @@ mod sync {
360360
assert_eq!(cell.get(), Some(&"hello".to_string()));
361361
}
362362

363-
#[cfg(not(feature = "critical-section"))]
363+
#[cfg(feature = "std")]
364364
#[test]
365365
fn wait() {
366366
let cell: OnceCell<String> = OnceCell::new();
@@ -372,7 +372,7 @@ mod sync {
372372
.unwrap();
373373
}
374374

375-
#[cfg(not(feature = "critical-section"))]
375+
#[cfg(feature = "std")]
376376
#[test]
377377
fn get_or_init_stress() {
378378
let n_threads = if cfg!(miri) { 30 } else { 1_000 };
@@ -437,7 +437,7 @@ mod sync {
437437

438438
#[test]
439439
#[cfg_attr(miri, ignore)] // miri doesn't support processes
440-
#[cfg(not(feature = "critical-section"))]
440+
#[cfg(feature = "std")]
441441
fn reentrant_init() {
442442
let examples_dir = {
443443
let mut exe = std::env::current_exe().unwrap();
@@ -465,7 +465,7 @@ mod sync {
465465
}
466466
}
467467

468-
#[cfg(feature = "critical-section")]
468+
#[cfg(not(feature = "std"))]
469469
#[test]
470470
#[should_panic(expected = "reentrant init")]
471471
fn reentrant_init() {
@@ -658,7 +658,7 @@ mod sync {
658658
}
659659
}
660660

661-
#[cfg(not(feature = "critical-section"))]
661+
#[cfg(feature = "std")]
662662
#[test]
663663
fn get_does_not_block() {
664664
let cell = OnceCell::new();
@@ -692,7 +692,7 @@ mod sync {
692692

693693
#[cfg(feature = "race")]
694694
mod race {
695-
#[cfg(not(feature = "critical-section"))]
695+
#[cfg(feature = "std")]
696696
use std::sync::Barrier;
697697
use std::{
698698
num::NonZeroUsize,
@@ -748,7 +748,7 @@ mod race {
748748
assert_eq!(cell.get(), Some(val1));
749749
}
750750

751-
#[cfg(not(feature = "critical-section"))]
751+
#[cfg(feature = "std")]
752752
#[test]
753753
fn once_non_zero_usize_first_wins() {
754754
let val1 = NonZeroUsize::new(92).unwrap();
@@ -828,14 +828,14 @@ mod race {
828828

829829
#[cfg(all(feature = "race", feature = "alloc"))]
830830
mod race_once_box {
831-
#[cfg(not(feature = "critical-section"))]
831+
#[cfg(feature = "std")]
832832
use std::sync::Barrier;
833833
use std::sync::{
834834
atomic::{AtomicUsize, Ordering::SeqCst},
835835
Arc,
836836
};
837837

838-
#[cfg(not(feature = "critical-section"))]
838+
#[cfg(feature = "std")]
839839
use crossbeam_utils::thread::scope;
840840

841841
use once_cell::race::OnceBox;
@@ -867,7 +867,7 @@ mod race_once_box {
867867
}
868868
}
869869

870-
#[cfg(not(feature = "critical-section"))]
870+
#[cfg(feature = "std")]
871871
#[test]
872872
fn once_box_smoke_test() {
873873
let heap = Heap::default();
@@ -922,7 +922,7 @@ mod race_once_box {
922922
assert_eq!(heap.total(), 0);
923923
}
924924

925-
#[cfg(not(feature = "critical-section"))]
925+
#[cfg(feature = "std")]
926926
#[test]
927927
fn once_box_first_wins() {
928928
let cell = OnceBox::new();

‎xtask/src/main.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn main() -> xshell::Result<()> {
3434
cmd!(sh, "cargo test --no-default-features --features unstable,alloc --test it").run()?;
3535

3636
cmd!(sh, "cargo test --no-default-features --features critical-section").run()?;
37-
cmd!(sh, "cargo test --no-default-features --features critical-section --release").run()?;
37+
cmd!(sh, "cargo test --features critical-section").run()?;
3838
}
3939

4040
{

0 commit comments

Comments
 (0)