Skip to content

Commit

Permalink
Update bitflags to 2.x
Browse files Browse the repository at this point in the history
This brings us up to the most recent release, and has some nice
properties, like there now being a trait that describes the bitflags API
instead of it all being open-coded. (This is the property that motivated
this change.)

There are some slightly annoying aspects, like the operations in that
trait no longer being const (because we don't have const impls yet). So,
this required some surgery.
  • Loading branch information
cbiffle committed Feb 2, 2024
1 parent e702b7d commit ead0d5d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 24 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ anyhow = { version = "1.0.31", default-features = false, features = ["std"] }
arrayvec = { version = "0.7.4", default-features = false }
atty = { version = "0.2", default-features = false }
bitfield = { version = "0.13", default-features = false }
bitflags = { version = "1.2.1", default-features = false }
bitflags = { version = "2.4.1", default-features = false }
bstringify = { version = "0.1.2", default-features = false }
byteorder = { version = "1.3.4", default-features = false }
cargo_metadata = { version = "0.12.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions drv/stm32xx-sys/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ cfg_if::cfg_if! {
) -> Option<ResetReason> {
bitflags::bitflags! {
// See RM0433 section 8.7.39 (RCC_RSR).
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct ResetFlags: u32 {
const LPWR = 1 << 30;
Expand Down
38 changes: 32 additions & 6 deletions lib/host-sp-messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,38 @@ impl From<HubpackError> for DecodeFailureReason {
}
}

#[derive(
Copy,
Clone,
Debug,
Eq,
PartialEq,
Serialize,
Deserialize,
SerializedSize,
FromBytes,
AsBytes,
)]
#[repr(transparent)]
pub struct Status(u64);

#[derive(
Copy,
Clone,
Debug,
Eq,
PartialEq,
Serialize,
Deserialize,
SerializedSize,
FromBytes,
AsBytes,
)]
#[repr(transparent)]
pub struct HostStartupOptions(u64);

bitflags::bitflags! {
#[derive(Serialize, Deserialize, SerializedSize, FromBytes, AsBytes)]
#[repr(transparent)]
pub struct Status: u64 {
impl Status: u64 {
const SP_TASK_RESTARTED = 1 << 0;
const ALERTS_AVAILABLE = 1 << 1;

Expand All @@ -521,9 +549,7 @@ bitflags::bitflags! {
// When adding fields to this struct, update the static assertions below to
// ensure our conversions to/from `gateway_messages::StartupOptions` remain
// valid!
#[derive(Serialize, Deserialize, SerializedSize, FromBytes, AsBytes)]
#[repr(transparent)]
pub struct HostStartupOptions: u64 {
impl HostStartupOptions: u64 {
const PHASE2_RECOVERY_MODE = 1 << 0;
const STARTUP_KBM = 1 << 1;
const STARTUP_BOOTRD = 1 << 2;
Expand Down
8 changes: 5 additions & 3 deletions sys/abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ pub struct ULease {
pub length: u32,
}

#[derive(Copy, Clone, Debug, FromBytes)]
#[repr(transparent)]
pub struct LeaseAttributes(u32);

bitflags::bitflags! {
#[derive(FromBytes)]
#[repr(transparent)]
pub struct LeaseAttributes: u32 {
impl LeaseAttributes: u32 {
/// Allow the borrower to read this memory.
const READ = 1 << 0;
/// Allow the borrower to write this memory.
Expand Down
17 changes: 14 additions & 3 deletions sys/kern/src/descs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct TaskDesc {
}

bitflags::bitflags! {
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct TaskFlags: u8 {
const START_AT_BOOT = 1 << 0;
Expand Down Expand Up @@ -131,10 +132,14 @@ impl RegionDesc {
}
}

// This is defined outside the bitflags! macro so that we can write our own
// const constructor fn, below.
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct RegionAttributes(u32);

bitflags::bitflags! {
#[repr(transparent)]
#[derive(Serialize, Deserialize)]
pub struct RegionAttributes: u32 {
impl RegionAttributes: u32 {
/// Region can be read by tasks that include it.
const READ = 1 << 0;
/// Region can be written by tasks that include it.
Expand All @@ -155,3 +160,9 @@ bitflags::bitflags! {
const RESERVED = !((1 << 5) - 1);
}
}

impl RegionAttributes {
pub const unsafe fn from_bits_unchecked(bits: u32) -> Self {
Self(bits)
}
}
3 changes: 2 additions & 1 deletion task/thermal/src/bsp/gimlet_bcdef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ pub(crate) struct Bsp {
}

bitflags::bitflags! {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct PowerBitmask: u32 {
// As far as I know, we don't have any devices which are active only
// in A2; you probably want to use `A0_OR_A2` instead.
const A2 = 0b00000001;
const A0 = 0b00000010;
const A0_OR_A2 = Self::A0.bits | Self::A2.bits;
const A0_OR_A2 = Self::A0.bits() | Self::A2.bits();

// Bonus bits for M.2 power, which is switched separately. We *cannot*
// read the M.2 drives when they are unpowered; otherwise, we risk
Expand Down
3 changes: 2 additions & 1 deletion task/thermal/src/bsp/sidecar_bcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ pub const USE_CONTROLLER: bool = true;
////////////////////////////////////////////////////////////////////////////////

bitflags::bitflags! {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct PowerBitmask: u32 {
// As far as I know, we don't have any devices which are active only
// in A2; you probably want to use `POWER_STATE_A0_OR_A2` instead
const A2 = 0b00000001;
const A0 = 0b00000010;
const A0_OR_A2 = Self::A0.bits | Self::A2.bits;
const A0_OR_A2 = Self::A0.bits() | Self::A2.bits();
}
}

Expand Down

0 comments on commit ead0d5d

Please sign in to comment.