Simplify the implementation by removing the closure-based API from `Guard::load` in favor of returning `Option>` directly. Signed-off-by: Andreas Hindborg --- rust/kernel/xarray.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs index eadddafb180ec..e654bf56dc97c 100644 --- a/rust/kernel/xarray.rs +++ b/rust/kernel/xarray.rs @@ -211,12 +211,8 @@ fn from(value: StoreError) -> Self { } impl<'a, T: ForeignOwnable> Guard<'a, T> { - fn load(&self, index: usize, f: F) -> Option - where - F: FnOnce(NonNull) -> U, - { - let mut state = XArrayState::new(self, index); - Some(f(state.load()?)) + fn load(&self, index: usize) -> Option> { + XArrayState::new(self, index).load() } /// Checks if the XArray contains an element at the specified index. @@ -242,18 +238,17 @@ pub fn contains_index(&self, index: usize) -> bool { /// Provides a reference to the element at the given index. pub fn get(&self, index: usize) -> Option> { - 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. pub fn get_mut(&mut self, index: usize) -> Option> { - 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.51.2