Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: device: support `dev_printk` on all devices

Currently, `dev_*` only works on the core `Device`, but not on any other
bus or class device objects. This causes a pattern of
`dev_info!(pdev.as_ref())` which is not ideal.

This adds support of using these devices directly with `dev_*` macros, by
adding `AsRef` call inside the macro. To make sure we can still use just
`kernel::device::Device`, as `AsRef` implementation is added for it; this
is typical for types that is designed to use with `AsRef` anyway, for
example, `str` implements `AsRef<str>` and `Path` implements `AsRef<Path>`.

Signed-off-by: Gary Guo <gary@garyguo.net>
Link: https://patch.msgid.link/20260123175854.176735-1-gary@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

authored by

Gary Guo and committed by
Danilo Krummrich
a38cd1fe e62e48ad

+8 -1
+8 -1
rust/kernel/device.rs
··· 599 599 impl DeviceContext for CoreInternal {} 600 600 impl DeviceContext for Normal {} 601 601 602 + impl<Ctx: DeviceContext> AsRef<Device<Ctx>> for Device<Ctx> { 603 + #[inline] 604 + fn as_ref(&self) -> &Device<Ctx> { 605 + self 606 + } 607 + } 608 + 602 609 /// Convert device references to bus device references. 603 610 /// 604 611 /// Bus devices can implement this trait to allow abstractions to provide the bus device in ··· 725 718 macro_rules! dev_printk { 726 719 ($method:ident, $dev:expr, $($f:tt)*) => { 727 720 { 728 - ($dev).$method($crate::prelude::fmt!($($f)*)); 721 + $crate::device::Device::$method($dev.as_ref(), $crate::prelude::fmt!($($f)*)) 729 722 } 730 723 } 731 724 }