[PATCH v4 3/5] rust: driver: Consolidate `Adapter` methods using `#[cfg]`

Igor Korotin posted 5 patches 4 months ago
There is a newer version of this series
[PATCH v4 3/5] rust: driver: Consolidate `Adapter` methods using `#[cfg]`
Posted by Igor Korotin 4 months ago
Refactor the `acpi_id_info` and `of_id_info` methods in the `Adapter` trait
to reduce duplication. Previously, each method had two versions selected
via `#[cfg(...)]` and `#[cfg(not(...))]`. This change merges them into a
single method per case by using `#[cfg]` blocks within the method bodies.

Suggested-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
---
 rust/kernel/driver.rs | 76 +++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 38 deletions(-)

diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs
index 638f1d270af8..805f61bd4a50 100644
--- a/rust/kernel/driver.rs
+++ b/rust/kernel/driver.rs
@@ -147,30 +147,30 @@ pub trait Adapter {
     /// Returns the driver's private data from the matching entry in the [`acpi::IdTable`], if any.
     ///
     /// If this returns `None`, it means there is no match with an entry in the [`acpi::IdTable`].
-    #[cfg(CONFIG_ACPI)]
     fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
-        let table = Self::acpi_id_table()?;
+        #[cfg(not(CONFIG_ACPI))] {
+            let _ = dev;
+            return None;
+        }
 
-        // 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_ref().as_raw()`.
-        let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
+        #[cfg(CONFIG_ACPI)] {
+            let table = Self::acpi_id_table()?;
 
-        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.
-            let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
+            // 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_ref().as_raw()`.
+            let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
 
-            Some(table.info(<acpi::DeviceId as crate::device_id::RawDeviceId>::index(id)))
-        }
-    }
+            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.
+                let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
 
-    #[cfg(not(CONFIG_ACPI))]
-    #[allow(missing_docs)]
-    fn acpi_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
-        None
+                Some(table.info(<acpi::DeviceId as crate::device_id::RawDeviceId>::index(id)))
+            }
+        }
     }
 
     /// The [`of::IdTable`] of the corresponding driver.
@@ -179,30 +179,30 @@ fn acpi_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
     /// Returns the driver's private data from the matching entry in the [`of::IdTable`], if any.
     ///
     /// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`].
-    #[cfg(CONFIG_OF)]
     fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
-        let table = Self::of_id_table()?;
+        #[cfg(not(CONFIG_OF))] {
+            let _ = dev;
+            return None;
+        }
 
-        // 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_ref().as_raw()`.
-        let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
+        #[cfg(CONFIG_OF)] {
+            let table = Self::of_id_table()?;
 
-        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.
-            let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
+            // 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_ref().as_raw()`.
+            let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
 
-            Some(table.info(<of::DeviceId as crate::device_id::RawDeviceId>::index(id)))
-        }
-    }
+            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.
+                let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
 
-    #[cfg(not(CONFIG_OF))]
-    #[allow(missing_docs)]
-    fn of_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
-        None
+                Some(table.info(<of::DeviceId as crate::device_id::RawDeviceId>::index(id)))
+            }
+        }
     }
 
     /// Returns the driver's private data from the matching entry of any of the ID tables, if any.
-- 
2.43.0
Re: [PATCH v4 3/5] rust: driver: Consolidate `Adapter` methods using `#[cfg]`
Posted by Danilo Krummrich 4 months ago
On Tue, Jun 10, 2025 at 04:05:38PM +0100, Igor Korotin wrote:
> Refactor the `acpi_id_info` and `of_id_info` methods in the `Adapter` trait
> to reduce duplication. Previously, each method had two versions selected
> via `#[cfg(...)]` and `#[cfg(not(...))]`. This change merges them into a
> single method per case by using `#[cfg]` blocks within the method bodies.
> 
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
> ---
>  rust/kernel/driver.rs | 76 +++++++++++++++++++++----------------------
>  1 file changed, 38 insertions(+), 38 deletions(-)
> 
> diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs
> index 638f1d270af8..805f61bd4a50 100644
> --- a/rust/kernel/driver.rs
> +++ b/rust/kernel/driver.rs
> @@ -147,30 +147,30 @@ pub trait Adapter {
>      /// Returns the driver's private data from the matching entry in the [`acpi::IdTable`], if any.
>      ///
>      /// If this returns `None`, it means there is no match with an entry in the [`acpi::IdTable`].
> -    #[cfg(CONFIG_ACPI)]
>      fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
> -        let table = Self::acpi_id_table()?;
> +        #[cfg(not(CONFIG_ACPI))] {
> +            let _ = dev;
> +            return None;
> +        }
>  
> -        // 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_ref().as_raw()`.
> -        let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
> +        #[cfg(CONFIG_ACPI)] {
> +            let table = Self::acpi_id_table()?;
>  
> -        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.
> -            let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
> +            // 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_ref().as_raw()`.
> +            let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
>  
> -            Some(table.info(<acpi::DeviceId as crate::device_id::RawDeviceId>::index(id)))
> -        }
> -    }
> +            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.
> +                let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
>  
> -    #[cfg(not(CONFIG_ACPI))]
> -    #[allow(missing_docs)]
> -    fn acpi_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
> -        None
> +                Some(table.info(<acpi::DeviceId as crate::device_id::RawDeviceId>::index(id)))
> +            }
> +        }
>      }

It seems there has been a misunderstanding, the idea was to refactor
Adapter::of_id_info() in one patch and introduce Adapter::acpi_id_info() in a
subsequent patch already being the suggested implementation by Benno.

We try to avoid introducing code, just to change it again in a subsequent patch.
Re: [PATCH v4 3/5] rust: driver: Consolidate `Adapter` methods using `#[cfg]`
Posted by Igor Korotin 4 months ago
On Tue, Jun 10, 2025 at 4:15 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> On Tue, Jun 10, 2025 at 04:05:38PM +0100, Igor Korotin wrote:
> > Refactor the `acpi_id_info` and `of_id_info` methods in the `Adapter` trait
> > to reduce duplication. Previously, each method had two versions selected
> > via `#[cfg(...)]` and `#[cfg(not(...))]`. This change merges them into a
> > single method per case by using `#[cfg]` blocks within the method bodies.
> >
> > Suggested-by: Benno Lossin <lossin@kernel.org>
> > Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
> > ---
> >  rust/kernel/driver.rs | 76 +++++++++++++++++++++----------------------
> >  1 file changed, 38 insertions(+), 38 deletions(-)
> >
> > diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs
> > index 638f1d270af8..805f61bd4a50 100644
> > --- a/rust/kernel/driver.rs
> > +++ b/rust/kernel/driver.rs
> > @@ -147,30 +147,30 @@ pub trait Adapter {
> >      /// Returns the driver's private data from the matching entry in the [`acpi::IdTable`], if any.
> >      ///
> >      /// If this returns `None`, it means there is no match with an entry in the [`acpi::IdTable`].
> > -    #[cfg(CONFIG_ACPI)]
> >      fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
> > -        let table = Self::acpi_id_table()?;
> > +        #[cfg(not(CONFIG_ACPI))] {
> > +            let _ = dev;
> > +            return None;
> > +        }
> >
> > -        // 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_ref().as_raw()`.
> > -        let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
> > +        #[cfg(CONFIG_ACPI)] {
> > +            let table = Self::acpi_id_table()?;
> >
> > -        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.
> > -            let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
> > +            // 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_ref().as_raw()`.
> > +            let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
> >
> > -            Some(table.info(<acpi::DeviceId as crate::device_id::RawDeviceId>::index(id)))
> > -        }
> > -    }
> > +            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.
> > +                let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
> >
> > -    #[cfg(not(CONFIG_ACPI))]
> > -    #[allow(missing_docs)]
> > -    fn acpi_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
> > -        None
> > +                Some(table.info(<acpi::DeviceId as crate::device_id::RawDeviceId>::index(id)))
> > +            }
> > +        }
> >      }
>
> It seems there has been a misunderstanding, the idea was to refactor
> Adapter::of_id_info() in one patch and introduce Adapter::acpi_id_info() in a
> subsequent patch already being the suggested implementation by Benno.
>
> We try to avoid introducing code, just to change it again in a subsequent patch.

That does make perfect sense. My bad. I'll reorder changes in the v5.

Sorry for the mess. Thanks for the review.

Best Regards
Igor