Re: [PATCH v3 03/12] rust: xarray: add `contains_index` method

From: Andreas Hindborg

Date: Tue Feb 24 2026 - 08:58:55 EST


Daniel Gomez <da.gomez@xxxxxxxxxx> writes:

> On 2026-02-09 15:38, Andreas Hindborg wrote:
>> Add a convenience method `contains_index` to check whether an element
>> exists at a given index in the XArray. This method provides a more
>> ergonomic API compared to calling `get` and checking for `Some`.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
>> ---
>> rust/kernel/xarray.rs | 21 +++++++++++++++++++++
>> 1 file changed, 21 insertions(+)
>>
>> diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
>> index d9762c6bef19c..ede48b5e1dba3 100644
>> --- a/rust/kernel/xarray.rs
>> +++ b/rust/kernel/xarray.rs
>> @@ -218,6 +218,27 @@ fn load<F, U>(&self, index: usize, f: F) -> Option<U>
>> Some(f(ptr))
>> }
>>
>> + /// Checks if the XArray contains an element at the specified index.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// # use kernel::{alloc::{flags::GFP_KERNEL, kbox::KBox}, xarray::{AllocKind, XArray}};
>> + /// let xa = KBox::pin_init(XArray::new(AllocKind::Alloc), GFP_KERNEL)?;
>
> Side comment: I'd also update the examples to the new coding style. And it'd
> probably be cleaner in the first commit.

I'll switch to the vertical import style.

>
>> + ///
>> + /// let mut guard = xa.lock();
>> + /// assert_eq!(guard.contains_index(42), false);
>> + ///
>> + /// guard.store(42, KBox::new(0u32, GFP_KERNEL)?, GFP_KERNEL)?;
>> + ///
>> + /// assert_eq!(guard.contains_index(42), true);
>> + ///
>> + /// # Ok::<(), kernel::error::Error>(())
>> + /// ```
>> + pub fn contains_index(&self, index: usize) -> bool {
>
> Nit. The method name may imply there's a contains_*() variant. I'd rename it to
> contains() so we are consistent with the rest of the API.

I disagree with this one. `contains` might cheat the reader as it is not
obvious whether we are querying for a key or value.

>
> Reviewed-by: Daniel Gomez <da.gomez@xxxxxxxxxxx>

Thanks!

Best regards,
Andreas Hindborg