[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 08/14] rust: define wrappers for methods of the QOM Device class
From: |
Paolo Bonzini |
Subject: |
[PATCH 08/14] rust: define wrappers for methods of the QOM Device class |
Date: |
Mon, 1 Jul 2024 16:58:40 +0200 |
Provide a trait that can be used to invoke methods of the QOM Device
class. The trait extends Deref and has a blanket implementation for any
type that dereferences to IsA<DeviceState>. This way, it can be used on any
struct that dereferences to Object or a subclass.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
qemu/src/hw/core/device.rs | 56 ++++++++++++++++++++++++++++++++++++++
qemu/src/hw/core/mod.rs | 1 +
qemu/src/hw/mod.rs | 1 +
qemu/src/lib.rs | 4 +++
4 files changed, 62 insertions(+)
create mode 100644 qemu/src/hw/core/device.rs
create mode 100644 qemu/src/hw/core/mod.rs
create mode 100644 qemu/src/hw/mod.rs
diff --git a/qemu/src/hw/core/device.rs b/qemu/src/hw/core/device.rs
new file mode 100644
index 0000000..294251e
--- /dev/null
+++ b/qemu/src/hw/core/device.rs
@@ -0,0 +1,56 @@
+//! Bindings for the QOM Device class
+//!
+//! @author Paolo Bonzini
+
+use crate::qom::object::ObjectType;
+
+use crate::qom::refs::IsA;
+use crate::qom::refs::ObjectCast;
+
+use crate::bindings;
+use crate::bindings::device_cold_reset;
+use crate::bindings::device_realize;
+use crate::bindings::DeviceState;
+use crate::bindings::Object;
+
+use crate::qom_isa;
+
+use crate::Result;
+
+use std::ffi::CStr;
+use std::ops::Deref;
+use std::ptr::null_mut;
+
+unsafe impl ObjectType for DeviceState {
+ const TYPE: &'static CStr = c"device";
+}
+
+qom_isa!(DeviceState, Object);
+
+/// Trait for methods exposed by the Object class. The methods can be
+/// called on all objects that have the trait `IsA<Object>`.
+///
+/// The trait should only be used through the blanket implementation,
+/// which guarantees safety via `IsA`
+pub trait DeviceMethods: Deref
+where
+ Self::Target: IsA<DeviceState>,
+{
+ fn realize(&self) -> Result<()> {
+ let device = self.upcast::<DeviceState>();
+ let mut err: *mut bindings::Error = null_mut();
+ // SAFETY: safety of this is the requirement for implementing IsA
+ unsafe {
+ device_realize(device.as_mut_ptr(), &mut err);
+ crate::Error::err_or_default(err)
+ }
+ }
+
+ fn cold_reset(&self) {
+ let device = self.upcast::<DeviceState>();
+ // SAFETY: safety of this is the requirement for implementing IsA
+ unsafe { device_cold_reset(device.as_mut_ptr()) }
+ }
+}
+
+impl<R: Deref> DeviceMethods for R where R::Target: IsA<DeviceState> {}
diff --git a/qemu/src/hw/core/mod.rs b/qemu/src/hw/core/mod.rs
new file mode 100644
index 0000000..5458924
--- /dev/null
+++ b/qemu/src/hw/core/mod.rs
@@ -0,0 +1 @@
+pub mod device;
diff --git a/qemu/src/hw/mod.rs b/qemu/src/hw/mod.rs
new file mode 100644
index 0000000..5a7ca06
--- /dev/null
+++ b/qemu/src/hw/mod.rs
@@ -0,0 +1 @@
+pub mod core;
diff --git a/qemu/src/lib.rs b/qemu/src/lib.rs
index a6e7b17..b0dcce1 100644
--- a/qemu/src/lib.rs
+++ b/qemu/src/lib.rs
@@ -2,8 +2,12 @@
#![allow(dead_code)]
pub mod bindings;
+pub use bindings::DeviceState;
pub use bindings::Object;
+pub mod hw;
+pub use hw::core::device::DeviceMethods;
+
pub mod qom;
pub use qom::object::ObjectClassMethods;
pub use qom::object::ObjectMethods;
--
2.45.2
- [PATCH 00/14] rust: example of bindings code for Rust in QEMU, Paolo Bonzini, 2024/07/01
- [PATCH 01/14] add skeleton, Paolo Bonzini, 2024/07/01
- [PATCH 02/14] set expectations, Paolo Bonzini, 2024/07/01
- [PATCH 04/14] rust: add tests for util::foreign, Paolo Bonzini, 2024/07/01
- [PATCH 05/14] rust: define wrappers for Error, Paolo Bonzini, 2024/07/01
- [PATCH 03/14] rust: define traits and pointer wrappers to convert from/to C representations, Paolo Bonzini, 2024/07/01
- [PATCH 07/14] rust: define wrappers for methods of the QOM Object class, Paolo Bonzini, 2024/07/01
- [PATCH 08/14] rust: define wrappers for methods of the QOM Device class,
Paolo Bonzini <=
- [PATCH 06/14] rust: define wrappers for basic QOM concepts, Paolo Bonzini, 2024/07/01
- [PATCH 09/14] rust: add idiomatic bindings to define Object subclasses, Paolo Bonzini, 2024/07/01
- [PATCH 10/14] rust: add idiomatic bindings to define Device subclasses, Paolo Bonzini, 2024/07/01
- [PATCH 12/14] rust: replace c"" literals with cstr crate, Paolo Bonzini, 2024/07/01
- [PATCH 14/14] rust: use version of toml_edit that does not require new Rust, Paolo Bonzini, 2024/07/01
- [PATCH 13/14] rust: introduce alternative to offset_of!, Paolo Bonzini, 2024/07/01
- [PATCH 11/14] rust: replace std::ffi::c_char with libc::c_char, Paolo Bonzini, 2024/07/01
- Re: [PATCH 00/14] rust: example of bindings code for Rust in QEMU, Pierrick Bouvier, 2024/07/04