Skip to content

Commit

Permalink
Merge #141
Browse files Browse the repository at this point in the history
141: Fix race::OnceBox<T>: Default to not require T: Default r=matklad a=Arnavion



Co-authored-by: Arnavion <me@arnavion.dev>
  • Loading branch information
bors[bot] and Arnavion authored Mar 2, 2021
2 parents 979a68e + 9f97e64 commit 2a769dd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 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.7.1

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

## 1.7.0

- Hide the `race` module behind (default) `race` feature.
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.0"
version = "1.7.1"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand Down
8 changes: 7 additions & 1 deletion src/race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,18 @@ mod once_box {
use alloc::boxed::Box;

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

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

impl<T> Drop for OnceBox<T> {
fn drop(&mut self) {
let ptr = *self.inner.get_mut();
Expand Down
8 changes: 8 additions & 0 deletions tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,4 +859,12 @@ mod race_once_box {
});
assert_eq!(res, "hello");
}

#[test]
fn once_box_default() {
struct Foo;

let cell: OnceBox<Foo> = Default::default();
assert!(cell.get().is_none());
}
}
4 changes: 4 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ fn try_main() -> Result<()> {
cmd!("cargo test --features 'unstable std parking_lot' --no-default-features").run()?;
cmd!("cargo test --features 'unstable std parking_lot' --no-default-features --release")
.run()?;

cmd!("cargo test --features 'unstable alloc' --no-default-features --test it").run()?;
cmd!("cargo test --features 'unstable std parking_lot alloc' --no-default-features")
.run()?;
}

{
Expand Down

0 comments on commit 2a769dd

Please sign in to comment.