Merge "Update once_cell to 1.9.0" am: 9bb9cf9fd8 am: 5d595849b5 am: 3545eb44bb

Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/once_cell/+/2004419

Change-Id: I4524f7bf49db11312c0ed589ff7e294c600a4219
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 @@
     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 @@
     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 @@
     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 @@
 [[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 @@
 # 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 @@
   }
   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 @@
         /// 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 @@
         /// 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 @@
         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 @@
         /// 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 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 @@
         /// 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 @@
 
 #[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()