public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment
@ 2026-04-25  6:08 Hsiu Che Yu
  2026-04-25  6:32 ` Alexandre Courbot
  2026-04-25 10:16 ` [PATCH v2 0/2] rust: alloc: fix and test `Vec::extend_with` Hsiu Che Yu
  0 siblings, 2 replies; 12+ messages in thread
From: Hsiu Che Yu @ 2026-04-25  6:08 UTC (permalink / raw)
  To: rust-for-linux
  Cc: Hsiu Che Yu, Danilo Krummrich, Lorenzo Stoakes, Vlastimil Babka,
	Liam R. Howlett, Uladzislau Rezki, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, open list

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


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-04-26 13:10 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-25  6:08 [PATCH] rust: alloc: add doc test for `Vec::extend_with` and fix comment Hsiu Che Yu
2026-04-25  6:32 ` Alexandre Courbot
2026-04-25  6:53   ` Hsiu Che Yu
2026-04-25  7:20     ` Alexandre Courbot
2026-04-25  9:00       ` Hsiu Che Yu
2026-04-25 10:16 ` [PATCH v2 0/2] rust: alloc: fix and test `Vec::extend_with` Hsiu Che Yu
2026-04-25 10:16   ` [PATCH v2 1/2] rust: alloc: add doc test for `Vec::extend_with` Hsiu Che Yu
2026-04-26  3:58     ` Alexandre Courbot
2026-04-25 10:16   ` [PATCH v2 2/2] rust: alloc: fix `Vec::extend_with` SAFETY comment Hsiu Che Yu
2026-04-25 15:17     ` Alexandre Courbot
2026-04-26 13:10     ` Miguel Ojeda
2026-04-26  4:00   ` [PATCH v2 0/2] rust: alloc: fix and test `Vec::extend_with` Alexandre Courbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox