[PATCH v6 05/12] rust: xarray: simplify `Guard::load`
From: Andreas Hindborg
Date: Fri Nov 20 2026 - 08:11:19 EST
Simplify the implementation by removing the closure-based API from
`Guard::load` in favor of returning `Option<NonNull<c_void>>` directly.
The closure-based API existed to avoid passing around untyped
pointers. The following patches add find and entry operations that
need to store the returned pointer in entry objects and pass it
between internal functions, where the closure style does not scale.
Change `load` to return the pointer directly, establishing the style
used for the rest of the series. Pointers are still only converted to
references at the public API boundary.
Reviewed-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
rust/kernel/xarray.rs | 25 +++++++++++--------------
1 file changed, 11 insertions(+), 14 deletions(-)
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index 183808971683..94386678ef81 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -214,28 +214,25 @@ fn from(value: StoreError<T>) -> Self {
}
impl<'a, T: ForeignOwnable> Guard<'a, T> {
- fn load<F, U>(&self, index: usize, f: F) -> Option<U>
- where
- F: FnOnce(NonNull<c_void>) -> U,
- {
- let mut state = XArrayState::new(self, index);
- Some(f(state.load()?))
+ #[inline]
+ fn load(&self, index: usize) -> Option<NonNull<c_void>> {
+ XArrayState::new(self, index).load()
}
/// Provides a reference to the element at the given index.
+ #[inline]
pub fn get(&self, index: usize) -> Option<T::Borrowed<'_>> {
- self.load(index, |ptr| {
- // SAFETY: `ptr` came from `T::into_foreign`.
- unsafe { T::borrow(ptr.as_ptr()) }
- })
+ let ptr = self.load(index)?;
+ // SAFETY: `ptr` came from `T::into_foreign`.
+ Some(unsafe { T::borrow(ptr.as_ptr()) })
}
/// Provides a mutable reference to the element at the given index.
+ #[inline]
pub fn get_mut(&mut self, index: usize) -> Option<T::BorrowedMut<'_>> {
- self.load(index, |ptr| {
- // SAFETY: `ptr` came from `T::into_foreign`.
- unsafe { T::borrow_mut(ptr.as_ptr()) }
- })
+ let ptr = self.load(index)?;
+ // SAFETY: `ptr` came from `T::into_foreign`.
+ Some(unsafe { T::borrow_mut(ptr.as_ptr()) })
}
/// Removes and returns the element at the given index.
--
2.54.0