Skip to content

Commit 44852cc

Browse files
bors[bot]ogoffart
andauthored
Merge #165
165: Add an atomic-polyfill optional dependancy r=matklad a=ogoffart To make it compile on platform that have limited atomics Fixes #155 Note that this re-implement the Debug version of the OnceBox because atomic-polyfill::AtomicPtr don't implement Debug (embassy-rs/atomic-polyfill#11) Co-authored-by: Olivier Goffart <ogoffart@sixtyfps.io>
2 parents 7b2943b + 12ccfec commit 44852cc

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.9
4+
5+
- Added an `atomic-polyfill` optional dependency to compile `race` on platforms without atomics
6+
37
## 1.8.0
48

59
- Add `try_insert` API -- a version of `set` that returns a reference.

Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "once_cell"
3-
version = "1.8.0"
3+
version = "1.9.0"
44
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
55
license = "MIT OR Apache-2.0"
66
edition = "2018"
@@ -24,6 +24,13 @@ members = ["xtask"]
2424
# for up to 16 bytes smaller, depending on the size of the T.
2525
parking_lot = { version = "0.11", optional = true, default_features = false }
2626

27+
# To be used in order to enable the race feature on targets
28+
# that do not have atomics
29+
# *Warning:* This can be unsound. Please read the README of
30+
# [atomic-polyfill](https://github.com/embassy-rs/atomic-polyfill)
31+
# and make sure you understand all the implications
32+
atomic-polyfill = { version = "0.1", optional = true }
33+
2734
[dev-dependencies]
2835
lazy_static = "1.0.0"
2936
crossbeam-utils = "0.7.2"

src/race.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
//!
77
//! This module does not require `std` feature.
88
9-
use core::{
10-
num::NonZeroUsize,
11-
sync::atomic::{AtomicUsize, Ordering},
12-
};
9+
#[cfg(feature = "atomic-polyfill")]
10+
use atomic_polyfill as atomic;
11+
#[cfg(not(feature = "atomic-polyfill"))]
12+
use core::sync::atomic;
13+
14+
use atomic::{AtomicUsize, Ordering};
15+
use core::num::NonZeroUsize;
1316

1417
/// A thread-safe cell which can be written to only once.
1518
#[derive(Default, Debug)]
@@ -160,21 +163,23 @@ pub use self::once_box::OnceBox;
160163

161164
#[cfg(feature = "alloc")]
162165
mod once_box {
163-
use core::{
164-
marker::PhantomData,
165-
ptr,
166-
sync::atomic::{AtomicPtr, Ordering},
167-
};
166+
use super::atomic::{AtomicPtr, Ordering};
167+
use core::{marker::PhantomData, ptr};
168168

169169
use alloc::boxed::Box;
170170

171171
/// A thread-safe cell which can be written to only once.
172-
#[derive(Debug)]
173172
pub struct OnceBox<T> {
174173
inner: AtomicPtr<T>,
175174
ghost: PhantomData<Option<Box<T>>>,
176175
}
177176

177+
impl<T> core::fmt::Debug for OnceBox<T> {
178+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
179+
write!(f, "OnceBox({:?})", self.inner.load(Ordering::Relaxed))
180+
}
181+
}
182+
178183
impl<T> Default for OnceBox<T> {
179184
fn default() -> Self {
180185
Self::new()

0 commit comments

Comments
 (0)