[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 02/12] rust: add a utility module for compile-time type checks
From: |
Paolo Bonzini |
Subject: |
[PATCH 02/12] rust: add a utility module for compile-time type checks |
Date: |
Fri, 20 Dec 2024 15:29:44 +0100 |
It is relatively common in the low-level qemu_api code to assert that
a field of a struct has a specific type; for example, it can be used
to ensure that the fields match what the qemu_api and C code expects
for safety.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api/meson.build | 1 +
rust/qemu-api/src/assertions.rs | 90 +++++++++++++++++++++++++++++++++
rust/qemu-api/src/lib.rs | 1 +
3 files changed, 92 insertions(+)
create mode 100644 rust/qemu-api/src/assertions.rs
diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build
index 9425ba7100c..60944a657de 100644
--- a/rust/qemu-api/meson.build
+++ b/rust/qemu-api/meson.build
@@ -15,6 +15,7 @@ _qemu_api_rs = static_library(
structured_sources(
[
'src/lib.rs',
+ 'src/assertions.rs',
'src/bindings.rs',
'src/bitops.rs',
'src/callbacks.rs',
diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs
new file mode 100644
index 00000000000..1a6cb3a6c7c
--- /dev/null
+++ b/rust/qemu-api/src/assertions.rs
@@ -0,0 +1,90 @@
+// Copyright 2024, Red Hat Inc.
+// Author(s): Paolo Bonzini <pbonzini@redhat.com>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+//! This module provides macros to check the equality of types and
+//! the type of `struct` fields. This can be useful to ensure that
+//! types match the expectations of C code.
+
+// Based on https://stackoverflow.com/questions/64251852/x/70978292#70978292
+// (stackoverflow answers are released under MIT license).
+
+#[doc(hidden)]
+pub trait EqType {
+ type Itself;
+}
+
+impl<T> EqType for T {
+ type Itself = T;
+}
+
+/// Assert that two types are the same.
+///
+/// # Examples
+///
+/// ```
+/// # use qemu_api::assert_same_type;
+/// # use std::ops::Deref;
+/// assert_same_type!(u32, u32);
+/// assert_same_type!(<Box<u32> as Deref>::Target, u32);
+/// ```
+///
+/// Different types will cause a compile failure
+///
+/// ```compile_fail
+/// # use qemu_api::assert_same_type;
+/// assert_same_type!(&Box<u32>, &u32);
+/// ```
+#[macro_export]
+macro_rules! assert_same_type {
+ ($t1:ty, $t2:ty) => {
+ const _: () = {
+ #[allow(unused)]
+ fn assert_same_type(v: $t1) {
+ fn types_must_be_equal<T, U>(_: T)
+ where
+ T: $crate::assertions::EqType<Itself = U>,
+ {
+ }
+ types_must_be_equal::<_, $t2>(v);
+ }
+ };
+ };
+}
+
+/// Assert that a field of a struct has the given type.
+///
+/// # Examples
+///
+/// ```
+/// # use qemu_api::assert_field_type;
+/// pub struct A {
+/// field1: u32,
+/// }
+///
+/// assert_field_type!(A, field1, u32);
+/// ```
+///
+/// Different types will cause a compile failure
+///
+/// ```compile_fail
+/// # use qemu_api::assert_field_type;
+/// # pub struct A { field1: u32 }
+/// assert_field_type!(A, field1, i32);
+/// ```
+#[macro_export]
+macro_rules! assert_field_type {
+ ($t:ty, $i:ident, $ti:ty) => {
+ const _: () = {
+ #[allow(unused)]
+ fn assert_field_type(v: $t) {
+ fn types_must_be_equal<T, U>(_: T)
+ where
+ T: $crate::assertions::EqType<Itself = U>,
+ {
+ }
+ types_must_be_equal::<_, $ti>(v.$i);
+ }
+ };
+ };
+}
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index 4b43e02c0f9..83c6a987c05 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -12,6 +12,7 @@
#[rustfmt::skip]
pub mod prelude;
+pub mod assertions;
pub mod bitops;
pub mod c_str;
pub mod callbacks;
--
2.47.1
- [PATCH 00/12] Next round of qemu_api patches, Paolo Bonzini, 2024/12/20
- [PATCH 04/12] rust: macros: check that #[derive(Object)] requires #[repr(C)], Paolo Bonzini, 2024/12/20
- [PATCH 01/12] rust: qom: add ParentField, Paolo Bonzini, 2024/12/20
- [PATCH 02/12] rust: add a utility module for compile-time type checks,
Paolo Bonzini <=
- [PATCH 03/12] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField, Paolo Bonzini, 2024/12/20
- [PATCH 11/12] rust: qemu-api-macros: add automatic TryFrom/TryInto derivation, Paolo Bonzini, 2024/12/20
- [PATCH 07/12] rust: pl011: only leave embedded object initialization in instance_init, Paolo Bonzini, 2024/12/20
- [PATCH 09/12] rust: qdev: expose inherited methods to subclasses of SysBusDevice, Paolo Bonzini, 2024/12/20
- [PATCH 05/12] rust: qom: automatically use Drop trait to implement instance_finalize, Paolo Bonzini, 2024/12/20
- [PATCH 10/12] rust: qemu-api-macros: extend error reporting facility to parse errors, Paolo Bonzini, 2024/12/20
- [PATCH 06/12] rust: qom: move device_id to PL011 class side, Paolo Bonzini, 2024/12/20
- [PATCH 08/12] rust: qom: make INSTANCE_POST_INIT take a shared reference, Paolo Bonzini, 2024/12/20
- [PATCH 12/12] rust: hide warnings for subprojects, Paolo Bonzini, 2024/12/20