From e32d6eefb7233e0a4077cf6eb7a7c2f28513c6b6 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Mon, 21 Jun 2021 13:35:24 -0700 Subject: Upgrade rust/crates/once_cell to 1.8.0 Test: make Change-Id: I5adb9aa89db970ff857566c9372bf5d498b09077 --- .cargo_vcs_info.json | 2 +- CHANGELOG.md | 4 ++ Cargo.toml | 4 +- Cargo.toml.orig | 4 +- METADATA | 8 +-- TEST_MAPPING | 6 +++ src/lib.rs | 134 +++++++++++++++++++++++++++++++++++++++++++-------- src/race.rs | 2 +- 8 files changed, 133 insertions(+), 31 deletions(-) diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 5b3900a..f73cbc1 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "6286eac17124140813e01342642f4fc060664991" + "sha1": "8671ffc69cb78e345b7647802e5a5fd0c53f8c07" } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 53fdf64..34725e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.8.0 + +- Add `try_insert` API -- a version of `set` that returns a reference. + ## 1.7.2 - Improve code size when using parking_lot feature. diff --git a/Cargo.toml b/Cargo.toml index 0d3797b..3129cb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,9 @@ [package] edition = "2018" name = "once_cell" -version = "1.7.2" +version = "1.8.0" authors = ["Aleksey Kladov "] -exclude = ["*.png", "*.svg", "/Cargo.lock.min", "/.travis.yml", "/run-miri-tests.sh", "rustfmt.toml"] +exclude = ["*.png", "*.svg", "/Cargo.lock.msrv", "/.travis.yml", "/run-miri-tests.sh", "rustfmt.toml"] description = "Single assignment cells and lazy values." documentation = "https://docs.rs/once_cell" readme = "README.md" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 259e3b0..a9c1a5c 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "once_cell" -version = "1.7.2" +version = "1.8.0" authors = ["Aleksey Kladov "] license = "MIT OR Apache-2.0" edition = "2018" @@ -13,7 +13,7 @@ repository = "https://github.com/matklad/once_cell" keywords = ["lazy", "static"] categories = ["rust-patterns", "memory-management"] -exclude = ["*.png", "*.svg", "/Cargo.lock.min", "/.travis.yml", "/run-miri-tests.sh", "rustfmt.toml"] +exclude = ["*.png", "*.svg", "/Cargo.lock.msrv", "/.travis.yml", "/run-miri-tests.sh", "rustfmt.toml"] [workspace] members = ["xtask"] diff --git a/METADATA b/METADATA index 3c1b227..23cfddf 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.7.2.crate" + value: "https://static.crates.io/crates/once_cell/once_cell-1.8.0.crate" } - version: "1.7.2" + version: "1.8.0" license_type: NOTICE last_upgrade_date { year: 2021 - month: 4 - day: 1 + month: 6 + day: 21 } } diff --git a/TEST_MAPPING b/TEST_MAPPING index a4df736..31babf5 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -4,6 +4,9 @@ { "name": "doh_unit_test" }, + { + "name": "keystore2_test" + }, { "name": "quiche_device_test_src_lib" }, @@ -61,6 +64,9 @@ }, { "name": "thread_local_device_test_src_lib" + }, + { + "name": "vpnprofilestore_test" } ] } diff --git a/src/lib.rs b/src/lib.rs index a455cb4..8fd9132 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! //! ```rust,ignore //! impl OnceCell { -//! fn new() -> OnceCell { ... } +//! const fn new() -> OnceCell { ... } //! fn set(&self, value: T) -> Result<(), T> { ... } //! fn get(&self) -> Option<&T> { ... } //! } @@ -24,11 +24,11 @@ //! [`Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html //! [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html //! -//! # Patterns +//! # Recipes //! //! `OnceCell` might be useful for a variety of patterns. //! -//! ## Safe Initialization of global data +//! ## Safe Initialization of Global Data //! //! ```rust //! use std::{env, io}; @@ -59,7 +59,7 @@ //! } //! ``` //! -//! ## Lazy initialized global data +//! ## Lazy Initialized Global Data //! //! This is essentially the `lazy_static!` macro, but without a macro. //! @@ -97,6 +97,9 @@ //! } //! ``` //! +//! Note that the variable that holds `Lazy` is declared as `static`, *not* +//! `const`. This is important: using `const` instead compiles, but works wrong. +//! //! [`sync::Lazy`]: sync/struct.Lazy.html //! [`unsync::Lazy`]: unsync/struct.Lazy.html //! @@ -139,10 +142,9 @@ //! } //! ``` //! -//! ## Building block +//! ## Lazily Compiled Regex //! -//! Naturally, it is possible to build other abstractions on top of `OnceCell`. -//! For example, this is a `regex!` macro which takes a string literal and returns an +//! This is a `regex!` macro which takes a string literal and returns an //! *expression* that evaluates to a `&'static Regex`: //! //! ``` @@ -156,7 +158,51 @@ //! //! This macro can be useful to avoid the "compile regex on every loop iteration" problem. //! -//! Another pattern would be a `LateInit` type for delayed initialization: +//! ## Runtime `include_bytes!` +//! +//! The `include_bytes` macro is useful to include test resources, but it slows +//! down test compilation a lot. An alternative is to load the resources at +//! runtime: +//! +//! ``` +//! use std::path::Path; +//! +//! use once_cell::sync::OnceCell; +//! +//! pub struct TestResource { +//! path: &'static str, +//! cell: OnceCell>, +//! } +//! +//! impl TestResource { +//! pub const fn new(path: &'static str) -> TestResource { +//! TestResource { path, cell: OnceCell::new() } +//! } +//! pub fn bytes(&self) -> &[u8] { +//! self.cell.get_or_init(|| { +//! let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); +//! let path = Path::new(dir.as_str()).join(self.path); +//! std::fs::read(&path).unwrap_or_else(|_err| { +//! panic!("failed to load test resource: {}", path.display()) +//! }) +//! }).as_slice() +//! } +//! } +//! +//! static TEST_IMAGE: TestResource = TestResource::new("test_data/lena.png"); +//! +//! #[test] +//! fn test_sobel_filter() { +//! let rgb: &[u8] = TEST_IMAGE.bytes(); +//! // ... +//! # drop(rgb); +//! } +//! ``` +//! +//! ## `lateinit` +//! +//! `LateInit` type for delayed initialization. It is reminiscent of Kotlin's +//! `lateinit` keyword and allows construction of cyclic data structures: //! //! //! ``` @@ -291,10 +337,11 @@ mod imp; #[path = "imp_std.rs"] mod imp; +/// Single-threaded version of `OnceCell`. pub mod unsync { use core::{ cell::{Cell, UnsafeCell}, - fmt, mem, + fmt, hint, mem, ops::{Deref, DerefMut}, }; @@ -417,9 +464,29 @@ pub mod unsync { /// assert!(cell.get().is_some()); /// ``` pub fn set(&self, value: T) -> Result<(), T> { - let slot = unsafe { &*self.inner.get() }; - if slot.is_some() { - return Err(value); + match self.try_insert(value) { + Ok(_) => Ok(()), + Err((_, value)) => Err(value), + } + } + + /// Like [`set`](Self::set), but also returns a referce to the final cell value. + /// + /// # Example + /// ``` + /// use once_cell::unsync::OnceCell; + /// + /// let cell = OnceCell::new(); + /// assert!(cell.get().is_none()); + /// + /// assert_eq!(cell.try_insert(92), Ok(&92)); + /// assert_eq!(cell.try_insert(62), Err((&92, 62))); + /// + /// assert!(cell.get().is_some()); + /// ``` + pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)> { + if let Some(old) = self.get() { + return Err((old, value)); } let slot = unsafe { &mut *self.inner.get() }; // This is the only place where we set the slot, no races @@ -427,7 +494,10 @@ pub mod unsync { // checked that slot is currently `None`, so this write // maintains the `inner`'s invariant. *slot = Some(value); - Ok(()) + Ok(match &*slot { + Some(value) => value, + None => unsafe { hint::unreachable_unchecked() }, + }) } /// Gets the contents of the cell, initializing it with `f` @@ -657,6 +727,7 @@ pub mod unsync { } } +/// Thread-safe, blocking version of `OnceCell`. #[cfg(feature = "std")] pub mod sync { use std::{ @@ -803,11 +874,33 @@ pub mod sync { /// } /// ``` pub fn set(&self, value: T) -> Result<(), T> { + match self.try_insert(value) { + Ok(_) => Ok(()), + Err((_, value)) => Err(value), + } + } + + /// Like [`set`](Self::set), but also returns a reference to the final cell value. + /// + /// # Example + /// + /// ``` + /// use once_cell::unsync::OnceCell; + /// + /// let cell = OnceCell::new(); + /// assert!(cell.get().is_none()); + /// + /// assert_eq!(cell.try_insert(92), Ok(&92)); + /// assert_eq!(cell.try_insert(62), Err((&92, 62))); + /// + /// assert!(cell.get().is_some()); + /// ``` + pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)> { let mut value = Some(value); - self.get_or_init(|| value.take().unwrap()); + let res = self.get_or_init(|| value.take().unwrap()); match value { - None => Ok(()), - Some(value) => Err(value), + None => Ok(res), + Some(value) => Err((res, value)), } } @@ -974,11 +1067,10 @@ pub mod sync { } } - // We never create a `&F` from a `&Lazy` so it is fine - // to not impl `Sync` for `F` - // we do create a `&mut Option` in `force`, but this is - // properly synchronized, so it only happens once - // so it also does not contribute to this impl. + // We never create a `&F` from a `&Lazy` so it is fine to not impl + // `Sync` for `F`. we do create a `&mut Option` in `force`, but this is + // properly synchronized, so it only happens once so it also does not + // contribute to this impl. unsafe impl Sync for Lazy where OnceCell: Sync {} // auto-derived `Send` impl is OK. diff --git a/src/race.rs b/src/race.rs index e0dc8fc..e1f5cba 100644 --- a/src/race.rs +++ b/src/race.rs @@ -1,4 +1,4 @@ -//! "First one wins" flavor of `OnceCell`. +//! Thread-safe, non-blocking, "first one wins" flavor of `OnceCell`. //! //! If two threads race to initialize a type from the `race` module, they //! don't block, execute initialization function together, but only one of -- cgit v1.2.3 From 9c624cb66e280f388e2384cbac6f840526ee6358 Mon Sep 17 00:00:00 2001 From: Alan Stokes Date: Tue, 22 Jun 2021 11:22:29 +0100 Subject: Allow CompOS APEX to access Ring. Bug: 191763370 Test: Client code (in progress) builds. Change-Id: Ibebe476512f66f479ef058ce09cdf733b2dd0617 --- Android.bp | 1 + cargo2android.json | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Android.bp b/Android.bp index 8c67ad3..61ba14a 100644 --- a/Android.bp +++ b/Android.bp @@ -51,6 +51,7 @@ rust_library { ], apex_available: [ "//apex_available:platform", + "com.android.compos", "com.android.resolv", "com.android.virt", ], diff --git a/cargo2android.json b/cargo2android.json index 2e8c0ea..4a1ad88 100644 --- a/cargo2android.json +++ b/cargo2android.json @@ -1,6 +1,7 @@ { "apex-available": [ "//apex_available:platform", + "com.android.compos", "com.android.resolv", "com.android.virt" ], @@ -8,4 +9,4 @@ "device": true, "min-sdk-version": "29", "run": true -} \ No newline at end of file +} -- cgit v1.2.3 From 5566498e5bd91c242f043a4109a783a44e131b1b Mon Sep 17 00:00:00 2001 From: easoncylee Date: Fri, 2 Jul 2021 11:27:58 +0800 Subject: Update vpnprofilestore_test to legacykeystore_test in TEST_MAPPING The test had been changed to legacykeystore_test from ag/14968529 Bug: 192796637 Test: presubmit Change-Id: I2c790feeb511a6e1db89c9b479bc877ae3328047 --- TEST_MAPPING | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TEST_MAPPING b/TEST_MAPPING index 31babf5..898c7cb 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -66,7 +66,7 @@ "name": "thread_local_device_test_src_lib" }, { - "name": "vpnprofilestore_test" + "name": "legacykeystore_test" } ] } -- cgit v1.2.3 From 84f337f0551a74d8c1bdf4ad298fc7b2399d8028 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Mon, 23 Aug 2021 09:58:48 -0700 Subject: Update TEST_MAPPING Test: None Change-Id: Ic8c0b1da020dc98045168d37fcb530351e63ab36 --- TEST_MAPPING | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/TEST_MAPPING b/TEST_MAPPING index 898c7cb..1e2fb88 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,12 +1,36 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { "presubmit": [ + { + "name": "ZipFuseTest" + }, + { + "name": "apkdmverity.test" + }, + { + "name": "authfs_device_test_src_lib" + }, { "name": "doh_unit_test" }, { "name": "keystore2_test" }, + { + "name": "legacykeystore_test" + }, + { + "name": "libapkverify.integration_test" + }, + { + "name": "libapkverify.test" + }, + { + "name": "libidsig.test" + }, + { + "name": "microdroid_manager_test" + }, { "name": "quiche_device_test_src_lib" }, @@ -66,7 +90,16 @@ "name": "thread_local_device_test_src_lib" }, { - "name": "legacykeystore_test" + "name": "virtualizationservice_device_test" + }, + { + "name": "webpki_device_test_src_lib" + }, + { + "name": "webpki_device_test_tests_dns_name_tests" + }, + { + "name": "webpki_device_test_tests_integration" } ] } -- cgit v1.2.3 From 467ba80bf0cff36531606f4db606be6088291f07 Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Wed, 20 Oct 2021 14:59:29 +0200 Subject: Enable tests Disable tests for Android that require catching an unwind on panic because Android does abort on panic. Test: atest x86_64 once_cell_host_test_src_lib: Passed: 5, Failed: 0, Ignored: 0, Assumption Failed: 0, x86_64 once_cell_host_test_tests_it: Passed: 50, Failed: 0, Ignored: 1, Assumption Failed: 0, x86 once_cell_device_test_src_lib: Passed: 2, Failed: 0, Ignored: 0, Assumption Failed: 0, x86 once_cell_device_test_tests_it: Passed: 47, Failed: 0, Ignored: 1, Assumption Failed: 0, Change-Id: I8eab3200f8d9d2e56d5cb130c550e522ca4ba8fb --- Android.bp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ TEST_MAPPING | 17 +++-------- cargo2android.json | 3 +- patches/imp_std.rs.patch | 20 +++++++++++++ patches/it.rs.patch | 36 ++++++++++++++++++++++++ src/imp_std.rs | 2 ++ tests/it.rs | 4 +++ 7 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 patches/imp_std.rs.patch create mode 100644 patches/it.rs.patch diff --git a/Android.bp b/Android.bp index 61ba14a..9073812 100644 --- a/Android.bp +++ b/Android.bp @@ -41,6 +41,8 @@ rust_library { name: "libonce_cell", host_supported: true, crate_name: "once_cell", + cargo_env_compat: true, + cargo_pkg_version: "1.8.0", srcs: ["src/lib.rs"], edition: "2018", features: [ @@ -57,3 +59,74 @@ rust_library { ], min_sdk_version: "29", } + +rust_defaults { + name: "once_cell_test_defaults", + crate_name: "once_cell", + srcs: ["src/lib.rs"], + cargo_env_compat: true, + cargo_pkg_version: "1.8.0", + test_suites: ["general-tests"], + auto_gen_config: true, + edition: "2018", + features: [ + "alloc", + "default", + "race", + "std", + ], + rustlibs: [ + "libcrossbeam_utils", + "liblazy_static", + "libregex", + ], +} + +rust_test_host { + name: "once_cell_host_test_src_lib", + defaults: ["once_cell_test_defaults"], + test_options: { + unit_test: true, + }, +} + +rust_test { + name: "once_cell_device_test_src_lib", + defaults: ["once_cell_test_defaults"], +} + +rust_defaults { + name: "once_cell_test_defaults_it", + crate_name: "it", + srcs: ["tests/it.rs"], + cargo_env_compat: true, + cargo_pkg_version: "1.8.0", + test_suites: ["general-tests"], + auto_gen_config: true, + edition: "2018", + features: [ + "alloc", + "default", + "race", + "std", + ], + rustlibs: [ + "libcrossbeam_utils", + "liblazy_static", + "libonce_cell", + "libregex", + ], +} + +rust_test_host { + name: "once_cell_host_test_tests_it", + defaults: ["once_cell_test_defaults_it"], + test_options: { + unit_test: true, + }, +} + +rust_test { + name: "once_cell_device_test_tests_it", + defaults: ["once_cell_test_defaults_it"], +} diff --git a/TEST_MAPPING b/TEST_MAPPING index 1e2fb88..8f658a8 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,15 +1,6 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { "presubmit": [ - { - "name": "ZipFuseTest" - }, - { - "name": "apkdmverity.test" - }, - { - "name": "authfs_device_test_src_lib" - }, { "name": "doh_unit_test" }, @@ -29,7 +20,10 @@ "name": "libidsig.test" }, { - "name": "microdroid_manager_test" + "name": "once_cell_device_test_src_lib" + }, + { + "name": "once_cell_device_test_tests_it" }, { "name": "quiche_device_test_src_lib" @@ -89,9 +83,6 @@ { "name": "thread_local_device_test_src_lib" }, - { - "name": "virtualizationservice_device_test" - }, { "name": "webpki_device_test_src_lib" }, diff --git a/cargo2android.json b/cargo2android.json index 4a1ad88..646ac59 100644 --- a/cargo2android.json +++ b/cargo2android.json @@ -8,5 +8,6 @@ "dependencies": true, "device": true, "min-sdk-version": "29", - "run": true + "run": true, + "tests": true } diff --git a/patches/imp_std.rs.patch b/patches/imp_std.rs.patch new file mode 100644 index 0000000..556297f --- /dev/null +++ b/patches/imp_std.rs.patch @@ -0,0 +1,20 @@ +diff --git a/src/imp_std.rs b/src/imp_std.rs +index d7dda96..f461c3d 100644 +--- a/src/imp_std.rs ++++ b/src/imp_std.rs +@@ -299,6 +299,7 @@ mod tests { + } + + #[test] ++ #[cfg(not(target_os = "android"))] + fn poison_bad() { + static O: OnceCell<()> = OnceCell::new(); + +@@ -320,6 +321,7 @@ mod tests { + } + + #[test] ++ #[cfg(not(target_os = "android"))] + fn wait_for_force_to_finish() { + static O: OnceCell<()> = OnceCell::new(); + diff --git a/patches/it.rs.patch b/patches/it.rs.patch new file mode 100644 index 0000000..8491db5 --- /dev/null +++ b/patches/it.rs.patch @@ -0,0 +1,36 @@ +diff --git a/tests/it.rs b/tests/it.rs +index 81faaff..c769487 100644 +--- a/tests/it.rs ++++ b/tests/it.rs +@@ -166,6 +166,7 @@ mod unsync { + + #[test] + #[cfg(feature = "std")] ++ #[cfg(not(target_os = "android"))] + fn lazy_poisoning() { + let x: Lazy = Lazy::new(|| panic!("kaboom")); + for _ in 0..2 { +@@ -288,6 +289,7 @@ mod sync { + } + + #[test] ++ #[cfg(not(target_os = "android"))] + fn get_or_try_init() { + let cell: OnceCell = OnceCell::new(); + assert!(cell.get().is_none()); +@@ -348,6 +350,7 @@ mod sync { + + #[test] + #[cfg_attr(miri, ignore)] // miri doesn't support processes ++ #[ignore = "Android: ignore for now. Need to compile these binaries separately."] + fn reentrant_init() { + let examples_dir = { + let mut exe = std::env::current_exe().unwrap(); +@@ -486,6 +489,7 @@ mod sync { + } + + #[test] ++ #[cfg(not(target_os = "android"))] + fn lazy_poisoning() { + let x: Lazy = Lazy::new(|| panic!("kaboom")); + for _ in 0..2 { diff --git a/src/imp_std.rs b/src/imp_std.rs index d7dda96..f461c3d 100644 --- a/src/imp_std.rs +++ b/src/imp_std.rs @@ -299,6 +299,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "android"))] fn poison_bad() { static O: OnceCell<()> = OnceCell::new(); @@ -320,6 +321,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "android"))] fn wait_for_force_to_finish() { static O: OnceCell<()> = OnceCell::new(); diff --git a/tests/it.rs b/tests/it.rs index 81faaff..c769487 100644 --- a/tests/it.rs +++ b/tests/it.rs @@ -166,6 +166,7 @@ mod unsync { #[test] #[cfg(feature = "std")] + #[cfg(not(target_os = "android"))] fn lazy_poisoning() { let x: Lazy = Lazy::new(|| panic!("kaboom")); for _ in 0..2 { @@ -288,6 +289,7 @@ mod sync { } #[test] + #[cfg(not(target_os = "android"))] fn get_or_try_init() { let cell: OnceCell = OnceCell::new(); assert!(cell.get().is_none()); @@ -348,6 +350,7 @@ mod sync { #[test] #[cfg_attr(miri, ignore)] // miri doesn't support processes + #[ignore = "Android: ignore for now. Need to compile these binaries separately."] fn reentrant_init() { let examples_dir = { let mut exe = std::env::current_exe().unwrap(); @@ -486,6 +489,7 @@ mod sync { } #[test] + #[cfg(not(target_os = "android"))] fn lazy_poisoning() { let x: Lazy = Lazy::new(|| panic!("kaboom")); for _ in 0..2 { -- cgit v1.2.3 From aeca3d82eca7e56bd79176dab894ddc7584f1848 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Mon, 29 Nov 2021 14:04:33 -0800 Subject: Refresh Android.bp, cargo2android.json, TEST_MAPPING. Test: None Change-Id: I0e3a181857090de51539b8ba68160c8e144982b1 --- Android.bp | 44 +++++++++--------------------- TEST_MAPPING | 79 ++++++++++++++++++++++++++++-------------------------- cargo2android.json | 2 +- 3 files changed, 55 insertions(+), 70 deletions(-) diff --git a/Android.bp b/Android.bp index 9073812..1823074 100644 --- a/Android.bp +++ b/Android.bp @@ -60,14 +60,18 @@ rust_library { min_sdk_version: "29", } -rust_defaults { - name: "once_cell_test_defaults", +rust_test { + name: "once_cell_test_src_lib", + host_supported: true, crate_name: "once_cell", - srcs: ["src/lib.rs"], cargo_env_compat: true, cargo_pkg_version: "1.8.0", + srcs: ["src/lib.rs"], test_suites: ["general-tests"], auto_gen_config: true, + test_options: { + unit_test: true, + }, edition: "2018", features: [ "alloc", @@ -82,27 +86,18 @@ rust_defaults { ], } -rust_test_host { - name: "once_cell_host_test_src_lib", - defaults: ["once_cell_test_defaults"], - test_options: { - unit_test: true, - }, -} - rust_test { - name: "once_cell_device_test_src_lib", - defaults: ["once_cell_test_defaults"], -} - -rust_defaults { - name: "once_cell_test_defaults_it", + name: "once_cell_test_tests_it", + host_supported: true, crate_name: "it", - srcs: ["tests/it.rs"], cargo_env_compat: true, cargo_pkg_version: "1.8.0", + srcs: ["tests/it.rs"], test_suites: ["general-tests"], auto_gen_config: true, + test_options: { + unit_test: true, + }, edition: "2018", features: [ "alloc", @@ -117,16 +112,3 @@ rust_defaults { "libregex", ], } - -rust_test_host { - name: "once_cell_host_test_tests_it", - defaults: ["once_cell_test_defaults_it"], - test_options: { - unit_test: true, - }, -} - -rust_test { - name: "once_cell_device_test_tests_it", - defaults: ["once_cell_test_defaults_it"], -} diff --git a/TEST_MAPPING b/TEST_MAPPING index 8f658a8..febfdce 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,96 +1,99 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { - "presubmit": [ + "imports": [ { - "name": "doh_unit_test" + "path": "external/rust/crates/quiche" }, { - "name": "keystore2_test" + "path": "external/rust/crates/ring" }, { - "name": "legacykeystore_test" + "path": "external/rust/crates/thread_local" }, { - "name": "libapkverify.integration_test" + "path": "external/rust/crates/webpki" + } + ], + "presubmit": [ + { + "name": "ZipFuseTest" }, { - "name": "libapkverify.test" + "name": "apkdmverity.test" }, { - "name": "libidsig.test" + "name": "authfs_device_test_src_lib" + }, + { + "name": "doh_unit_test" }, { - "name": "once_cell_device_test_src_lib" + "name": "keystore2_test" }, { - "name": "once_cell_device_test_tests_it" + "name": "legacykeystore_test" }, { - "name": "quiche_device_test_src_lib" + "name": "libapkverify.integration_test" }, { - "name": "ring_device_test_src_lib", - "options": [ - { - "test-timeout": "100000" - } - ] + "name": "libapkverify.test" }, { - "name": "ring_device_test_tests_aead_tests" + "name": "libidsig.test" }, { - "name": "ring_device_test_tests_agreement_tests" + "name": "microdroid_manager_test" }, { - "name": "ring_device_test_tests_constant_time_tests" + "name": "once_cell_test_src_lib" }, { - "name": "ring_device_test_tests_digest_tests", - "options": [ - { - "test-timeout": "600000" - } - ] + "name": "once_cell_test_tests_it" }, { - "name": "ring_device_test_tests_ecdsa_tests" + "name": "virtualizationservice_device_test" + } + ], + "presubmit-rust": [ + { + "name": "ZipFuseTest" }, { - "name": "ring_device_test_tests_ed25519_tests" + "name": "apkdmverity.test" }, { - "name": "ring_device_test_tests_hkdf_tests" + "name": "authfs_device_test_src_lib" }, { - "name": "ring_device_test_tests_hmac_tests" + "name": "doh_unit_test" }, { - "name": "ring_device_test_tests_pbkdf2_tests" + "name": "keystore2_test" }, { - "name": "ring_device_test_tests_quic_tests" + "name": "legacykeystore_test" }, { - "name": "ring_device_test_tests_rand_tests" + "name": "libapkverify.integration_test" }, { - "name": "ring_device_test_tests_rsa_tests" + "name": "libapkverify.test" }, { - "name": "ring_device_test_tests_signature_tests" + "name": "libidsig.test" }, { - "name": "thread_local_device_test_src_lib" + "name": "microdroid_manager_test" }, { - "name": "webpki_device_test_src_lib" + "name": "once_cell_test_src_lib" }, { - "name": "webpki_device_test_tests_dns_name_tests" + "name": "once_cell_test_tests_it" }, { - "name": "webpki_device_test_tests_integration" + "name": "virtualizationservice_device_test" } ] } diff --git a/cargo2android.json b/cargo2android.json index 646ac59..506d866 100644 --- a/cargo2android.json +++ b/cargo2android.json @@ -10,4 +10,4 @@ "min-sdk-version": "29", "run": true, "tests": true -} +} \ No newline at end of file -- cgit v1.2.3 From 533bf1317cfb4320a512a8dd06cc508fb596505c Mon Sep 17 00:00:00 2001 From: David LeGare Date: Wed, 2 Mar 2022 16:21:26 +0000 Subject: Update once_cell to 1.9.0 Test: cd external/rust/crates && atest --host -c Change-Id: Iddd1b8516f9187f948c45695a889ed6b987566ca --- .cargo_vcs_info.json | 7 ++++--- Android.bp | 6 +++--- CHANGELOG.md | 4 ++++ Cargo.toml | 15 +++++++++------ Cargo.toml.orig | 9 ++++++++- METADATA | 10 +++++----- src/lib.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 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 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 "] 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 "] 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 = 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 = OnceCell::new(); + /// cell.set(92).unwrap(); + /// cell = OnceCell::new(); + /// ``` pub fn take(&mut self) -> Option { 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 = 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 = OnceCell::new(); + /// cell.set(92).unwrap(); + /// cell = OnceCell::new(); + /// ``` pub fn take(&mut self) -> Option { 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 { inner: AtomicPtr, ghost: PhantomData>>, } + impl core::fmt::Debug for OnceBox { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "OnceBox({:?})", self.inner.load(Ordering::Relaxed)) + } + } + impl Default for OnceBox { fn default() -> Self { Self::new() -- cgit v1.2.3