Re: [PATCH] rust: opp: simplify callers of `to_c_str_array`

From: Tamir Duberstein

Date: Wed Oct 22 2025 - 07:23:08 EST


Hi Viresh,

On Wed, Oct 22, 2025 at 1:40 AM Viresh Kumar <viresh.kumar@xxxxxxxxxx> wrote:
>
> Hi Tamir,
>
> On 20-10-25, 09:07, Tamir Duberstein wrote:
> > Use `Option` combinators to make this a bit less noisy.
> >
> > Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxxx>
> > ---
> > rust/kernel/opp.rs | 25 ++++++++-----------------
> > 1 file changed, 8 insertions(+), 17 deletions(-)
> >
> > diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
> > index 9d6c58178a6f..b84786f45522 100644
> > --- a/rust/kernel/opp.rs
> > +++ b/rust/kernel/opp.rs
> > @@ -443,23 +443,14 @@ pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
> > ///
> > /// The returned [`ConfigToken`] will remove the configuration when dropped.
> > pub fn set(self, dev: &Device) -> Result<ConfigToken> {
> > - let (_clk_list, clk_names) = match &self.clk_names {
> > - Some(x) => {
> > - let list = to_c_str_array(x)?;
> > - let ptr = list.as_ptr();
> > - (Some(list), ptr)
> > - }
> > - None => (None, ptr::null()),
> > - };
> > -
> > - let (_regulator_list, regulator_names) = match &self.regulator_names {
> > - Some(x) => {
> > - let list = to_c_str_array(x)?;
> > - let ptr = list.as_ptr();
> > - (Some(list), ptr)
> > - }
> > - None => (None, ptr::null()),
> > - };
> > + let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
> > + let clk_names = clk_names.map_or(ptr::null(), |c| c.as_ptr());
> > + let regulator_names = self
> > + .regulator_names
> > + .as_deref()
> > + .map(to_c_str_array)
> > + .transpose()?;
> > + let regulator_names = regulator_names.map_or(ptr::null(), |c| c.as_ptr());
>
> I had to keep _clk_list and _regulator_list earlier to make sure the list isn't
> freed while its pointer is still used (sent to dev_pm_opp_set_config()). Won't
> this change cause an issue here ?

I believe the `{clk,regulator}_names` vector bindings remain alive for
the whole scope, even if they are shadowed. See
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=800b334c514c2024d7b5e47fc54c1f2d.

Cheers.
Tamir