Skip to contentSkip to conversation

rust: opp: use to_result for error handling

PATCH Applied Onur Özkan 3 participants 6 messages Aug 21, 2025 – Aug 25, 2025

Subsystems (2)
lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH 1/1 rust: opp: use to_result for error handling

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/opp.rs | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
rust/kernel/opp.rs +5 -11
index 08126035d2c6..9d79c2816af5 100644
@@ -12,7 +12,7 @@
     clk::Hertz,
     cpumask::{Cpumask, CpumaskVar},
     device::Device,
-    error::{code::*, from_err_ptr, from_result, to_result, Error, Result, VTABLE_DEFAULT_ERROR},
+    error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
     ffi::c_ulong,
     prelude::*,
     str::CString,
@@ -500,11 +500,8 @@ pub fn set(self, dev: &Device) -> Result<ConfigToken> {
         // requirements. The OPP core guarantees not to access fields of [`Config`] after this call
         // and so we don't need to save a copy of them for future use.
         let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ConfigToken(ret))
-        }
+
+        to_result(ret).map(|()| ConfigToken(ret))
     }
 
     /// Config's clk callback.
@@ -713,11 +710,8 @@ pub fn opp_count(&self) -> Result<u32> {
         // SAFETY: The requirements are satisfied by the existence of [`Device`] and its safety
         // requirements.
         let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ret as u32)
-        }
+
+        to_result(ret).map(|()| ret as u32)
     }
 
     /// Returns max clock latency (in nanoseconds) of the [`OPP`]s in the [`Table`].

--
2.50.0

#1 lore raw
Quoted 37 lines · Onur Özkan

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/opp.rs | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
rust/kernel/opp.rs +3 -6
index 08126035d2c6..9d79c2816af5 100644
@@ -12,7 +12,7 @@
     clk::Hertz,
     cpumask::{Cpumask, CpumaskVar},
     device::Device,
-    error::{code::*, from_err_ptr, from_result, to_result, Error, Result, VTABLE_DEFAULT_ERROR},
+    error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
     ffi::c_ulong,
     prelude::*,
     str::CString,
@@ -500,11 +500,8 @@ pub fn set(self, dev: &Device) -> Result<ConfigToken> {
         // requirements. The OPP core guarantees not to access fields of [`Config`] after this call
         // and so we don't need to save a copy of them for future use.
         let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ConfigToken(ret))
-        }
+
+        to_result(ret).map(|()| ConfigToken(ret))
     }
 
     /// Config's clk callback.
@@ -713,11 +710,8 @@ pub fn opp_count(&self) -> Result<u32> {
         // SAFETY: The requirements are satisfied by the existence of [`Device`] and its safety

rust/kernel/opp.rs pub fn opp_count(&self) -> Result<u32> { · line 715

Quoted 10 lines
rust/kernel/opp.rs +2 -5
         // requirements.
         let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ret as u32)
-        }
+
+        to_result(ret).map(|()| ret as u32)
     }

I would get rid of ret as well in both the cases, like it is done by other users of to_result().

Signature · 1 line
viresh
author reviewer
#2 lore raw
Quoted 46 lines · On Thu, 21 Aug 2025 14:51:37 +0530 Viresh Kumar <[email protected]> wrote:
Quoted 45 lines · Onur Özkan

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/opp.rs | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
rust/kernel/opp.rs +1 -1
index 08126035d2c6..9d79c2816af5 100644
@@ -12,7 +12,7 @@
     clk::Hertz,
     cpumask::{Cpumask, CpumaskVar},
     device::Device,
-    error::{code::*, from_err_ptr, from_result, to_result, Error,
 Result, VTABLE_DEFAULT_ERROR},
+    error::{code::*, from_err_ptr, from_result, to_result, Result,
 VTABLE_DEFAULT_ERROR}, ffi::c_ulong,
     prelude::*,

str::CString,

rust/kernel/opp.rs +0 -0
@@ -500,11 +500,8 @@ pub fn set(self, dev: &Device) ->

Result<ConfigToken> { // requirements. The OPP core guarantees not to access fields of [Config] after this call // and so we don't need to save a copy of them for future use. let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };

rust/kernel/opp.rs +2 -5
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ConfigToken(ret))
-        }
+
+        to_result(ret).map(|()| ConfigToken(ret))
     }
 
     /// Config's clk callback.
@@ -713,11 +710,8 @@ pub fn opp_count(&self) -> Result<u32> {
         // SAFETY: The requirements are satisfied by the existence

of [Device] and its safety // requirements.

         let ret = unsafe {

bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };

rust/kernel/opp.rs +0 -3
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
Quoted 9 lines

rust/kernel/opp.rs pub fn opp_count(&self) -> Result<u32> { · line 715

Quoted 5 lines
rust/kernel/opp.rs +2 -2
-            Ok(ret as u32)
-        }
+
+        to_result(ret).map(|()| ret as u32)
     }

I would get rid of ret as well in both the cases, like it is done by other users of to_result().

ret is returned at the end unlike other to_result usages in other functions.

-Onur

#3 lore raw
Quoted 2 lines · Onur Özkan

ret is returned at the end unlike other to_result usages in other functions.

Ahh, I missed that to_result() doesn't return ret at all.

Signature · 1 line
viresh
Applied
#4 lore raw
In reply to Onur Özkan
Quoted 7 lines · Onur Özkan

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/opp.rs | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

Applied. Thanks.

Signature · 1 line
viresh
Reviewed-by on v1 1/1
#5 lore raw
In reply to Onur Özkan
Quoted 50 lines · Onur Özkan

Simplifies error handling by replacing the manual check of the return value with the to_result helper.

---
 rust/kernel/opp.rs | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
rust/kernel/opp.rs +5 -12
index 08126035d2c6..9d79c2816af5 100644
@@ -12,7 +12,7 @@
     clk::Hertz,
     cpumask::{Cpumask, CpumaskVar},
     device::Device,
-    error::{code::*, from_err_ptr, from_result, to_result, Error, Result, VTABLE_DEFAULT_ERROR},
+    error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
     ffi::c_ulong,
     prelude::*,
     str::CString,
@@ -500,11 +500,8 @@ pub fn set(self, dev: &Device) -> Result<ConfigToken> {
         // requirements. The OPP core guarantees not to access fields of [`Config`] after this call
         // and so we don't need to save a copy of them for future use.
         let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ConfigToken(ret))
-        }
+
+        to_result(ret).map(|()| ConfigToken(ret))
     }
 
     /// Config's clk callback.
@@ -713,11 +710,8 @@ pub fn opp_count(&self) -> Result<u32> {
         // SAFETY: The requirements are satisfied by the existence of [`Device`] and its safety
         // requirements.
         let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ret as u32)
-        }
+
+        to_result(ret).map(|()| ret as u32)
     }
 
     /// Returns max clock latency (in nanoseconds) of the [`OPP`]s in the [`Table`].
--
Quoted 1 line

2.50.0