From 5ff74b43a51c737bd9acb603a830f0313296ba92 Mon Sep 17 00:00:00 2001 From: Pierre Chevalier Date: Tue, 16 Jan 2024 00:38:02 -0800 Subject: [PATCH] Update bitflags to 2.4 Summary: ## Motivation Since the latest compiler update, we are getting `clippy::bad_bit_mask` errors at the callsites of `bitflags!` macros where one of the variant is zero. [Upstream won't address it in the `1.x` branch](https://github.com/bitflags/bitflags/pull/373) and recommends upgrading to the `2.x` branch. We are very close to reaching **zero clippy lints** in [Mononoke and other servers](https://fburl.com/code/pd76yn5e), which would feel nice. ## Specific categories of changes (in case it helps with the code review) The change from `1.x` to `2.x` introduces a number of backward compatibility breakages which I had to workaround in our codebase. See [the release notes for 2.0](https://github.com/bitflags/bitflags/releases/tag/2.0.0) for the explanation for the manual fixes I had to perform at each call site. --- **Adding traits to derive:** ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] ``` > Generated flags types now derive fewer traits. If you need to maintain backwards compatibility, you can derive the following yourself: --- **Replacing read uses of `.bits` with `.bits()`** > You can now use the .bits() method instead of the old .bits. > The representation of generated flags types has changed from a struct with the single field bits to a newtype. --- **Replacing raw setting of `.bits` with `.from_bits_retain()`** Due to the point above, the representation of the type is not exposed anymore. From [the documentation](https://docs.rs/bitflags/latest/bitflags/example_generated/struct.Flags.html#method.from_bits_retain), `from_bits_retain` "convert from a bits value exactly", which matches the old behaviour --- **Replacing the unsafe `from_bits_unchecked` method with `from_bits_retain`** > The unsafe from_bits_unchecked method is now a safe from_bits_retain method. --- **Extracting some structs outside of the `bitflags!` macro** Apart from the derives that `bitflags` knows about, such as `serde`, `bitflags` now can't deal with custom derives in macros with the previous syntax. I followed the recommendation from [their documentation](https://docs.rs/bitflags/latest/bitflags/index.html#custom-derives) to declare the `struct` ahead of of the macro call and only declare the `impl` block inside the macro. --- **Changes to test output** This does not stand out in the release notes, but as of [this upstream PR](https://github.com/bitflags/bitflags/pull/297), the `Debug` output of generated bitflags has changed. This means any tests that rely on this (and of course, there are a few) needed updating. In particular, the `vespa` tests rely on that output in a non-obvious way. You might have to trust me (and CI) on these ones... Reviewed By: dtolnay Differential Revision: D49742979 fbshipit-source-id: c818c37af45f0964e8fdb7ec6173ad66bb982c00 --- below/store/Cargo.toml | 2 +- below/store/src/lib.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/below/store/Cargo.toml b/below/store/Cargo.toml index 27256ec2..cce92894 100644 --- a/below/store/Cargo.toml +++ b/below/store/Cargo.toml @@ -11,7 +11,7 @@ license = "Apache-2.0" [dependencies] anyhow = "1.0.75" -bitflags = "1.3" +bitflags = "2.4" bytes = { version = "1.1", features = ["serde"] } common = { package = "below-common", version = "0.7.1", path = "../common" } humantime = "2.1" diff --git a/below/store/src/lib.rs b/below/store/src/lib.rs index 5b3b58e5..d2b40045 100644 --- a/below/store/src/lib.rs +++ b/below/store/src/lib.rs @@ -94,6 +94,7 @@ pub const MAX_CHUNK_COMPRESS_SIZE: u32 = 1 << MAX_CHUNK_COMPRESS_SIZE_PO2; const_assert_eq!(MAX_CHUNK_COMPRESS_SIZE, 32768); bitflags! { + #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] struct IndexEntryFlags: u32 { /// If set, data item is compressed with zstd. const COMPRESSED = 0x1; @@ -122,7 +123,7 @@ bitflags! { impl IndexEntryFlags { fn get_chunk_compress_size_po2(&self) -> u32 { - (self.bits & Self::CHUNK_COMPRESS_SIZE_PO2.bits) >> CHUNK_COMPRESS_SHIFT + (self.bits() & Self::CHUNK_COMPRESS_SIZE_PO2.bits()) >> CHUNK_COMPRESS_SHIFT } fn set_chunk_compress_size_po2(&mut self, chunk_compress_size_po2: u32) -> Result<()> { @@ -132,7 +133,7 @@ impl IndexEntryFlags { MAX_CHUNK_COMPRESS_SIZE_PO2 ); } - self.bits |= chunk_compress_size_po2 << CHUNK_COMPRESS_SHIFT; + *self |= IndexEntryFlags::from_bits_retain(chunk_compress_size_po2 << CHUNK_COMPRESS_SHIFT); Ok(()) } }