rust: regulator: use `to_result` for error handling
Subsystems (2)
Onur Özkan
reviewerPATCH 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
@@ -267,11 +267,8 @@ pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Resultpub 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
Quoted 30 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/regulator.rs| 7 ++-----1 file changed, 2 insertions(+), 5 deletions(-)rust/kernel/regulator.rs +2
-5@@ -267,11 +267,8 @@ pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Resultpub 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
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
Mark Brown
maintainerQuoted 4 lines · Onur Özkan
Simplifies error handling by replacing the manual check of the return value with the
to_resulthelper.
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