[PATCH v6 2/2] rust: alloc: kvec: add doc example for as_slice method

Hui Zhu posted 2 patches 2 months, 1 week ago
[PATCH v6 2/2] rust: alloc: kvec: add doc example for as_slice method
Posted by Hui Zhu 2 months, 1 week ago
From: Hui Zhu <zhuhui@kylinos.cn>

Add a practical usage example to the documentation of KVec::as_slice()
showing how to:
Create a new KVec.
Push elements into it.
Convert to a slice via as_slice().

Co-developed-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
---
 rust/kernel/alloc/kvec.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 92d0ed3f302e..f57e08c64929 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -224,6 +224,16 @@ unsafe fn dec_len(&mut self, count: usize) -> &mut [T] {
     }
 
     /// Returns a slice of the entire vector.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let mut v = KVec::new();
+    /// v.push(1, GFP_KERNEL)?;
+    /// v.push(2, GFP_KERNEL)?;
+    /// assert_eq!(v.as_slice(), &[1, 2]);
+    /// # Ok::<(), Error>(())
+    /// ```
     #[inline]
     pub fn as_slice(&self) -> &[T] {
         self
-- 
2.43.0
Re: [PATCH v6 2/2] rust: alloc: kvec: add doc example for as_slice method
Posted by Danilo Krummrich 2 months, 1 week ago
On Wed Jul 30, 2025 at 5:35 AM CEST, Hui Zhu wrote:
> From: Hui Zhu <zhuhui@kylinos.cn>
>
> Add a practical usage example to the documentation of KVec::as_slice()
> showing how to:
> Create a new KVec.
> Push elements into it.
> Convert to a slice via as_slice().
>
> Co-developed-by: Geliang Tang <geliang@kernel.org>
> Signed-off-by: Geliang Tang <geliang@kernel.org>
> Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>

Thanks -- I will pick this one up after -rc1 is out.
Re: [PATCH v6 2/2] rust: alloc: kvec: add doc example for as_slice method
Posted by Alice Ryhl 2 months, 1 week ago
On Wed, Jul 30, 2025 at 11:35:22AM +0800, Hui Zhu wrote:
> From: Hui Zhu <zhuhui@kylinos.cn>
> 
> Add a practical usage example to the documentation of KVec::as_slice()
> showing how to:
> Create a new KVec.
> Push elements into it.
> Convert to a slice via as_slice().
> 
> Co-developed-by: Geliang Tang <geliang@kernel.org>
> Signed-off-by: Geliang Tang <geliang@kernel.org>
> Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>

It looks like this did not change since v5 where I gave my

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

tag. Please make sure to include the tag in future versions if you have
not made any changes.

Alice
Re: [PATCH v6 2/2] rust: alloc: kvec: add doc example for as_slice method
Posted by Hui Zhu 2 months ago
2025年7月30日 16:03, "Alice Ryhl" <aliceryhl@google.com mailto:aliceryhl@google.com?to=%22Alice%20Ryhl%22%20%3Caliceryhl%40google.com%3E > 写到:


> 
> On Wed, Jul 30, 2025 at 11:35:22AM +0800, Hui Zhu wrote:
> 
> > 
> > From: Hui Zhu <zhuhui@kylinos.cn>
> >  
> >  Add a practical usage example to the documentation of KVec::as_slice()
> >  showing how to:
> >  Create a new KVec.
> >  Push elements into it.
> >  Convert to a slice via as_slice().
> >  
> >  Co-developed-by: Geliang Tang <geliang@kernel.org>
> >  Signed-off-by: Geliang Tang <geliang@kernel.org>
> >  Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
> > 
> It looks like this did not change since v5 where I gave my
> 
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> 
> tag. Please make sure to include the tag in future versions if you have
> not made any changes.

Oops!
Sorry for that.  Fixed in the v7 version.

Best,
Hui

> 
> Alice
>