Skip to contentSkip to conversation

rust: regulator: use `to_result` for error handling

PATCH In review Onur Özkan 4 participants 4 messages Aug 21, 2025 – Aug 28, 2025

Subsystems (2)
lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH 1/1 rust: regulator: 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/regulator.rs | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
rust/kernel/regulator.rs +2 -5
index 65f3a125348f..73ad4ad4747d 100644
@@ -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

reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 30 lines · Onur Özkan

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

---
rust/kernel/regulator.rs | 7 ++-----

1 file changed, 2 insertions(+), 5 deletions(-)
rust/kernel/regulator.rs +2 -5
index 65f3a125348f..73ad4ad4747d 100644
@@ -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>> {
Quoted 4 lines

—
2.50.0

#2 lore raw
In reply to Onur Özkan

rust/kernel/regulator.rs pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Result · line 271

Quoted 8 lines · Onur Özkan
rust/kernel/regulator.rs +2 -5
-        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

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

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