rust: device: add device name method

Add a name() method to the `Device` type, which returns a CStr that
contains the device name.

Signed-off-by: Timur Tabi <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Reviewed-by: Alexandre Courbot <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Tested-by: John Hubbard <[email protected]>
Tested-by: Eliot Courtney <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Danilo Krummrich <[email protected]>
This commit is contained in:
Timur Tabi authored and Danilo Krummrich committed 2026-03-24 23:51:23 +01:00
1 parent 651c27d6a7
commit d35ae50c5f
2 files changed
+16

No files matched your search

+5
View File
@@ -25,3 +25,8 @@ __rust_helper void rust_helper_dev_set_drvdata(struct device *dev, void *data)
{
dev_set_drvdata(dev, data);
}
__rust_helper const char *rust_helper_dev_name(const struct device *dev)
{
return dev_name(dev);
}
+11
View File
@@ -489,6 +489,17 @@ impl<Ctx: DeviceContext> Device<Ctx> {
// defined as a `#[repr(transparent)]` wrapper around `fwnode_handle`.
Some(unsafe { &*fwnode_handle.cast() })
}
/// Returns the name of the device.
///
/// This is the kobject name of the device, or its initial name if the kobject is not yet
/// available.
#[inline]
pub fn name(&self) -> &CStr {
// SAFETY: By its type invariant `self.as_raw()` is a valid pointer to a `struct device`.
// The returned string is valid for the lifetime of the device.
unsafe { CStr::from_char_ptr(bindings::dev_name(self.as_raw())) }
}
}
// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic