diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-04-06 20:37:00 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-04-06 20:37:00 +0000 |
commit | e065f9357f66f35cbc4430bbb64924d87c175628 (patch) | |
tree | 95353df6eca9bf63f57a86070e6fad69c6061276 | |
parent | b8a6632bf435b82f880bf4807e10221a2cd5e2f6 (diff) | |
parent | 46bec3e36f28d875767de49289a350f018b6bd4c (diff) | |
download | once_cell-android13-qpr1-release.tar.gz |
Snap for 8414339 from 46bec3e36f28d875767de49289a350f018b6bd4c to tm-qpr1-releaseandroid-13.0.0_r30android-13.0.0_r29android-13.0.0_r28android-13.0.0_r27android-13.0.0_r24android-13.0.0_r23android-13.0.0_r22android-13.0.0_r21android-13.0.0_r20android-13.0.0_r19android-13.0.0_r18android-13.0.0_r17android-13.0.0_r16android13-qpr1-s8-releaseandroid13-qpr1-s7-releaseandroid13-qpr1-s6-releaseandroid13-qpr1-s5-releaseandroid13-qpr1-s4-releaseandroid13-qpr1-s3-releaseandroid13-qpr1-s2-releaseandroid13-qpr1-s1-releaseandroid13-qpr1-release
Change-Id: I61e437b7ad81afdcebb644b499e879067d8336ad
-rw-r--r-- | .cargo_vcs_info.json | 7 | ||||
-rw-r--r-- | Android.bp | 6 | ||||
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | Cargo.toml | 15 | ||||
-rw-r--r-- | Cargo.toml.orig | 9 | ||||
-rw-r--r-- | METADATA | 10 | ||||
-rw-r--r-- | src/lib.rs | 52 | ||||
-rw-r--r-- | src/race.rs | 25 |
8 files changed, 98 insertions, 30 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index f73cbc1..f070bea 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,6 @@ { "git": { - "sha1": "8671ffc69cb78e345b7647802e5a5fd0c53f8c07" - } -} + "sha1": "44852cc72dbfbf57c5477a907ec0ab36527bc36b" + }, + "path_in_vcs": "" +}
\ No newline at end of file @@ -42,7 +42,7 @@ rust_library { host_supported: true, crate_name: "once_cell", cargo_env_compat: true, - cargo_pkg_version: "1.8.0", + cargo_pkg_version: "1.9.0", srcs: ["src/lib.rs"], edition: "2018", features: [ @@ -65,7 +65,7 @@ rust_test { host_supported: true, crate_name: "once_cell", cargo_env_compat: true, - cargo_pkg_version: "1.8.0", + cargo_pkg_version: "1.9.0", srcs: ["src/lib.rs"], test_suites: ["general-tests"], auto_gen_config: true, @@ -91,7 +91,7 @@ rust_test { host_supported: true, crate_name: "it", cargo_env_compat: true, - cargo_pkg_version: "1.8.0", + cargo_pkg_version: "1.9.0", srcs: ["tests/it.rs"], test_suites: ["general-tests"], auto_gen_config: true, diff --git a/CHANGELOG.md b/CHANGELOG.md index 34725e9..2c95053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -3,17 +3,16 @@ # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g., crates.io) dependencies +# to registry (e.g., crates.io) dependencies. # -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "once_cell" -version = "1.8.0" +version = "1.9.0" authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"] exclude = ["*.png", "*.svg", "/Cargo.lock.msrv", "/.travis.yml", "/run-miri-tests.sh", "rustfmt.toml"] description = "Single assignment cells and lazy values." @@ -53,6 +52,10 @@ required-features = ["std"] [[example]] name = "test_synchronization" required-features = ["std"] +[dependencies.atomic-polyfill] +version = "0.1" +optional = true + [dependencies.parking_lot] version = "0.11" optional = true diff --git a/Cargo.toml.orig b/Cargo.toml.orig index a9c1a5c..3b2d1aa 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "once_cell" -version = "1.8.0" +version = "1.9.0" authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"] license = "MIT OR Apache-2.0" edition = "2018" @@ -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" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/once_cell/once_cell-1.8.0.crate" + value: "https://static.crates.io/crates/once_cell/once_cell-1.9.0.crate" } - version: "1.8.0" + version: "1.9.0" license_type: NOTICE last_upgrade_date { - year: 2021 - month: 6 - day: 21 + year: 2022 + month: 3 + day: 1 } } @@ -441,6 +441,18 @@ pub mod unsync { /// Gets a mutable reference to the underlying value. /// /// Returns `None` if the cell is empty. + /// + /// This method is allowed to violate the invariant of writing to a `OnceCell` + /// at most once because it requires `&mut` access to `self`. As with all + /// interior mutability, `&mut` access permits arbitrary modification: + /// + /// ``` + /// use once_cell::unsync::OnceCell; + /// + /// let mut cell: OnceCell<u32> = OnceCell::new(); + /// cell.set(92).unwrap(); + /// cell = OnceCell::new(); + /// ``` pub fn get_mut(&mut self) -> Option<&mut T> { // Safe because we have unique access unsafe { &mut *self.inner.get() }.as_mut() @@ -590,6 +602,18 @@ pub mod unsync { /// assert_eq!(cell.take(), Some("hello".to_string())); /// assert_eq!(cell.get(), None); /// ``` + /// + /// This method is allowed to violate the invariant of writing to a `OnceCell` + /// at most once because it requires `&mut` access to `self`. As with all + /// interior mutability, `&mut` access permits arbitrary modification: + /// + /// ``` + /// use once_cell::unsync::OnceCell; + /// + /// let mut cell: OnceCell<u32> = OnceCell::new(); + /// cell.set(92).unwrap(); + /// cell = OnceCell::new(); + /// ``` pub fn take(&mut self) -> Option<T> { mem::replace(self, Self::default()).into_inner() } @@ -737,7 +761,7 @@ pub mod sync { panic::RefUnwindSafe, }; - use crate::imp::OnceCell as Imp; + use crate::{imp::OnceCell as Imp, take_unchecked}; /// A thread-safe cell which can be written to only once. /// @@ -835,6 +859,18 @@ pub mod sync { /// Gets the mutable reference to the underlying value. /// /// Returns `None` if the cell is empty. + /// + /// This method is allowed to violate the invariant of writing to a `OnceCell` + /// at most once because it requires `&mut` access to `self`. As with all + /// interior mutability, `&mut` access permits arbitrary modification: + /// + /// ``` + /// use once_cell::sync::OnceCell; + /// + /// let mut cell: OnceCell<u32> = OnceCell::new(); + /// cell.set(92).unwrap(); + /// cell = OnceCell::new(); + /// ``` pub fn get_mut(&mut self) -> Option<&mut T> { self.0.get_mut() } @@ -897,7 +933,7 @@ pub mod sync { /// ``` pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)> { let mut value = Some(value); - let res = self.get_or_init(|| value.take().unwrap()); + let res = self.get_or_init(|| unsafe { take_unchecked(&mut value) }); match value { None => Ok(res), Some(value) => Err((res, value)), @@ -999,6 +1035,18 @@ pub mod sync { /// assert_eq!(cell.take(), Some("hello".to_string())); /// assert_eq!(cell.get(), None); /// ``` + /// + /// This method is allowed to violate the invariant of writing to a `OnceCell` + /// at most once because it requires `&mut` access to `self`. As with all + /// interior mutability, `&mut` access permits arbitrary modification: + /// + /// ``` + /// use once_cell::sync::OnceCell; + /// + /// let mut cell: OnceCell<u32> = OnceCell::new(); + /// cell.set(92).unwrap(); + /// cell = OnceCell::new(); + /// ``` pub fn take(&mut self) -> Option<T> { mem::replace(self, Self::default()).into_inner() } diff --git a/src/race.rs b/src/race.rs index e1f5cba..3576420 100644 --- a/src/race.rs +++ b/src/race.rs @@ -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)] @@ -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() |