[PATCH v2 6/7] rust: alloc: add Vec::remove

Alice Ryhl posted 7 patches 9 months ago
There is a newer version of this series
[PATCH v2 6/7] rust: alloc: add Vec::remove
Posted by Alice Ryhl 9 months ago
This is needed by Rust Binder in the range allocator, and by upcoming
GPU drivers during firmware initialization.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/alloc/kvec.rs | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 866369406ea95e68adea366828552e76e451e24f..f7f7f9c650f8167ad6f53b0d83e328203445aa1f 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -356,6 +356,34 @@ pub fn pop(&mut self) -> Option<T> {
         Some(unsafe { self.as_mut_ptr().add(len_sub_1).read() })
     }
 
+    /// Removes the element at the given index.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let mut v = kernel::kvec![1, 2, 3]?;
+    /// assert_eq!(v.remove(1), 2);
+    /// assert_eq!(v, [1, 3]);
+    /// # Ok::<(), Error>(())
+    /// ```
+    pub fn remove(&mut self, i: idx) -> T {
+        // INVARIANT: This breaks the invariants by invalidating the value at index `i`, but we
+        // restore the invariants below.
+        // SAFETY: Since `&self[i]` did not result in a panic, the value at index `i` is valid.
+        let value = unsafe { ptr::read(&self[i]) };
+
+        // SAFETY: We checked that `i` is in-bounds.
+        let p = unsafe { self.as_mut_ptr().add(i) };
+        // INVARIANT: The invariants are still broken, but now the invalid value is the last
+        // element of the vector.
+        // SAFETY: `p.add(1).add(self.len - i - 1)` is `i+1+len-i-1 == len` elements after the
+        // beginning of the vector, so this is in-bounds of the vector.
+        unsafe { ptr::copy(p.add(1), p, self.len - i - 1) };
+        // INVARIANT: This restores the invariants since the invalid element no longer needs to be
+        // valid.
+        self.len = self.len - 1;
+    }
+
     /// Creates a new [`Vec`] instance with at least the given capacity.
     ///
     /// # Examples

-- 
2.49.0.395.g12beb8f557-goog