qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: assertions: add static_assert
@ 2025-03-20 11:55 Paolo Bonzini
  2025-03-20 14:00 ` Peter Maydell
  2025-03-20 19:19 ` Pierrick Bouvier
  0 siblings, 2 replies; 4+ messages in thread
From: Paolo Bonzini @ 2025-03-20 11:55 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-rust, Peter Maydell

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.

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Supersedes: <20250320113356.799412-1-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs
index 104dec39774..bba38cfda11 100644
--- a/rust/qemu-api/src/assertions.rs
+++ b/rust/qemu-api/src/assertions.rs
@@ -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);
+    };
+}
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: assertions: add static_assert
  2025-03-20 11:55 [PATCH v2] rust: assertions: add static_assert Paolo Bonzini
@ 2025-03-20 14:00 ` Peter Maydell
  2025-03-20 19:19 ` Pierrick Bouvier
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2025-03-20 14:00 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: qemu-devel, qemu-rust

On Thu, 20 Mar 2025 at 11:55, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> 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.
>
> Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> Supersedes: <20250320113356.799412-1-pbonzini@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  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

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

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

thanks
-- PMM


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: assertions: add static_assert
  2025-03-20 11:55 [PATCH v2] rust: assertions: add static_assert Paolo Bonzini
  2025-03-20 14:00 ` Peter Maydell
@ 2025-03-20 19:19 ` Pierrick Bouvier
  2025-03-20 19:21   ` Pierrick Bouvier
  1 sibling, 1 reply; 4+ messages in thread
From: Pierrick Bouvier @ 2025-03-20 19:19 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: qemu-rust, Peter Maydell

On 3/20/25 04:55, Paolo Bonzini wrote:
> 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.
> 
> Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> Supersedes: <20250320113356.799412-1-pbonzini@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
>   1 file changed, 22 insertions(+)
> 
> diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs
> index 104dec39774..bba38cfda11 100644
> --- a/rust/qemu-api/src/assertions.rs
> +++ b/rust/qemu-api/src/assertions.rs
> @@ -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);
> +    };
> +}

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.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: assertions: add static_assert
  2025-03-20 19:19 ` Pierrick Bouvier
@ 2025-03-20 19:21   ` Pierrick Bouvier
  0 siblings, 0 replies; 4+ messages in thread
From: Pierrick Bouvier @ 2025-03-20 19:21 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: qemu-rust, Peter Maydell

On 3/20/25 12:19, Pierrick Bouvier wrote:
> On 3/20/25 04:55, Paolo Bonzini wrote:
>> 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.
>>
>> Suggested-by: Peter Maydell <peter.maydell@linaro.org>
>> Supersedes: <20250320113356.799412-1-pbonzini@redhat.com>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>>    rust/qemu-api/src/assertions.rs | 22 ++++++++++++++++++++++
>>    1 file changed, 22 insertions(+)
>>
>> diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs
>> index 104dec39774..bba38cfda11 100644
>> --- a/rust/qemu-api/src/assertions.rs
>> +++ b/rust/qemu-api/src/assertions.rs
>> @@ -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);
>> +    };
>> +}
> 
> 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.

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-03-20 19:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-20 11:55 [PATCH v2] rust: assertions: add static_assert Paolo Bonzini
2025-03-20 14:00 ` Peter Maydell
2025-03-20 19:19 ` Pierrick Bouvier
2025-03-20 19:21   ` Pierrick Bouvier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).