Skip to contentSkip to conversation

rust: assertions: add static_assert

PATCH In review Paolo Bonzini 3 participants 4 messages Mar 20, 2025 Rust

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rust: assertions: add static_assert

Add a new assertion that is similar to "const { assert!(...) }" but can be used outside functions and with older versions of Rust. A similar macro is found in Linux, whereas the "static_assertions" crate has a const_assert macro that produces worse error messages.

Supersedes: <[email protected]>

---
 rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
rust/qemu-api/src/assertions.rs +22 -0
index 104dec39774..bba38cfda11 100644
@@ -120,3 +120,25 @@ macro_rules! assert_match {
         );
     };
 }
+
+/// Assert at compile time that an expression is true.  This is similar
+/// to `const { assert!(...); }` but it works outside functions, as well as
+/// on versions of Rust before 1.79.
+///
+/// # Examples
+///
+/// ```
+/// # use qemu_api::static_assert;
+/// static_assert!("abc".len() == 3);
+/// ```
+///
+/// ```compile_fail
+/// # use qemu_api::static_assert;
+/// static_assert!("abc".len() == 2); // does not compile
+/// ```
+#[macro_export]
+macro_rules! static_assert {
+    ($x:expr) => {
+        const _: () = assert!($x);
+    };
+}
Signature · 1 line
2.48.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Paolo Bonzini

Add a new assertion that is similar to "const { assert!(...) }" but can be used outside functions and with older versions of Rust. A similar macro is found in Linux, whereas the "static_assertions" crate has a const_assert macro that produces worse error messages.

Quoted 6 lines

Supersedes: <[email protected]>

---
 rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

Macro magic in Rust is somewhat beyond my current competency, but it works for my use case, and it looks like it's doing the same thing Linux uses, so on that basis

(I put this patch into the series I just sent out that fixes the PL011 size issue.)

thanks
-- PMM

#2 lore raw
In reply to Paolo Bonzini
Quoted 32 lines · Paolo Bonzini

Add a new assertion that is similar to "const { assert!(...) }" but can be used outside functions and with older versions of Rust. A similar macro is found in Linux, whereas the "static_assertions" crate has a const_assert macro that produces worse error messages.

Supersedes: <[email protected]>

---
  rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
rust/qemu-api/src/assertions.rs +12 -0
index 104dec39774..bba38cfda11 100644
@@ -120,3 +120,25 @@ macro_rules! assert_match {
          );
      };
  }
+
+/// Assert at compile time that an expression is true.  This is similar
+/// to `const { assert!(...); }` but it works outside functions, as well as
+/// on versions of Rust before 1.79.
+///
+/// # Examples
+///
+/// ```
+/// # use qemu_api::static_assert;
+/// static_assert!("abc".len() == 3);
+/// ```
+///

rust/qemu-api/src/assertions.rs macro_rules! assert_match { · line 144

Quoted 10 lines
rust/qemu-api/src/assertions.rs +10 -0
+/// ```compile_fail
+/// # use qemu_api::static_assert;
+/// static_assert!("abc".len() == 2); // does not compile
+/// ```
+#[macro_export]
+macro_rules! static_assert {
+    ($x:expr) => {
+        const _: () = assert!($x);
+    };
+}

How about using something already done, and exhaustive for this?
https://docs.rs/static_assertions/latest/static_assertions/

It provides a lot of convenient asserts related to types, traits, and configs, which will probably end up being introduced in QEMU Rust at some point.

#3 lore raw
Quoted 47 lines · Pierrick Bouvier
Quoted 32 lines · Paolo Bonzini

Add a new assertion that is similar to "const { assert!(...) }" but can be used outside functions and with older versions of Rust. A similar macro is found in Linux, whereas the "static_assertions" crate has a const_assert macro that produces worse error messages.

Supersedes: <[email protected]>

---
   rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
   1 file changed, 22 insertions(+)
rust/qemu-api/src/assertions.rs +12 -0
index 104dec39774..bba38cfda11 100644
@@ -120,3 +120,25 @@ macro_rules! assert_match {
           );
       };
   }
+
+/// Assert at compile time that an expression is true.  This is similar
+/// to `const { assert!(...); }` but it works outside functions, as well as
+/// on versions of Rust before 1.79.
+///
+/// # Examples
+///
+/// ```
+/// # use qemu_api::static_assert;
+/// static_assert!("abc".len() == 3);
+/// ```
+///

rust/qemu-api/src/assertions.rs macro_rules! assert_match { · line 144

Quoted 10 lines
rust/qemu-api/src/assertions.rs +10 -0
+/// ```compile_fail
+/// # use qemu_api::static_assert;
+/// static_assert!("abc".len() == 2); // does not compile
+/// ```
+#[macro_export]
+macro_rules! static_assert {
+    ($x:expr) => {
+        const _: () = assert!($x);
+    };
+}

How about using something already done, and exhaustive for this?
https://docs.rs/static_assertions/latest/static_assertions/

Quoted 3 lines

It provides a lot of convenient asserts related to types, traits, and configs, which will probably end up being introduced in QEMU Rust at some point.

Just saw it was mentioned "don't like this crate because of error messages". Well...