rust: opp: use to_result for error handling
Subsystems (2)
Onur Özkan
reviewerPATCH 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
@@ -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 (retas u32 ) - } + +to_result (ret).map (|()| retas u32 ) }/// Returns max clock latency (in nanoseconds) of the [`OPP`]s in the [`Table`].
--
2.50.0
Quoted 37 lines · Onur Özkan
Simplifies error handling by replacing the manual check of the return value with the
to_resulthelper.Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/opp.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)rust/kernel/opp.rs +3
-6@@ -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 (retas u32 ) - } + +to_result (ret).map (|()| retas 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
Onur Özkan
reviewerQuoted 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_resulthelper.Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/opp.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)rust/kernel/opp.rs +1
-1@@ -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 (retas u32 ) - } + +to_result (ret).map (|()| retas u32 ) }I would get rid of
retas well in both the cases, like it is done by other users ofto_result().
ret is returned at the end unlike other to_result usages in other functions.
-Onur
Quoted 2 lines · Onur Özkan
retis returned at the end unlike otherto_resultusages in other functions.
Ahh, I missed that to_result() doesn't return ret at all.
Signature · 1 line
viresh
Viresh Kumar
AppliedQuoted 7 lines · Onur Özkan
Simplifies error handling by replacing the manual check of the return value with the
to_resulthelper.Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/opp.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
Applied. Thanks.
Signature · 1 line
viresh
Quoted 50 lines · Onur Özkan
Simplifies error handling by replacing the manual check of the return value with the
to_resulthelper.Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/opp.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)rust/kernel/opp.rs +5
-12@@ -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 (retas u32 ) - } + +to_result (ret).map (|()| retas u32 ) }/// Returns max clock latency (in nanoseconds) of the [`OPP`]s in the [`Table`]. --
Quoted 1 line
2.50.0