[PATCH] rust: regulator: use `to_result` for error handling

Onur Özkan posted 1 patch 1 month, 1 week ago
rust/kernel/regulator.rs | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
[PATCH] rust: regulator: use `to_result` for error handling
Posted by Onur Özkan 1 month, 1 week ago
Simplifies error handling by replacing the manual check
of the return value with the `to_result` helper.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/regulator.rs | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs
index 65f3a125348f..73ad4ad4747d 100644
--- a/rust/kernel/regulator.rs
+++ b/rust/kernel/regulator.rs
@@ -267,11 +267,8 @@ pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Result
     pub fn get_voltage(&self) -> Result<Voltage> {
         // SAFETY: Safe as per the type invariants of `Regulator`.
         let voltage = unsafe { bindings::regulator_get_voltage(self.inner.as_ptr()) };
-        if voltage < 0 {
-            Err(kernel::error::Error::from_errno(voltage))
-        } else {
-            Ok(Voltage::from_microvolts(voltage))
-        }
+
+        to_result(voltage).map(|()| Voltage::from_microvolts(voltage))
     }

     fn get_internal(dev: &Device, name: &CStr) -> Result<Regulator<T>> {
--
2.50.0

Re: [PATCH] rust: regulator: use `to_result` for error handling
Posted by Mark Brown 1 month ago
On Thu, 21 Aug 2025 12:07:20 +0300, Onur Özkan wrote:
> Simplifies error handling by replacing the manual check
> of the return value with the `to_result` helper.
> 
> 

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next

Thanks!

[1/1] rust: regulator: use `to_result` for error handling
      commit: e2ab5f600bb01d3625d667d97b3eb7538e388336

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

Re: [PATCH] rust: regulator: use `to_result` for error handling
Posted by Miguel Ojeda 1 month ago
On Thu, Aug 21, 2025 at 11:08 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> -        if voltage < 0 {
> -            Err(kernel::error::Error::from_errno(voltage))
> -        } else {
> -            Ok(Voltage::from_microvolts(voltage))
> -        }
> +
> +        to_result(voltage).map(|()| Voltage::from_microvolts(voltage))

No big deal either way, but our usual idiom for this so far is:

    to_result(voltage)?;
    Ok(Voltage::from_microvolts(voltage))

which, in a way, uses early return to put the happy path in a less
indented / more visible place.

(I noticed a few similar patches lately)

Cheers,
Miguel
Re: [PATCH] rust: regulator: use `to_result` for error handling
Posted by Daniel Almeida 1 month, 1 week ago

> On 21 Aug 2025, at 06:07, Onur Özkan <work@onurozkan.dev> wrote:
> 
> Simplifies error handling by replacing the manual check
> of the return value with the `to_result` helper.
> 
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
> rust/kernel/regulator.rs | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs
> index 65f3a125348f..73ad4ad4747d 100644
> --- a/rust/kernel/regulator.rs
> +++ b/rust/kernel/regulator.rs
> @@ -267,11 +267,8 @@ pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Result
>     pub fn get_voltage(&self) -> Result<Voltage> {
>         // SAFETY: Safe as per the type invariants of `Regulator`.
>         let voltage = unsafe { bindings::regulator_get_voltage(self.inner.as_ptr()) };
> -        if voltage < 0 {
> -            Err(kernel::error::Error::from_errno(voltage))
> -        } else {
> -            Ok(Voltage::from_microvolts(voltage))
> -        }
> +
> +        to_result(voltage).map(|()| Voltage::from_microvolts(voltage))
>     }
> 
>     fn get_internal(dev: &Device, name: &CStr) -> Result<Regulator<T>> {
> —
> 2.50.0
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>