aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2022-03-08 22:03:10 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-03-08 22:03:10 +0000
commit46bec3e36f28d875767de49289a350f018b6bd4c (patch)
tree95353df6eca9bf63f57a86070e6fad69c6061276
parentb8a6632bf435b82f880bf4807e10221a2cd5e2f6 (diff)
parent3545eb44bb1a6b7dbd047aa56444bfd68167d762 (diff)
downloadonce_cell-android13-qpr3-s2-release.tar.gz
Merge "Update once_cell to 1.9.0" am: 9bb9cf9fd8 am: 5d595849b5 am: 3545eb44bbt_frc_odp_330442040t_frc_odp_330442000t_frc_ase_330444010android-13.0.0_r83android-13.0.0_r82android-13.0.0_r81android-13.0.0_r80android-13.0.0_r79android-13.0.0_r78android-13.0.0_r77android-13.0.0_r76android-13.0.0_r75android-13.0.0_r74android-13.0.0_r73android-13.0.0_r72android-13.0.0_r71android-13.0.0_r70android-13.0.0_r69android-13.0.0_r68android-13.0.0_r67android-13.0.0_r66android-13.0.0_r65android-13.0.0_r64android-13.0.0_r63android-13.0.0_r62android-13.0.0_r61android-13.0.0_r60android-13.0.0_r59android-13.0.0_r58android-13.0.0_r57android-13.0.0_r56android-13.0.0_r54android-13.0.0_r53android-13.0.0_r52android-13.0.0_r51android-13.0.0_r50android-13.0.0_r49android-13.0.0_r48android-13.0.0_r47android-13.0.0_r46android-13.0.0_r45android-13.0.0_r44android-13.0.0_r43android-13.0.0_r42android-13.0.0_r41android-13.0.0_r40android-13.0.0_r39android-13.0.0_r38android-13.0.0_r37android-13.0.0_r36android-13.0.0_r35android-13.0.0_r34android-13.0.0_r33android-13.0.0_r32aml_go_odp_330912000aml_go_ads_330915100aml_go_ads_330915000aml_go_ads_330913000android13-qpr3-s9-releaseandroid13-qpr3-s8-releaseandroid13-qpr3-s7-releaseandroid13-qpr3-s6-releaseandroid13-qpr3-s5-releaseandroid13-qpr3-s4-releaseandroid13-qpr3-s3-releaseandroid13-qpr3-s2-releaseandroid13-qpr3-s14-releaseandroid13-qpr3-s13-releaseandroid13-qpr3-s12-releaseandroid13-qpr3-s11-releaseandroid13-qpr3-s10-releaseandroid13-qpr3-s1-releaseandroid13-qpr3-releaseandroid13-qpr3-c-s8-releaseandroid13-qpr3-c-s7-releaseandroid13-qpr3-c-s6-releaseandroid13-qpr3-c-s5-releaseandroid13-qpr3-c-s4-releaseandroid13-qpr3-c-s3-releaseandroid13-qpr3-c-s2-releaseandroid13-qpr3-c-s12-releaseandroid13-qpr3-c-s11-releaseandroid13-qpr3-c-s10-releaseandroid13-qpr3-c-s1-releaseandroid13-qpr2-s9-releaseandroid13-qpr2-s8-releaseandroid13-qpr2-s7-releaseandroid13-qpr2-s6-releaseandroid13-qpr2-s5-releaseandroid13-qpr2-s3-releaseandroid13-qpr2-s2-releaseandroid13-qpr2-s12-releaseandroid13-qpr2-s11-releaseandroid13-qpr2-s10-releaseandroid13-qpr2-s1-releaseandroid13-qpr2-releaseandroid13-qpr2-b-s1-releaseandroid13-mainline-go-adservices-releaseandroid13-frc-odp-releaseandroid13-devandroid13-d4-s2-releaseandroid13-d4-s1-releaseandroid13-d4-releaseandroid13-d3-s1-release
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/once_cell/+/2004419 Change-Id: I4524f7bf49db11312c0ed589ff7e294c600a4219
-rw-r--r--.cargo_vcs_info.json7
-rw-r--r--Android.bp6
-rw-r--r--CHANGELOG.md4
-rw-r--r--Cargo.toml15
-rw-r--r--Cargo.toml.orig9
-rw-r--r--METADATA10
-rw-r--r--src/lib.rs52
-rw-r--r--src/race.rs25
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
diff --git a/Android.bp b/Android.bp
index 1823074..7211fe6 100644
--- a/Android.bp
+++ b/Android.bp
@@ -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.
diff --git a/Cargo.toml b/Cargo.toml
index 3129cb3..0a94bfc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/METADATA b/METADATA
index 23cfddf..4b16d85 100644
--- a/METADATA
+++ b/METADATA
@@ -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
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 8fd9132..cb8bfc7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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()