Skip to contentSkip to conversation

rust: tests: do not import bindings::*

PATCH In review Paolo Bonzini 2 participants 3 messages Feb 14, 2025 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: tests: do not import bindings::*

Similar to the devices, spell the exact set of C functions that are called directly.

---
 rust/qemu-api/tests/tests.rs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rust/qemu-api/tests/tests.rs +2 -1
index 92dbfb8a0c8..03569e4a44c 100644
@@ -8,13 +8,14 @@
 };
 
 use qemu_api::{
-    bindings::*,
+    bindings::{module_call_init, module_init_type, object_new, object_unref, qdev_prop_bool},
     c_str,
     cell::{self, BqlCell},
     declare_properties, define_property,
     prelude::*,
     qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl},
     qom::{ClassInitImpl, ObjectImpl, ParentField},
+    sysbus::SysBusDevice,
     vmstate::VMStateDescription,
     zeroable::Zeroable,
 };
Signature · 1 line
2.48.1
#1 lore raw
Quoted 13 lines · Paolo Bonzini

Similar to the devices, spell the exact set of C functions that are called directly.

---
 rust/qemu-api/tests/tests.rs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rust/qemu-api/tests/tests.rs +0 -0
index 92dbfb8a0c8..03569e4a44c 100644

rust/qemu-api/tests/tests.rs line 15

Quoted 9 lines
rust/qemu-api/tests/tests.rs +1 -1
@@ -8,13 +8,14 @@
 };
 
 use qemu_api::{
-    bindings::*,
+    bindings::{module_call_init, module_init_type, object_new, object_unref, qdev_prop_bool},
     c_str,
     cell::{self, BqlCell},
     declare_properties, define_property,
     prelude::*,

Should this be expanded too if we want to discourage wildcard imports ?

rust/qemu-api/tests/tests.rs line 18

Quoted 3 lines
rust/qemu-api/tests/tests.rs +1 -0
     qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl},
     qom::{ClassInitImpl, ObjectImpl, ParentField},
+    sysbus::SysBusDevice,

This addition seems distinct from the bindings::* removal. Should it have been a separate patch ?

rust/qemu-api/tests/tests.rs line 21

Quoted 3 lines
rust/qemu-api/tests/tests.rs +0 -0
     vmstate::VMStateDescription,
     zeroable::Zeroable,
 };

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw

First of all, thanks for taking a look!!

Quoted 3 lines
Quoted 1 line
     prelude::*,

Should this be expanded too if we want to discourage wildcard imports ?

The prelude is a bit special; it provides a subset of items that most users will want to include into the namespace. You're right that it goes against the rule of discouraging wildcard imports, and in fact that's why it almost entirely consists of extension traits (e.g. DeviceMethods, SysBusDeviceMethods, ObjectCast, ObjectDeref, ObjectClassMethods). You shouldn't have to name the traits at all if the API works, and having them in the prelude lowers the cognitive load.

There are a few more symbols that are imported in the prelude: for example the BqlCell and BqlRefCell, the ObjectType and IsA traits, or the qom_isa macro; those will be used almost everywhere.

The exact shape of the prelude might change in the future, since qemu_api might get split into separate crates to enable using Rust in tools (which do not have qdev). For example there could be separate block, qdev and qom preludes.

In any case, it's an exception and for this reason the amount of non-extension-traits symbols that it includes should be limited.

Quoted 6 lines
Quoted 3 lines

qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl}, qom::{ClassInitImpl, ObjectImpl, ParentField},
+ sysbus::SysBusDevice,

This addition seems distinct from the bindings::* removal. Should it have been a separate patch ?

SysBusDevice is re-exported from sysbus, similar to DeviceState or VMStateDescription that you see in the context. This is done because in general using "bindings" is an anti-pattern; you should use APIs that have been wrapped properly as is the case for SysBusDevice.

The tests use some "bindings::foo" symbols either because they're setting up the test environment (module_call_init) or because they're intentionally bypassing the high-level API (object_new/unref). The only case where something isn't yet covered by safe wrappers is qdev_prop_bool.

In fact, a lot of the structs that the tests need were imported twice, once from bindings and once from e.g. qdev or vmstate. SysBusDevice was the odd one out that was imported only from bindings, so it has added.

Paolo

Quoted 6 lines
Quoted 3 lines

vmstate::VMStateDescription,
zeroable::Zeroable,
};

With regards,
Daniel