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: implement Device::parent()

Device::parent() returns a reference to the device' parent device, if
any.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20250414131934.28418-3-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+19
+19
rust/kernel/device.rs
··· 67 67 self.0.get() 68 68 } 69 69 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 + 70 89 /// Convert a raw C `struct device` pointer to a `&'a Device`. 71 90 /// 72 91 /// # Safety