Skip to content

Commit fbb92b6

Browse files
author
Danilo Krummrich
committed
rust: device: implement impl_device_context_into_aref!
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. Reviewed-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Add missing `::` prefix in macros. - Danilo ] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent cb271c2 commit fbb92b6

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

rust/kernel/device.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,27 @@ macro_rules! impl_device_context_deref {
279279
};
280280
}
281281

282+
#[doc(hidden)]
283+
#[macro_export]
284+
macro_rules! __impl_device_context_into_aref {
285+
($src:ty, $device:tt) => {
286+
impl ::core::convert::From<&$device<$src>> for $crate::types::ARef<$device> {
287+
fn from(dev: &$device<$src>) -> Self {
288+
(&**dev).into()
289+
}
290+
}
291+
};
292+
}
293+
294+
/// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an
295+
/// `ARef<Device>`.
296+
#[macro_export]
297+
macro_rules! impl_device_context_into_aref {
298+
($device:tt) => {
299+
::kernel::__impl_device_context_into_aref!($crate::device::Core, $device);
300+
};
301+
}
302+
282303
#[doc(hidden)]
283304
#[macro_export]
284305
macro_rules! dev_printk {

rust/kernel/pci.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,7 @@ impl Device<device::Core> {
425425
// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
426426
// argument.
427427
kernel::impl_device_context_deref!(unsafe { Device });
428-
429-
impl From<&Device<device::Core>> for ARef<Device> {
430-
fn from(dev: &Device<device::Core>) -> Self {
431-
(&**dev).into()
432-
}
433-
}
428+
kernel::impl_device_context_into_aref!(Device);
434429

435430
// SAFETY: Instances of `Device` are always reference-counted.
436431
unsafe impl crate::types::AlwaysRefCounted for Device {

rust/kernel/platform.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
of,
1111
prelude::*,
1212
str::CStr,
13-
types::{ARef, ForeignOwnable, Opaque},
13+
types::{ForeignOwnable, Opaque},
1414
ThisModule,
1515
};
1616

@@ -192,12 +192,7 @@ impl Device {
192192
// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
193193
// argument.
194194
kernel::impl_device_context_deref!(unsafe { Device });
195-
196-
impl From<&Device<device::Core>> for ARef<Device> {
197-
fn from(dev: &Device<device::Core>) -> Self {
198-
(&**dev).into()
199-
}
200-
}
195+
kernel::impl_device_context_into_aref!(Device);
201196

202197
// SAFETY: Instances of `Device` are always reference-counted.
203198
unsafe impl crate::types::AlwaysRefCounted for Device {

0 commit comments

Comments
 (0)