* [PATCH 1/2] rust/bits: Align SubAssign behavior with Sub
2026-08-02 17:03 [PATCH 0/2] rust/bits: Fix minor bugs in bits crate Nguyen Dinh Phi
@ 2026-08-02 17:03 ` Nguyen Dinh Phi
2026-08-02 17:03 ` [PATCH 2/2] rust/bits: Use checked_ilog2() in Binary::format to avoid panic Nguyen Dinh Phi
1 sibling, 0 replies; 3+ messages in thread
From: Nguyen Dinh Phi @ 2026-08-02 17:03 UTC (permalink / raw)
To: pbonzini, qemu-devel; +Cc: qemu-rust, Nguyen Dinh Phi
Update SubAssign to perform a bit-clear operation instead of arithmetic
subtraction, matching the behavior of Sub.
Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com>
---
rust/bits/src/lib.rs | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/rust/bits/src/lib.rs b/rust/bits/src/lib.rs
index d1141f7c88..60b63f969d 100644
--- a/rust/bits/src/lib.rs
+++ b/rust/bits/src/lib.rs
@@ -320,7 +320,7 @@ fn sub(self, rhs: $struct_name) -> Self::Output {
impl ::std::ops::SubAssign<$struct_name> for $struct_name {
fn sub_assign(&mut self, rhs: $struct_name) {
- self.0 = self.0 - rhs.0
+ self.0 &= !rhs.0
}
}
@@ -443,4 +443,11 @@ pub fn test_xor() {
InterruptMask::OE | InterruptMask::PE | InterruptMask::FE
);
}
+
+ #[test]
+ pub fn test_sub_assign() {
+ let mut op1 = InterruptMask::E;
+ op1 -= InterruptMask::RI;
+ assert_eq!(op1, InterruptMask::E - InterruptMask::RI);
+ }
}
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] rust/bits: Use checked_ilog2() in Binary::format to avoid panic
2026-08-02 17:03 [PATCH 0/2] rust/bits: Fix minor bugs in bits crate Nguyen Dinh Phi
2026-08-02 17:03 ` [PATCH 1/2] rust/bits: Align SubAssign behavior with Sub Nguyen Dinh Phi
@ 2026-08-02 17:03 ` Nguyen Dinh Phi
1 sibling, 0 replies; 3+ messages in thread
From: Nguyen Dinh Phi @ 2026-08-02 17:03 UTC (permalink / raw)
To: pbonzini, qemu-devel; +Cc: qemu-rust, Nguyen Dinh Phi
ilog2() panics when VALID__ is 0 on empty bits. Switch to checked_ilog2()
to handle zero safely.
Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com>
---
rust/bits/src/lib.rs | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/rust/bits/src/lib.rs b/rust/bits/src/lib.rs
index 60b63f969d..5769bc761a 100644
--- a/rust/bits/src/lib.rs
+++ b/rust/bits/src/lib.rs
@@ -215,7 +215,9 @@ pub const fn invert(self) -> Self {
impl ::std::fmt::Binary for $struct_name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
// If no width, use the highest valid bit
- let width = f.width().unwrap_or((Self::VALID__.ilog2() + 1) as usize);
+ let width = f
+ .width()
+ .unwrap_or(Self::VALID__.checked_ilog2().map_or(1, |bit| (bit + 1) as usize));
write!(f, "{:0>width$.precision$b}", self.0,
width = width,
precision = f.precision().unwrap_or(width))
@@ -412,6 +414,12 @@ pub struct InterruptMask(u32) {
}
}
+ bits! {
+ pub struct EmptyMask(u32) {
+ NONE = 0,
+ }
+ }
+
#[test]
pub fn test_not() {
assert_eq!(
@@ -450,4 +458,9 @@ pub fn test_sub_assign() {
op1 -= InterruptMask::RI;
assert_eq!(op1, InterruptMask::E - InterruptMask::RI);
}
+
+ #[test]
+ pub fn test_bit_display_empty() {
+ assert_eq!(format!("{:b}", EmptyMask::NONE), "0");
+ }
}
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread