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 | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 3c72e0bdddb8..d384c34d1a5e 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -224,6 +224,15 @@ 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]);
+ /// ```
#[inline]
pub fn as_slice(&self) -> &[T] {
self
--
2.43.0
On Thu, Jul 24, 2025 at 9:54 AM Hui Zhu <hui.zhu@linux.dev> 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>
> ---
> rust/kernel/alloc/kvec.rs | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 3c72e0bdddb8..d384c34d1a5e 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -224,6 +224,15 @@ 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);
These function calls are fallible, but you are ignoring the errors.
Please use the question mark operator.
Alice
Hi Alice,
On Thu, Jul 24, 2025 at 10:21:46AM +0200, Alice Ryhl wrote:
> On Thu, Jul 24, 2025 at 9:54 AM Hui Zhu <hui.zhu@linux.dev> 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>
> > ---
> > rust/kernel/alloc/kvec.rs | 9 +++++++++
> > 1 file changed, 9 insertions(+)
> >
> > diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> > index 3c72e0bdddb8..d384c34d1a5e 100644
> > --- a/rust/kernel/alloc/kvec.rs
> > +++ b/rust/kernel/alloc/kvec.rs
> > @@ -224,6 +224,15 @@ 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);
>
> These function calls are fallible, but you are ignoring the errors.
> Please use the question mark operator.
>
> Alice
Just sent v4 according to your comments.
Best,
Hui
© 2016 - 2026 Red Hat, Inc.