Re: [PATCH RFC] soc: qcom: socinfo: Re-implement in Rust

From: Gary Guo

Date: Mon Dec 15 2025 - 07:11:55 EST


On Sat, 13 Dec 2025 08:58:56 -0800
Matthew Maurer <mmaurer@xxxxxxxxxx> wrote:

> Some options:
> 1. Make holes explicit
> ```
> pub(crate) const PMIC_MODELS: &[Option<&'str>] = &[
> Some("foo"),
> Some("bar"),
> None,
> Some("baz"),
> // ...
> };
> ```
> This is the one I'd suggest if we want to get rid of the 92. It has
> the downside of some extra explicit `None` entries, but the array
> isn't *that* sparse.
>
> 2. Factor out 92 into a constant.
> 3. Define the constant in terms of index/value pairs instead. I could
> use `const`-time code to produce the array we want:
> ```
> const PMIC_ENTRIES: &[(usize, &str)] = &[(1, "foo"), (9, "bar"), (42, "baz")];
>
> const PMIC_MODELS_LEN: usize = {
> let mut max = 0;
> let mut i = 0;
> while i < PMIC_ENTRIES.len() {
> if PMIC_ENTRIES[i].0 > max {
> max = PMIC_ENTRIES[i].0;
> }
> i += 1;
> }
> max + 1
> };
>
> pub const PMIC_MODELS: [Option<&'static str>; PMIC_MODELS_LEN] = {
> let mut models = [None; PMIC_MODELS_LEN];
> let mut i = 0;
> while i < PMIC_ENTRIES.len() {
> let (idx, val) = PMIC_ENTRIES[i];
> models[idx] = Some(val);
> i += 1;
> }
> models
> };
> ```
> (The slightly icky looking loops are because not all features are
> available in const mode.)
> This seems a bit overkill for what's going on.

How about making a macro for this and make it available from kernel crate?
Looks like this should do what you need?


/// Create a sparse array of `[Option<T>; _]`.
macro_rules! sparse_array {
($(
$index:literal: $value:expr
),* $(,)?) => {{
const SIZE: usize = {
let mut size = 0;
$(if $index >= size {
size = $index + 1;
})*
size
};

const {
let mut arr = [None; SIZE];
$(arr[$index] = Some($value);)*
arr
}
}}
}

fn main() {
const EXAMPLE: &[Option<u32>] = &sparse_array! {
0: 1,
5: 2,
};
println!("{:?}", EXAMPLE);
}

Best,
Gary