rust: cpufreq: streamline find_supply_names
Subsystems (2)
PATCH 1/1 rust: cpufreq: streamline find_supply_names
Remove local variables from find_supply_names() and use .and_then() with the more concise kernel::kvec![] macro, instead of KVec::with_capacity() followed by .push() and Some().
No functional changes intended.
--- drivers/cpufreq/rcpufreq_dt.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/cpufreq/rcpufreq_dt.rs +3 -7
@@ -28,15 +28,11 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {/// Finds supply name for the CPU from DT. fn find_supply_names (dev: &Device , cpu: cpu::CpuId ) ->Option <KVec <CString >> {// Try "cpu0" for older DTs, fallback to "cpu". -let name = (cpu.as_u32 () ==0 ) + (cpu.as_u32 () ==0 ) .then (||find_supply_name_exact (dev,"cpu0" )) .flatten () - .or_else (||find_supply_name_exact (dev,"cpu" ))?; - -let mut list =KVec ::with_capacity (1 ,GFP_KERNEL ).ok ()?; - list.push (name,GFP_KERNEL ).ok ()?; - -Some (list) + .or_else (||find_supply_name_exact (dev,"cpu" )) + .and_then (|name| kernel::kvec![name].ok ()) }/// Represents the cpufreq dt device.
Signature · 1 line
2.51.0
Quoted 24 lines · Thorsten Blum
Remove local variables from
find_supply_names()and use .and_then() with the more concise kernel::kvec![] macro, instead ofKVec::with_capacity()followed by .push() andSome().No functional changes intended.
Signed-off-by Thorsten Blum <[email protected]>--- drivers/cpufreq/rcpufreq_dt.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)drivers/cpufreq/rcpufreq_dt.rs +1
-2@@ -28,15 +28,11 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {/// Finds supply name for the CPU from DT. fn find_supply_names (dev: &Device , cpu: cpu::CpuId ) ->Option <KVec <CString >> {// Try "cpu0" for older DTs, fallback to "cpu". -let name = (cpu.as_u32 () ==0 ) + (cpu.as_u32 () ==0 ) .then (||find_supply_name_exact (dev,"cpu0" )) .flatten () - .or_else (||find_supply_name_exact (dev,"cpu" ))?;
drivers/cpufreq/rcpufreq_dt.rs fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> { · line 38
Quoted 10 lines
drivers/cpufreq/rcpufreq_dt.rs +2
-5- -let mut list =KVec ::with_capacity (1 ,GFP_KERNEL ).ok ()?; - list.push (name,GFP_KERNEL ).ok ()?; - -Some (list) + .or_else(|| find_supply_name_exact(dev, "cpu")) + .and_then (|name| kernel::kvec![name].ok ()) }/// Represents the cpufreq dt device.
Applied. Thanks.
Signature · 1 line
viresh
Alice Ryhl
reviewerQuoted 22 lines · Thorsten Blum
Remove local variables from
find_supply_names()and use .and_then() with the more concise kernel::kvec![] macro, instead ofKVec::with_capacity()followed by .push() andSome().No functional changes intended.
Signed-off-by Thorsten Blum <[email protected]>--- drivers/cpufreq/rcpufreq_dt.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)drivers/cpufreq/rcpufreq_dt.rs +1
-1@@ -28,15 +28,11 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {/// Finds supply name for the CPU from DT. fn find_supply_names (dev: &Device , cpu: cpu::CpuId ) ->Option <KVec <CString >> {// Try "cpu0" for older DTs, fallback to "cpu". -let name = (cpu.as_u32 () ==0 ) + (cpu.as_u32 () ==0 )
drivers/cpufreq/rcpufreq_dt.rs fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> { · line 35
Quoted 10 lines
drivers/cpufreq/rcpufreq_dt.rs +2
-6.then(|| find_supply_name_exact(dev, "cpu0")) .flatten () - .or_else (||find_supply_name_exact (dev,"cpu" ))?; - -let mut list =KVec ::with_capacity (1 ,GFP_KERNEL ).ok ()?; - list.push (name,GFP_KERNEL ).ok ()?; - -Some (list) + .or_else (||find_supply_name_exact (dev,"cpu" )) + .and_then (|name| kernel::kvec![name].ok ())
This is a pre-existing issue, but ... this treats allocation failure and non-existence the same way. That sounds wrong.
Alice
Viresh Kumar
maintainerQuoted 2 lines · Alice Ryhl
This is a pre-existing issue, but ... this treats allocation failure and non-existence the same way. That sounds wrong.
What about this over the current patch:
drivers/cpufreq/rcpufreq_dt.rs +9 -5
@@ -26,13 +26,17 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> { }/// Finds supply name for the CPU from DT. -fn find_supply_names (dev: &Device , cpu: cpu::CpuId ) ->Option <KVec <CString >> { +fn find_supply_names (dev: &Device , cpu: cpu::CpuId ) ->Result <Option <KVec <CString >>> {// Try "cpu0" for older DTs, fallback to "cpu". - (cpu.as_u32 () ==0 ) +let name = (cpu.as_u32 () ==0 ) .then (||find_supply_name_exact (dev,"cpu0" )) .flatten () - .or_else (||find_supply_name_exact (dev,"cpu" )) - .and_then (|name| kernel::kvec![name].ok ()) + .or_else (||find_supply_name_exact (dev,"cpu" )); + +Ok (match name { +None =>None , +Some (n) =>Some (kernel::kvec![n]?), + }) }/// Represents the cpufreq dt device.
@@ -68,7 +72,7 @@ fn init(policy: &mut cpufreq::Policy) -> Result<Self::PData> { mask.set (cpu); -let token =find_supply_names (dev, cpu) +let token =find_supply_names (dev, cpu)? .map(|names| { opp::Config::<Self>::new() .set_regulator_names(names)?
Signature · 1 line
viresh