Skip to contentSkip to conversation

rust: simplify `Adapter::id_info`

PATCH In review Onur Özkan 6 participants 11 messages Jan 17 – Jan 24

Subsystems (2)
lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

v2 cover rust: simplify `Adapter::id_info`

Changes in v2:
- Rebased to cover new changes in Adapter::id_info since v1.

Onur Özkan (1):
rust: simplify Adapter::id_info

 rust/kernel/driver.rs | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
Signature · 1 line
2.51.2
author reviewer
#1 lore raw

PATCH v2 1/1 rust: simplify `Adapter::id_info`

id_info() checks ACPI first and falls back to OF.

This replaces the unnecessarily verbose approach with a simple or_else() chain and drops temporary variables.

No functional change intended.

---
 rust/kernel/driver.rs | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
rust/kernel/driver.rs +1 -11
index 649d06468f41..6cef792d54e4 100644
@@ -307,16 +307,6 @@ fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
     /// If this returns `None`, it means that there is no match in any of the ID tables directly
     /// associated with a [`device::Device`].
     fn id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
-        let id = Self::acpi_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        let id = Self::of_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        None
+        Self::acpi_id_info(dev).or_else(|| Self::of_id_info(dev))
     }
 }
Signature · 1 line
2.51.2
maintainer
#2 lore raw
Quoted 22 lines · Onur Özkan

id_info() checks ACPI first and falls back to OF.

This replaces the unnecessarily verbose approach with a simple or_else() chain and drops temporary variables.

No functional change intended.

---
 rust/kernel/driver.rs | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
rust/kernel/driver.rs +0 -2
index 649d06468f41..6cef792d54e4 100644
@@ -307,16 +307,6 @@ fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
     /// If this returns `None`, it means that there is no match in any of the ID tables directly
     /// associated with a [`device::Device`].
     fn id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
-        let id = Self::acpi_id_info(dev);
-        if id.is_some() {

rust/kernel/driver.rs fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> { · line 310

Quoted 10 lines
rust/kernel/driver.rs +1 -9
-            return id;
-        }
-
-        let id = Self::of_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        None
+        Self::acpi_id_info(dev).or_else(|| Self::of_id_info(dev))

Have we already started the game of "rust golf" on the kernel? The original code here is much easier to read, and the compiler should produce the same thing for both, right?

thanks,

greg k-h

author reviewer
#3 lore raw
Quoted 39 lines · On Sat, 17 Jan 2026 11:00:11 +0100 Greg KH <[email protected]> wrote:
Quoted 23 lines · Onur Özkan

id_info() checks ACPI first and falls back to OF.

This replaces the unnecessarily verbose approach with a simple or_else() chain and drops temporary variables.

No functional change intended.

---
 rust/kernel/driver.rs | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
rust/kernel/driver.rs +0 -0
index 649d06468f41..6cef792d54e4 100644
@@ -307,16 +307,6 @@ fn of_id_info(dev: &device::Device) ->

Option<&'static Self::IdInfo> { /// If this returns None, it means that there is no match in any of the ID tables directly /// associated with a [device::Device]. fn id_info(dev:
&device::Device) -> Option<&'static Self::IdInfo> {

rust/kernel/driver.rs +0 -2
-        let id = Self::acpi_id_info(dev);
-        if id.is_some() {

rust/kernel/driver.rs fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> { · line 310

Quoted 10 lines
rust/kernel/driver.rs +1 -9
-            return id;
-        }
-
-        let id = Self::of_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        None
+        Self::acpi_id_info(dev).or_else(|| Self::of_id_info(dev))

Have we already started the game of "rust golf" on the kernel? The original code here is much easier to read, and the compiler should produce the same thing for both, right?

Quoted 3 lines

thanks,

greg k-h

Hi Greg,

I don't know what "rust golf" means, the main motivation here was to use a more idiomatic Rust pattern.

Functions called in Adapter::id_info already return the same Result type and it's quite annoying(IMO) to do "if x.is_some() { return x }" in cases like this. Clippy would argue on many cases similar to this. Readability is subjective, but to me the new code is slightly simpler to follow (as it does the "try X, then Y" thing in a shorter way).

That said, I don't have strong feelings on the new code. The main motivation was to drop the "annoying" code with more idiomatic one, but seems like not everyone agrees with it. Feel free to ignore the patch if you prefer the original code.

Regards,
Onur Özkan

maintainer
#4 lore raw
Quoted 42 lines · Onur Özkan
Quoted 39 lines · On Sat, 17 Jan 2026 11:00:11 +0100 Greg KH <[email protected]> wrote:
Quoted 23 lines · Onur Özkan

id_info() checks ACPI first and falls back to OF.

This replaces the unnecessarily verbose approach with a simple or_else() chain and drops temporary variables.

No functional change intended.

---
 rust/kernel/driver.rs | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
rust/kernel/driver.rs +0 -0
index 649d06468f41..6cef792d54e4 100644
@@ -307,16 +307,6 @@ fn of_id_info(dev: &device::Device) ->

Option<&'static Self::IdInfo> { /// If this returns None, it means that there is no match in any of the ID tables directly /// associated with a [device::Device]. fn id_info(dev:
&device::Device) -> Option<&'static Self::IdInfo> {

rust/kernel/driver.rs +0 -2
-        let id = Self::acpi_id_info(dev);
-        if id.is_some() {

rust/kernel/driver.rs fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> { · line 310

Quoted 10 lines
rust/kernel/driver.rs +1 -9
-            return id;
-        }
-
-        let id = Self::of_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        None
+        Self::acpi_id_info(dev).or_else(|| Self::of_id_info(dev))

Have we already started the game of "rust golf" on the kernel? The original code here is much easier to read, and the compiler should produce the same thing for both, right?

Quoted 8 lines
Quoted 3 lines

thanks,

greg k-h

Hi Greg,

I don't know what "rust golf" means, the main motivation here was to use a more idiomatic Rust pattern.

I was referring to this:
https://en.wikipedia.org/wiki/Code_golf
sorry for not being explicit :)

Quoted 5 lines

Functions called in Adapter::id_info already return the same Result type and it's quite annoying(IMO) to do "if x.is_some() { return x }" in cases like this. Clippy would argue on many cases similar to this. Readability is subjective, but to me the new code is slightly simpler to follow (as it does the "try X, then Y" thing in a shorter way).

Does clippy complain about this one?

Quoted 4 lines

That said, I don't have strong feelings on the new code. The main motivation was to drop the "annoying" code with more idiomatic one, but seems like not everyone agrees with it. Feel free to ignore the patch if you prefer the original code.

I don't have strong feelings either, but the original is "easier" for those of us used to C code.

thanks,

greg k-h

maintainer
#5 lore raw
Quoted 1 line · Greg KH

Does clippy complain about this one?

No, it does not.

Quoted 2 lines

I don't have strong feelings either, but the original is "easier" for those of us used to C code.

I think it's a matter of preference. Personally, I like those functional characteristics of Rust and the corresponding possibility of compact expressions as long as it is not overdone.

This case seems pretty simple though. :)

In comparison, this is code from allocating the level 1 page directory for the GSP (radix3) firmware, which is probably a bit too much.

	level1 <- {
	    // Allocate the level 1 page table, map the level 2 page table onto it, and map it
	    // into the device address space.
	    VVec::<u8>::with_capacity(
	        level2.iter().count() * size_of::<u64>(),
	        GFP_KERNEL,
	    )
	    .map_err(|_| ENOMEM)
	    .and_then(|level1| map_into_lvl(&level2, level1))
	    .map(|level1| SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KERNEL))?
	},
reviewer
#6 lore raw
Quoted 17 lines · Danilo Krummrich
Quoted 1 line · Greg KH

Does clippy complain about this one?

No, it does not.

Quoted 2 lines

I don't have strong feelings either, but the original is "easier" for those of us used to C code.

I think it's a matter of preference. Personally, I like those functional characteristics of Rust and the corresponding possibility of compact expressions as long as it is not overdone.

This case seems pretty simple though. :)

In comparison, this is code from allocating the level 1 page directory for the GSP (radix3) firmware, which is probably a bit too much.

Quoted 10 lines
	level1 <- {
	    // Allocate the level 1 page table, map the level 2 page table onto it, and map it
	    // into the device address space.
	    VVec::<u8>::with_capacity(
	        level2.iter().count() * size_of::<u64>(),
	        GFP_KERNEL,
	    )
	    .map_err(|_| ENOMEM)
	    .and_then(|level1| map_into_lvl(&level2, level1))
	    .map(|level1| SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KERNEL))?

For this specific instance, it looks like it could just be

    let level1 = VVec::<u8>::with_capacity(
        level2.iter().count() * size_of::<u64>(),
        GFP_KERNEL,
    )?;
    let level1 = map_into_lvl(&level2, level1)?;
    SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KENREL)?

which IMO looks clearer.

I suspect what people want is Elixir's pipe operator so the above is like this (non-existent, imaginary syntax):

    VVec::<u8>::with_capacity(
        level2.iter().count() * size_of::<u64>(),
        GFP_KERNEL,
    )?
    |> map_into_lvl(&level2, _)?
    |> SGTable::new(dev, _, DataDirection::ToDevice, GFP_KERNEL)?

But I think keeping things as Result (and not using ?) just to use the functional combinatiors is probably a bad idea.

Best,
Gary

#7 lore raw
Quoted 22 lines · Gary Guo

For this specific instance, it looks like it could just be

    let level1 = VVec::<u8>::with_capacity(
        level2.iter().count() * size_of::<u64>(),
        GFP_KERNEL,
    )?;
    let level1 = map_into_lvl(&level2, level1)?;
    SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KENREL)?

which IMO looks clearer.

I suspect what people want is Elixir's pipe operator so the above is like this (non-existent, imaginary syntax):

    VVec::<u8>::with_capacity(
        level2.iter().count() * size_of::<u64>(),
        GFP_KERNEL,
    )?
    |> map_into_lvl(&level2, _)?
    |> SGTable::new(dev, _, DataDirection::ToDevice, GFP_KERNEL)?
Quoted 2 lines

But I think keeping things as Result (and not using ?) just to use the functional combinatiors is probably a bad idea.

Agreed -- I think when ? is applicable, it can make things cleaner to get things "out" of the type (and, after all, we did get the "symbol soup" complaints about closures in the past... :).

Cheers,
Miguel

#8 lore raw
In reply to Danilo Krummrich
Quoted 5 lines · Danilo Krummrich
Quoted 1 line · Greg KH

Does clippy complain about this one?

No, it does not.

Quoted 8 lines
Quoted 2 lines

I don't have strong feelings either, but the original is "easier" for those of us used to C code.

I think it's a matter of preference. Personally, I like those functional characteristics of Rust and the corresponding possibility of compact expressions as long as it is not overdone.

This case seems pretty simple though. :)

IIUC for this patch the consensus seems to be in favor of preserving the original C-like style, but FWIW I found the new style proposed by Onur easier to read.

I understand that it may not be intuitive unless one has some familiarity with Rust, but it is a common way to write things in that language and since it has graduated from its experimental status, it doesn't seem judicious to me to artificially restrict its features.

author reviewer
#9 lore raw
In reply to Greg KH
Quoted 48 lines · On Sat, 17 Jan 2026 13:07:54 +0100 Greg KH <[email protected]> wrote:
Quoted 47 lines · Onur Özkan
Quoted 43 lines · On Sat, 17 Jan 2026 11:00:11 +0100 Greg KH <[email protected]> wrote:
Quoted 33 lines · Onur Özkan

id_info() checks ACPI first and falls back to OF.

This replaces the unnecessarily verbose approach with a simple or_else() chain and drops temporary variables.

No functional change intended.

---
 rust/kernel/driver.rs | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
rust/kernel/driver.rs +0 -0
index 649d06468f41..6cef792d54e4 100644
@@ -307,16 +307,6 @@ fn of_id_info(dev: &device::Device) ->

Option<&'static Self::IdInfo> { /// If this returns None, it means that there is no match in any of the ID tables directly /// associated with a [device::Device]. fn id_info(dev:
&device::Device) -> Option<&'static Self::IdInfo> {

rust/kernel/driver.rs +1 -11
-        let id = Self::acpi_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        let id = Self::of_id_info(dev);
-        if id.is_some() {
-            return id;
-        }
-
-        None
+        Self::acpi_id_info(dev).or_else(||
Quoted 1 line

Self::of_id_info(dev))

Have we already started the game of "rust golf" on the kernel? The original code here is much easier to read, and the compiler should produce the same thing for both, right?

thanks,

greg k-h

Quoted 9 lines
Quoted 4 lines

Hi Greg,

I don't know what "rust golf" means, the main motivation here was to use a more idiomatic Rust pattern.

I was referring to this:
https://en.wikipedia.org/wiki/Code_golf
sorry for not being explicit :)

I didn't know this term before - good to know :)

Quoted 9 lines
Quoted 6 lines

Functions called in Adapter::id_info already return the same Result type and it's quite annoying(IMO) to do "if x.is_some() { return x }" in cases like this. Clippy would argue on many cases similar to this. Readability is subjective, but to me the new code is slightly simpler to follow (as it does the "try X, then Y" thing in a shorter way).

Does clippy complain about this one?

No, it doesn't.

Quoted 8 lines
Quoted 4 lines

That said, I don't have strong feelings on the new code. The main motivation was to drop the "annoying" code with more idiomatic one, but seems like not everyone agrees with it. Feel free to ignore the patch if you prefer the original code.

I don't have strong feelings either, but the original is "easier" for those of us used to C code.

Fair point, you are right.

Thanks,
Onur Özkan

Quoted 3 lines

thanks,

greg k-h

#10 lore raw
In reply to Onur Özkan
Quoted 3 lines · Onur Özkan

I don't know what "rust golf" means, the main motivation here was to use a more idiomatic Rust pattern.

I have seen some patches recently arguing certain methods being more idiomatic etc.

I would like to caution that the fact that a method exists doesn't make it a good idea to automatically apply it in every case, and I don't think using a maximally functional style is actually idiomatic in Rust to begin with.

Even if it were actually more idiomatic, the idiomatic argument isn't very strong anyway, i.e. what matters is making the code look best, and the change needs to be a fair improvement -- otherwise it may not be worth it to shuffle the code around etc.

In this particular instance, it is a significant reduction in lines, indeed, but I don't think the original code is "annoying" at all -- it was already easy to follow.

In general, I think authors and maintainers should pick whatever style they think suits best for each situation, since both can be overdone.

Cheers,
Miguel