[PATCH v2 2/8] rust: alloc: add Vec::push_init
Eliot Courtney <[email protected]>
| Newsgroups | dev.linux.lists.nova-gpu,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
Add `Vec::push_init` which is the init-infallible version of `try_push_init`. Signed-off-by: Eliot Courtney <[email protected]> --- 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