Skip to content

Commit a4c9f71

Browse files
author
Danilo Krummrich
committed
rust: device: implement Device::parent()
Device::parent() returns a reference to the device' parent device, if any. Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 9647b6c commit a4c9f71

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rust/kernel/device.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ impl<Ctx: DeviceContext> Device<Ctx> {
6767
self.0.get()
6868
}
6969

70+
/// Returns a reference to the parent device, if any.
71+
#[expect(unused)]
72+
pub(crate) fn parent(&self) -> Option<&Self> {
73+
// SAFETY:
74+
// - By the type invariant `self.as_raw()` is always valid.
75+
// - The parent device is only ever set at device creation.
76+
let parent = unsafe { (*self.as_raw()).parent };
77+
78+
if parent.is_null() {
79+
None
80+
} else {
81+
// SAFETY:
82+
// - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
83+
// - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
84+
// reference count of its parent.
85+
Some(unsafe { Self::as_ref(parent) })
86+
}
87+
}
88+
7089
/// Convert a raw C `struct device` pointer to a `&'a Device`.
7190
///
7291
/// # Safety

0 commit comments

Comments
 (0)