From 9b675bea75ecaded550c274361e649d7064f06e9 Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Mon, 19 Dec 2022 10:27:20 +0100 Subject: Upgrade slab to 0.4.7 This project was upgraded with external_updater. Usage: tools/external_updater/updater.sh update rust/crates/slab For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md Test: TreeHugger Change-Id: I2a05eaa50dfb3ee4d9c3d243c1c26e99614e46ce --- .cargo_vcs_info.json | 7 ++++--- Android.bp | 5 +++-- CHANGELOG.md | 16 +++++++++++--- Cargo.toml | 25 +++++++++++++++++----- Cargo.toml.orig | 10 +++++---- METADATA | 12 +++++++---- build.rs | 24 +++++++++++++++++++++ src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++---- tests/slab.rs | 6 ++++++ 9 files changed, 139 insertions(+), 25 deletions(-) create mode 100644 build.rs diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 90db974..aa15a75 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,6 @@ { "git": { - "sha1": "c512385b1a70100c8597f0f519390c80e90c9529" - } -} + "sha1": "817a2ec227e819f308e8e4d0487084940b4ef831" + }, + "path_in_vcs": "" +} \ No newline at end of file diff --git a/Android.bp b/Android.bp index d6285ad..39d18c2 100644 --- a/Android.bp +++ b/Android.bp @@ -23,7 +23,7 @@ rust_library { host_supported: true, crate_name: "slab", cargo_env_compat: true, - cargo_pkg_version: "0.4.5", + cargo_pkg_version: "0.4.7", srcs: ["src/lib.rs"], edition: "2018", features: [ @@ -44,7 +44,7 @@ rust_test { host_supported: true, crate_name: "slab", cargo_env_compat: true, - cargo_pkg_version: "0.4.5", + cargo_pkg_version: "0.4.7", srcs: ["tests/slab.rs"], test_suites: ["general-tests"], auto_gen_config: true, @@ -61,4 +61,5 @@ rust_test { "libserde_test", "libslab", ], + proc_macros: ["librustversion"], } diff --git a/CHANGELOG.md b/CHANGELOG.md index 501a25c..88ac716 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,18 @@ +# 0.4.7 (July 19, 2022) + +* Use `#[track_caller]` on Rust 1.46+ (#119) +* Make `Slab::new` const on Rust 1.39+ (#119) + +# 0.4.6 (April 2, 2022) + +* Add `Slab::vacant_key` (#114) +* Fix stacked borrows violation in `Slab::get2_unchecked_mut` (#115) + # 0.4.5 (October 13, 2021) - * Add alternate debug output for listing items in the slab (#108) - * Fix typo in debug output of IntoIter (#109) - * Impl 'Clone' for 'Iter' (#110) +* Add alternate debug output for listing items in the slab (#108) +* Fix typo in debug output of IntoIter (#109) +* Impl 'Clone' for 'Iter' (#110) # 0.4.4 (August 06, 2021) diff --git a/Cargo.toml b/Cargo.toml index e1cca93..f1c192e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,23 +11,35 @@ [package] edition = "2018" +rust-version = "1.31" name = "slab" -version = "0.4.5" +version = "0.4.7" authors = ["Carl Lerche "] exclude = ["/.*"] description = "Pre-allocated storage for a uniform data type" -homepage = "https://github.com/tokio-rs/slab" -documentation = "https://docs.rs/slab" readme = "README.md" -keywords = ["slab", "allocator", "no_std"] -categories = ["memory-management", "data-structures", "no-std"] +keywords = [ + "slab", + "allocator", + "no_std", +] +categories = [ + "memory-management", + "data-structures", + "no-std", +] license = "MIT" repository = "https://github.com/tokio-rs/slab" + [dependencies.serde] version = "1.0.95" features = ["alloc"] optional = true default-features = false + +[dev-dependencies.rustversion] +version = "1" + [dev-dependencies.serde] version = "1" features = ["derive"] @@ -35,6 +47,9 @@ features = ["derive"] [dev-dependencies.serde_test] version = "1" +[build-dependencies.autocfg] +version = "1" + [features] default = ["std"] std = [] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index bc25be8..d8f19e8 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -6,15 +6,13 @@ name = "slab" # - README.md # - Update CHANGELOG.md # - Create git tag -version = "0.4.5" +version = "0.4.7" authors = ["Carl Lerche "] edition = "2018" +rust-version = "1.31" license = "MIT" description = "Pre-allocated storage for a uniform data type" -documentation = "https://docs.rs/slab" -homepage = "https://github.com/tokio-rs/slab" repository = "https://github.com/tokio-rs/slab" -readme = "README.md" keywords = ["slab", "allocator", "no_std"] categories = ["memory-management", "data-structures", "no-std"] exclude = ["/.*"] @@ -23,9 +21,13 @@ exclude = ["/.*"] std = [] default = ["std"] +[build-dependencies] +autocfg = "1" + [dependencies] serde = { version = "1.0.95", optional = true, default-features = false, features = ["alloc"] } [dev-dependencies] +rustversion = "1" serde = { version = "1", features = ["derive"] } serde_test = "1" diff --git a/METADATA b/METADATA index 6ca4640..dbd8fc2 100644 --- a/METADATA +++ b/METADATA @@ -1,3 +1,7 @@ +# This project was upgraded with external_updater. +# Usage: tools/external_updater/updater.sh update rust/crates/slab +# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md + name: "slab" description: "Pre-allocated storage for a uniform data type" third_party { @@ -7,13 +11,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/slab/slab-0.4.5.crate" + value: "https://static.crates.io/crates/slab/slab-0.4.7.crate" } - version: "0.4.5" + version: "0.4.7" license_type: NOTICE last_upgrade_date { year: 2022 - month: 3 - day: 1 + month: 12 + day: 19 } } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..b60351a --- /dev/null +++ b/build.rs @@ -0,0 +1,24 @@ +fn main() { + let cfg = match autocfg::AutoCfg::new() { + Ok(cfg) => cfg, + Err(e) => { + // If we couldn't detect the compiler version and features, just + // print a warning. This isn't a fatal error: we can still build + // Slab, we just can't enable cfgs automatically. + println!( + "cargo:warning=slab: failed to detect compiler features: {}", + e + ); + return; + } + }; + // Note that this is `no_`*, not `has_*`. This allows treating as the latest + // stable rustc is used when the build script doesn't run. This is useful + // for non-cargo build systems that don't run the build script. + if !cfg.probe_rustc_version(1, 39) { + println!("cargo:rustc-cfg=slab_no_const_vec_new"); + } + if !cfg.probe_rustc_version(1, 46) { + println!("cargo:rustc-cfg=slab_no_track_caller"); + } +} diff --git a/src/lib.rs b/src/lib.rs index 271c1db..577f7b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -219,14 +219,35 @@ impl Slab { /// The function does not allocate and the returned slab will have no /// capacity until `insert` is called or capacity is explicitly reserved. /// + /// This is `const fn` on Rust 1.39+. + /// /// # Examples /// /// ``` /// # use slab::*; /// let slab: Slab = Slab::new(); /// ``` - pub fn new() -> Slab { - Slab::with_capacity(0) + #[cfg(not(slab_no_const_vec_new))] + pub const fn new() -> Self { + Self { + entries: Vec::new(), + next: 0, + len: 0, + } + } + /// Construct a new, empty `Slab`. + /// + /// The function does not allocate and the returned slab will have no + /// capacity until `insert` is called or capacity is explicitly reserved. + /// + /// This is `const fn` on Rust 1.39+. + #[cfg(slab_no_const_vec_new)] + pub fn new() -> Self { + Self { + entries: Vec::new(), + next: 0, + len: 0, + } } /// Construct a new, empty `Slab` with the specified capacity. @@ -830,8 +851,10 @@ impl Slab { /// assert_eq!(slab[key2], 1); /// ``` pub unsafe fn get2_unchecked_mut(&mut self, key1: usize, key2: usize) -> (&mut T, &mut T) { - let ptr1 = self.entries.get_unchecked_mut(key1) as *mut Entry; - let ptr2 = self.entries.get_unchecked_mut(key2) as *mut Entry; + debug_assert_ne!(key1, key2); + let ptr = self.entries.as_mut_ptr(); + let ptr1 = ptr.add(key1); + let ptr2 = ptr.add(key2); match (&mut *ptr1, &mut *ptr2) { (&mut Entry::Occupied(ref mut val1), &mut Entry::Occupied(ref mut val2)) => { (val1, val2) @@ -875,6 +898,7 @@ impl Slab { /// slab.key_of(bad); // this will panic /// unreachable!(); /// ``` + #[cfg_attr(not(slab_no_track_caller), track_caller)] pub fn key_of(&self, present_element: &T) -> usize { let element_ptr = present_element as *const T as usize; let base_ptr = self.entries.as_ptr() as usize; @@ -917,6 +941,30 @@ impl Slab { key } + /// Returns the key of the next vacant entry. + /// + /// This function returns the key of the vacant entry which will be used + /// for the next insertion. This is equivalent to + /// `slab.vacant_entry().key()`, but it doesn't require mutable access. + /// + /// # Examples + /// + /// ``` + /// # use slab::*; + /// let mut slab = Slab::new(); + /// assert_eq!(slab.vacant_key(), 0); + /// + /// slab.insert(0); + /// assert_eq!(slab.vacant_key(), 1); + /// + /// slab.insert(1); + /// slab.remove(0); + /// assert_eq!(slab.vacant_key(), 0); + /// ``` + pub fn vacant_key(&self) -> usize { + self.next + } + /// Return a handle to a vacant entry allowing for further manipulation. /// /// This function is useful when creating values that must contain their @@ -1019,6 +1067,7 @@ impl Slab { /// assert_eq!(slab.remove(hello), "hello"); /// assert!(!slab.contains(hello)); /// ``` + #[cfg_attr(not(slab_no_track_caller), track_caller)] pub fn remove(&mut self, key: usize) -> T { self.try_remove(key).expect("invalid key") } @@ -1126,6 +1175,7 @@ impl Slab { impl ops::Index for Slab { type Output = T; + #[cfg_attr(not(slab_no_track_caller), track_caller)] fn index(&self, key: usize) -> &T { match self.entries.get(key) { Some(&Entry::Occupied(ref v)) => v, @@ -1135,6 +1185,7 @@ impl ops::Index for Slab { } impl ops::IndexMut for Slab { + #[cfg_attr(not(slab_no_track_caller), track_caller)] fn index_mut(&mut self, key: usize) -> &mut T { match self.entries.get_mut(key) { Some(&mut Entry::Occupied(ref mut v)) => v, diff --git a/tests/slab.rs b/tests/slab.rs index 8b03c1e..34371d1 100644 --- a/tests/slab.rs +++ b/tests/slab.rs @@ -698,3 +698,9 @@ fn try_remove() { assert_eq!(slab.try_remove(key), None); assert_eq!(slab.get(key), None); } + +#[rustversion::since(1.39)] +#[test] +fn const_new() { + static _SLAB: Slab<()> = Slab::new(); +} -- cgit v1.2.3