Implement `Borrow<[T]>` and `BorrowMut<[T]>` for `Vec<T>`. This allows
`Vec<T>` to be used in generic APIs asking for types implementing those
traits. `[T; N]` and `&mut [T]` also implement those traits allowing
users to use either owned, borrowed and heap-owned values.
The implementation leverages `as_slice` and `as_mut_slice`.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 1a0dd852a468ccda6ea1b521bc1e7dbc8d7fc79c..3f368d4a67683ac5a0ff87d7df33a3bb640ced59 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -8,6 +8,7 @@
AllocError, Allocator, Box, Flags,
};
use core::{
+ borrow::{Borrow, BorrowMut},
fmt,
marker::PhantomData,
mem::{ManuallyDrop, MaybeUninit},
@@ -890,6 +891,62 @@ fn deref_mut(&mut self) -> &mut [T] {
}
}
+/// Allows `Vec<T>` to be used as a `Borrow<[T]>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::Borrow;
+/// struct Foo<B: Borrow<[u32]>>(B);
+///
+/// // Owned array.
+/// let foo_array = Foo([1, 2, 3]);
+///
+/// // Owned vector.
+/// let foo_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);
+///
+/// let arr = [1, 2, 3];
+/// // Borrowed slice from `arr`.
+/// let foo_borrowed = Foo(&arr[..]);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T, A> Borrow<[T]> for Vec<T, A>
+where
+ A: Allocator,
+{
+ fn borrow(&self) -> &[T] {
+ self.as_slice()
+ }
+}
+
+/// Allows `Vec<T>` to be used as a `BorrowMut<[T]>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::BorrowMut;
+/// struct Foo<B: BorrowMut<[u32]>>(B);
+///
+/// // Owned array.
+/// let foo_array = Foo([1, 2, 3]);
+///
+/// // Owned vector.
+/// let foo_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);
+///
+/// let mut arr = [1, 2, 3];
+/// // Borrowed slice from `arr`.
+/// let foo_borrowed = Foo(&mut arr[..]);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T, A> BorrowMut<[T]> for Vec<T, A>
+where
+ A: Allocator,
+{
+ fn borrow_mut(&mut self) -> &mut [T] {
+ self.as_mut_slice()
+ }
+}
+
impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {}
impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
--
2.49.0
On Fri Jun 13, 2025 at 3:46 PM CEST, Alexandre Courbot wrote:
> Implement `Borrow<[T]>` and `BorrowMut<[T]>` for `Vec<T>`. This allows
> `Vec<T>` to be used in generic APIs asking for types implementing those
> traits. `[T; N]` and `&mut [T]` also implement those traits allowing
> users to use either owned, borrowed and heap-owned values.
>
> The implementation leverages `as_slice` and `as_mut_slice`.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
One comment below, with that fixed:
Reviewed-by: Benno Lossin <lossin@kernel.org>
> ---
> rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 57 insertions(+)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 1a0dd852a468ccda6ea1b521bc1e7dbc8d7fc79c..3f368d4a67683ac5a0ff87d7df33a3bb640ced59 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -8,6 +8,7 @@
> AllocError, Allocator, Box, Flags,
> };
> use core::{
> + borrow::{Borrow, BorrowMut},
> fmt,
> marker::PhantomData,
> mem::{ManuallyDrop, MaybeUninit},
> @@ -890,6 +891,62 @@ fn deref_mut(&mut self) -> &mut [T] {
> }
> }
>
> +/// Allows `Vec<T>` to be used as a `Borrow<[T]>`.
I personally would vote against this first line description here. I
don't think that it will show up in a summary view of rust doc (since
trait impls don't appear in searches or module overviews). Additionally,
this first sentence seems like this kind of comment:
// call `foo`:
foo();
So let's just remove it and directly start with the examples :)
Also for the other cases.
---
Cheers,
Benno
> +///
> +/// # Examples
> +///
> +/// ```
> +/// # use core::borrow::Borrow;
> +/// struct Foo<B: Borrow<[u32]>>(B);
> +///
> +/// // Owned array.
> +/// let foo_array = Foo([1, 2, 3]);
> +///
> +/// // Owned vector.
> +/// let foo_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);
> +///
> +/// let arr = [1, 2, 3];
> +/// // Borrowed slice from `arr`.
> +/// let foo_borrowed = Foo(&arr[..]);
> +/// # Ok::<(), Error>(())
> +/// ```
> +impl<T, A> Borrow<[T]> for Vec<T, A>
> +where
> + A: Allocator,
> +{
> + fn borrow(&self) -> &[T] {
> + self.as_slice()
> + }
> +}
> +
> +/// Allows `Vec<T>` to be used as a `BorrowMut<[T]>`.
> +///
> +/// # Examples
> +///
> +/// ```
> +/// # use core::borrow::BorrowMut;
> +/// struct Foo<B: BorrowMut<[u32]>>(B);
> +///
> +/// // Owned array.
> +/// let foo_array = Foo([1, 2, 3]);
> +///
> +/// // Owned vector.
> +/// let foo_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);
> +///
> +/// let mut arr = [1, 2, 3];
> +/// // Borrowed slice from `arr`.
> +/// let foo_borrowed = Foo(&mut arr[..]);
> +/// # Ok::<(), Error>(())
> +/// ```
> +impl<T, A> BorrowMut<[T]> for Vec<T, A>
> +where
> + A: Allocator,
> +{
> + fn borrow_mut(&mut self) -> &mut [T] {
> + self.as_mut_slice()
> + }
> +}
> +
> impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {}
>
> impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
On Sun Jun 15, 2025 at 4:19 AM JST, Benno Lossin wrote:
> On Fri Jun 13, 2025 at 3:46 PM CEST, Alexandre Courbot wrote:
>> Implement `Borrow<[T]>` and `BorrowMut<[T]>` for `Vec<T>`. This allows
>> `Vec<T>` to be used in generic APIs asking for types implementing those
>> traits. `[T; N]` and `&mut [T]` also implement those traits allowing
>> users to use either owned, borrowed and heap-owned values.
>>
>> The implementation leverages `as_slice` and `as_mut_slice`.
>>
>> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>
> One comment below, with that fixed:
>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
>
>> ---
>> rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 57 insertions(+)
>>
>> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
>> index 1a0dd852a468ccda6ea1b521bc1e7dbc8d7fc79c..3f368d4a67683ac5a0ff87d7df33a3bb640ced59 100644
>> --- a/rust/kernel/alloc/kvec.rs
>> +++ b/rust/kernel/alloc/kvec.rs
>> @@ -8,6 +8,7 @@
>> AllocError, Allocator, Box, Flags,
>> };
>> use core::{
>> + borrow::{Borrow, BorrowMut},
>> fmt,
>> marker::PhantomData,
>> mem::{ManuallyDrop, MaybeUninit},
>> @@ -890,6 +891,62 @@ fn deref_mut(&mut self) -> &mut [T] {
>> }
>> }
>>
>> +/// Allows `Vec<T>` to be used as a `Borrow<[T]>`.
>
> I personally would vote against this first line description here. I
> don't think that it will show up in a summary view of rust doc (since
> trait impls don't appear in searches or module overviews). Additionally,
> this first sentence seems like this kind of comment:
>
> // call `foo`:
> foo();
>
> So let's just remove it and directly start with the examples :)
That's fine by me, I was just a bit nervous to start a doccomment
directly with the examples, but in this context it appears to make
sense.
Thanks for the review!
On Sun, Jun 15, 2025 at 2:36 PM Alexandre Courbot <acourbot@nvidia.com> wrote: > > That's fine by me, I was just a bit nervous to start a doccomment > directly with the examples, but in this context it appears to make > sense. Yeah, that may be my fault by emphasizing a lot the "title is used as short description by `rustdoc`" thing :) Here that does not apply, and I agree that (at least the title here) is not really conveying anything. Cheers, Miguel
On Sun Jun 15, 2025 at 3:16 PM CEST, Miguel Ojeda wrote: > On Sun, Jun 15, 2025 at 2:36 PM Alexandre Courbot <acourbot@nvidia.com> wrote: >> >> That's fine by me, I was just a bit nervous to start a doccomment >> directly with the examples, but in this context it appears to make >> sense. > > Yeah, that may be my fault by emphasizing a lot the "title is used as > short description by `rustdoc`" thing :) I also do that pretty often :) > Here that does not apply, and I agree that (at least the title here) > is not really conveying anything. Might be something for our "how to write rust docs" documentation :) --- Cheers, Benno
© 2016 - 2026 Red Hat, Inc.