[PATCH v2 2/8] rust: alloc: add Vec::push_init

From: Eliot Courtney

Date: Thu Aug 27 2026 - 10:27:11 EST


Add `Vec::push_init` which is the init-infallible version of
`try_push_init`.

Signed-off-by: Eliot Courtney <ecourtney@xxxxxxxxxx>
---
rust/kernel/alloc/kvec.rs | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index fe86530624c1..bb4da220293b 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -369,7 +369,24 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {

/// Appends an element to the back of the [`Vec`] instance by initializing it in place.
///
- /// Unlike [`Vec::push`], the initializer may be fallible. If the allocation fails, the
+ /// # Examples
+ ///
+ /// ```
+ /// use pin_init::init_zeroed;
+ ///
+ /// let mut v = KVec::<[u8; 200]>::new();
+ /// v.push_init(init_zeroed(), GFP_KERNEL)?;
+ /// assert_eq!(v[0], [0; 200]);
+ /// # Ok::<(), Error>(())
+ /// ```
+ pub fn push_init(&mut self, init: impl Init<T>, flags: Flags) -> Result<(), AllocError> {
+ self.try_push_init(init, flags)
+ .map_err(|PushInitError::AllocError(_)| AllocError)
+ }
+
+ /// Appends an element to the back of the [`Vec`] instance by initializing it in place.
+ ///
+ /// Unlike [`Vec::push_init`], the initializer may be fallible. If the allocation fails, the
/// original initializer `init` is handed back in [`PushInitError::AllocError`]. If the
/// initializer itself fails, its error is returned in [`PushInitError::InitError`].
///

--
2.55.0