Message ID | 20250413173758.12068-3-dakr@kernel.org (mailing list archive) |
---|---|
State | Handled Elsewhere |
Delegated to: | Bjorn Helgaas |
Headers | show |
Series | Implement "Bound" device context | expand |
On Sun Apr 13, 2025 at 7:36 PM CEST, Danilo Krummrich wrote: > Implement a macro to implement all From conversions of a certain device > to ARef<Device>. > > This avoids unnecessary boiler plate code for every device > implementation. > > Signed-off-by: Danilo Krummrich <dakr@kernel.org> One nit below, with that fixed: Reviewed-by: Benno Lossin <benno.lossin@proton.me> > --- > rust/kernel/device.rs | 21 +++++++++++++++++++++ > rust/kernel/pci.rs | 7 +------ > rust/kernel/platform.rs | 9 ++------- > 3 files changed, 24 insertions(+), 13 deletions(-) > > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs > index 7cb6f0fc005d..26e71224460b 100644 > --- a/rust/kernel/device.rs > +++ b/rust/kernel/device.rs > @@ -279,6 +279,27 @@ macro_rules! impl_device_context_deref { > }; > } > > +#[doc(hidden)] > +#[macro_export] > +macro_rules! __impl_device_context_into_aref { > + ($src:ty, $device:tt) => { > + impl core::convert::From<&$device<$src>> for $crate::types::ARef<$device> { Missing `::` in front of `core`. > + fn from(dev: &$device<$src>) -> Self { > + (&**dev).into() > + } > + } > + }; > +} > + > +/// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an > +/// `ARef<Device>`. > +#[macro_export] > +macro_rules! impl_device_context_into_aref { > + ($device:tt) => { > + kernel::__impl_device_context_into_aref!($crate::device::Core, $device); > + }; > +} > + > #[doc(hidden)] > #[macro_export] > macro_rules! dev_printk {
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 7cb6f0fc005d..26e71224460b 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -279,6 +279,27 @@ macro_rules! impl_device_context_deref { }; } +#[doc(hidden)] +#[macro_export] +macro_rules! __impl_device_context_into_aref { + ($src:ty, $device:tt) => { + impl core::convert::From<&$device<$src>> for $crate::types::ARef<$device> { + fn from(dev: &$device<$src>) -> Self { + (&**dev).into() + } + } + }; +} + +/// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an +/// `ARef<Device>`. +#[macro_export] +macro_rules! impl_device_context_into_aref { + ($device:tt) => { + kernel::__impl_device_context_into_aref!($crate::device::Core, $device); + }; +} + #[doc(hidden)] #[macro_export] macro_rules! dev_printk { diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 8474608e7a90..7c6ec05383e0 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -425,12 +425,7 @@ pub fn set_master(&self) { // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic // argument. kernel::impl_device_context_deref!(unsafe { Device }); - -impl From<&Device<device::Core>> for ARef<Device> { - fn from(dev: &Device<device::Core>) -> Self { - (&**dev).into() - } -} +kernel::impl_device_context_into_aref!(Device); // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 22590bdff7bb..9476b717c425 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -10,7 +10,7 @@ of, prelude::*, str::CStr, - types::{ARef, ForeignOwnable, Opaque}, + types::{ForeignOwnable, Opaque}, ThisModule, }; @@ -192,12 +192,7 @@ fn as_raw(&self) -> *mut bindings::platform_device { // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic // argument. kernel::impl_device_context_deref!(unsafe { Device }); - -impl From<&Device<device::Core>> for ARef<Device> { - fn from(dev: &Device<device::Core>) -> Self { - (&**dev).into() - } -} +kernel::impl_device_context_into_aref!(Device); // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device {
Implement a macro to implement all From conversions of a certain device to ARef<Device>. This avoids unnecessary boiler plate code for every device implementation. Signed-off-by: Danilo Krummrich <dakr@kernel.org> --- rust/kernel/device.rs | 21 +++++++++++++++++++++ rust/kernel/pci.rs | 7 +------ rust/kernel/platform.rs | 9 ++------- 3 files changed, 24 insertions(+), 13 deletions(-)