[PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment

Hsiu Che Yu posted 1 patch 1 month, 3 weeks ago
rust/kernel/alloc/kvec.rs | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
[PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment
Posted by Hsiu Che Yu 1 month, 3 weeks ago
Add a doc test for `Vec::extend_with` demonstrating basic usage and the
zero-length case.

Also fix an incorrect operator in the SAFETY comment, changing `<` to `<=`,
since `Vec::reserve` guarantees capacity for exactly n additional elements,
so the equal case should be included.

Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>
---
 rust/kernel/alloc/kvec.rs | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index ac8d6f763ae8..fc4bf0a7934d 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -737,6 +737,24 @@ pub fn retain(&mut self, mut f: impl FnMut(&mut T) -> bool) {
 
 impl<T: Clone, A: Allocator> Vec<T, A> {
     /// Extend the vector by `n` clones of `value`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let mut v = KVec::new();
+    /// v.push(1, GFP_KERNEL)?;
+    ///
+    /// v.extend_with(3, 5, GFP_KERNEL)?;
+    /// assert_eq!(&v, &[1, 5, 5, 5]);
+    ///
+    /// v.extend_with(2, 8, GFP_KERNEL)?;
+    /// assert_eq!(&v, &[1, 5, 5, 5, 8, 8]);
+    ///
+    /// v.extend_with(0, 3, GFP_KERNEL)?;
+    /// assert_eq!(&v, &[1, 5, 5, 5, 8, 8]);
+    ///
+    /// # Ok::<(), Error>(())
+    /// ```
     pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
         if n == 0 {
             return Ok(());
@@ -754,7 +772,7 @@ pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), Al
         spare[n - 1].write(value);
 
         // SAFETY:
-        // - `self.len() + n < self.capacity()` due to the call to reserve above,
+        // - `self.len() + n <= self.capacity()` due to the call to reserve above,
         // - the loop and the line above initialized the next `n` elements.
         unsafe { self.inc_len(n) };
 
-- 
2.43.0
Re: [PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment
Posted by Alexandre Courbot 1 month, 3 weeks ago
Hello Hsiu,

On Sat Apr 25, 2026 at 3:08 PM JST, Hsiu Che Yu wrote:
> Add a doc test for `Vec::extend_with` demonstrating basic usage and the
> zero-length case.
>
> Also fix an incorrect operator in the SAFETY comment, changing `<` to `<=`,
> since `Vec::reserve` guarantees capacity for exactly n additional elements,
> so the equal case should be included.

As per the guidelines [1], "A patch should do one thing and one thing
only".

These should be two separate patches.

[1] https://kernelnewbies.org/PatchSeries
Re: [PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment
Posted by Hsiu Che Yu 1 month, 3 weeks ago
Hi Alexandre,

On Sat, Apr 25, 2026 at 03:32:57PM +0900, Alexandre Courbot wrote:
>As per the guidelines [1], "A patch should do one thing and one thing
>only".
>
>These should be two separate patches.

You're right, my apologies for bundling two unrelated changes together.

Should I send these as two separate v2 patches, or start fresh with two new patch series?

Thanks,
Hsiu
Re: [PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment
Posted by Alexandre Courbot 1 month, 3 weeks ago
On Sat Apr 25, 2026 at 3:53 PM JST, Hsiu Che Yu wrote:
> Hi Alexandre,
>
> On Sat, Apr 25, 2026 at 03:32:57PM +0900, Alexandre Courbot wrote:
>>As per the guidelines [1], "A patch should do one thing and one thing
>>only".
>>
>>These should be two separate patches.
>
> You're right, my apologies for bundling two unrelated changes together.
>
> Should I send these as two separate v2 patches, or start fresh with two new patch series?

A v2 sounds appropriate since this it is the same work, only rearranged a bit.
Re: [PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment
Posted by Hsiu Che Yu 1 month, 3 weeks ago
On Sat, Apr 25, 2026 at 04:20:22PM +0900, Alexandre Courbot wrote:
>A v2 sounds appropriate since this it is the same work, only rearranged a bit.

Got it. I will send out v2 shortly.

thanks,
Hsiu