Skip to content

Commit

Permalink
Merge #165
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bors[bot] and ogoffart authored Dec 14, 2021
2 parents 7b2943b + 12ccfec commit 44852cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.9

- Added an `atomic-polyfill` optional dependency to compile `race` on platforms without atomics

## 1.8.0

- Add `try_insert` API -- a version of `set` that returns a reference.
Expand Down
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "once_cell"
version = "1.8.0"
version = "1.9.0"
authors = ["Aleksey Kladov <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand All @@ -24,6 +24,13 @@ members = ["xtask"]
# for up to 16 bytes smaller, depending on the size of the T.
parking_lot = { version = "0.11", optional = true, default_features = false }

# To be used in order to enable the race feature on targets
# that do not have atomics
# *Warning:* This can be unsound. Please read the README of
# [atomic-polyfill](https://github.com/embassy-rs/atomic-polyfill)
# and make sure you understand all the implications
atomic-polyfill = { version = "0.1", optional = true }

[dev-dependencies]
lazy_static = "1.0.0"
crossbeam-utils = "0.7.2"
Expand Down
25 changes: 15 additions & 10 deletions src/race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
//!
//! This module does not require `std` feature.

use core::{
num::NonZeroUsize,
sync::atomic::{AtomicUsize, Ordering},
};
#[cfg(feature = "atomic-polyfill")]
use atomic_polyfill as atomic;
#[cfg(not(feature = "atomic-polyfill"))]
use core::sync::atomic;

use atomic::{AtomicUsize, Ordering};
use core::num::NonZeroUsize;

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

#[cfg(feature = "alloc")]
mod once_box {
use core::{
marker::PhantomData,
ptr,
sync::atomic::{AtomicPtr, Ordering},
};
use super::atomic::{AtomicPtr, Ordering};
use core::{marker::PhantomData, ptr};

use alloc::boxed::Box;

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

impl<T> core::fmt::Debug for OnceBox<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "OnceBox({:?})", self.inner.load(Ordering::Relaxed))
}
}

impl<T> Default for OnceBox<T> {
fn default() -> Self {
Self::new()
Expand Down

0 comments on commit 44852cc

Please sign in to comment.