[PATCH 1/9] rust: device: implement impl_device_context_deref!

From: Danilo Krummrich
Date: Mon Mar 31 2025 - 16:30:01 EST


The Deref hierarchy for device context generics is the same for every
(bus specific) device.

Implement those with a generic macro to avoid duplicated boiler plate
code and ensure the correct Deref hierarchy for every device
implementation.

Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/device.rs | 29 +++++++++++++++++++++++++++++
rust/kernel/pci.rs | 14 +-------------
rust/kernel/platform.rs | 15 +--------------
3 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 21b343a1dc4d..e3ab8980270e 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -235,6 +235,35 @@ impl Sealed for super::Normal {}
impl DeviceContext for Core {}
impl DeviceContext for Normal {}

+#[doc(hidden)]
+#[macro_export]
+macro_rules! __impl_device_context_deref {
+ ($src:ty, $dst:ty, $device:tt) => {
+ impl core::ops::Deref for $device<$src> {
+ type Target = $device<$dst>;
+
+ fn deref(&self) -> &Self::Target {
+ let ptr: *const Self = self;
+
+ // CAST: `Device<Ctx: DeviceContext>` types are transparent to each other.
+ let ptr = ptr.cast::<Self::Target>();
+
+ // SAFETY: `ptr` was derived from `&self`.
+ unsafe { &*ptr }
+ }
+ }
+ };
+}
+
+/// Implement [`core::ops::Deref`] traits for allowed [`DeviceContext`] conversions of a (bus
+/// specific) device.
+#[macro_export]
+macro_rules! impl_device_context_deref {
+ ($device:tt) => {
+ kernel::__impl_device_context_deref!($crate::device::Core, $crate::device::Normal, $device);
+ };
+}
+
#[doc(hidden)]
#[macro_export]
macro_rules! dev_printk {
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index c97d6d470b28..0e735409bfc4 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -422,19 +422,7 @@ pub fn set_master(&self) {
}
}

-impl Deref for Device<device::Core> {
- type Target = Device;
-
- fn deref(&self) -> &Self::Target {
- let ptr: *const Self = self;
-
- // CAST: `Device<Ctx>` is a transparent wrapper of `Opaque<bindings::pci_dev>`.
- let ptr = ptr.cast::<Device>();
-
- // SAFETY: `ptr` was derived from `&self`.
- unsafe { &*ptr }
- }
-}
+kernel::impl_device_context_deref!(Device);

impl From<&Device<device::Core>> for ARef<Device> {
fn from(dev: &Device<device::Core>) -> Self {
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 4917cb34e2fe..9268e1edca9b 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -16,7 +16,6 @@

use core::{
marker::PhantomData,
- ops::Deref,
ptr::{addr_of_mut, NonNull},
};

@@ -190,19 +189,7 @@ fn as_raw(&self) -> *mut bindings::platform_device {
}
}

-impl Deref for Device<device::Core> {
- type Target = Device;
-
- fn deref(&self) -> &Self::Target {
- let ptr: *const Self = self;
-
- // CAST: `Device<Ctx>` is a transparent wrapper of `Opaque<bindings::platform_device>`.
- let ptr = ptr.cast::<Device>();
-
- // SAFETY: `ptr` was derived from `&self`.
- unsafe { &*ptr }
- }
-}
+kernel::impl_device_context_deref!(Device);

impl From<&Device<device::Core>> for ARef<Device> {
fn from(dev: &Device<device::Core>) -> Self {
--
2.49.0