Re: [PATCH v3 15/16] rust: platform: add basic platform device / driver abstractions

From: Alice Ryhl
Date: Tue Oct 29 2024 - 09:16:42 EST


On Tue, Oct 22, 2024 at 11:33 PM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
> + /// Find the [`of::DeviceId`] within [`Driver::ID_TABLE`] matching the given [`Device`], if any.
> + fn of_match_device(pdev: &Device) -> Option<&of::DeviceId> {
> + let table = Self::ID_TABLE;
> +
> + // SAFETY:
> + // - `table` has static lifetime, hence it's valid for read,
> + // - `dev` is guaranteed to be valid while it's alive, and so is
> + // `pdev.as_dev().as_raw()`.
> + let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), pdev.as_dev().as_raw()) };
> +
> + if raw_id.is_null() {
> + None
> + } else {
> + // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct of_device_id` and
> + // does not add additional invariants, so it's safe to transmute.
> + Some(unsafe { &*raw_id.cast::<of::DeviceId>() })
> + }
> + }

Sorry if this has already been mentioned, but this method should
probably be marked #[cfg(CONFIG_OF)] so that you can use these
abstractions even if OF is disabled.

Alice