* [PATCH v2 0/4] rust: a few common Borrow/BorrowMut implementations
@ 2025-06-13 13:46 Alexandre Courbot
2025-06-13 13:46 ` [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` Alexandre Courbot
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: Alexandre Courbot @ 2025-06-13 13:46 UTC (permalink / raw)
To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: rust-for-linux, linux-kernel, Alexandre Courbot
The Borrow trait has multiple uses, one of them being to store either an
owned value or a reference to it inside a generic container. This series
adds these implementations for `Box`, `Arc`, `Vec`, and `CString`. I
came across the need for this while experimenting with the scatterlist
abstraction series [1].
This second revision adds examples to each impl block as requested. I am
personally not quite convinced that they are needed, as they just
illustrate the basic usage of `Borrow`, but have added them to this
revision so we can decide whether we want them or not.
[1] https://lore.kernel.org/rust-for-linux/DA9JTYA0EQU8.26M0ZX80FOBWY@nvidia.com/
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Changes in v2:
- Rebase on top of v6.16-rc1.
- Improve commit messages. (thanks Benno!)
- Add examples on each impl block.
- Link to v1: https://lore.kernel.org/r/20250601-borrow_impls-v1-0-e1caeb428db4@nvidia.com
---
Alexandre Courbot (4):
rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types
rust: alloc: implement `Borrow` and `BorrowMut` for `KBox`
rust: str: implement `Borrow` and `BorrowMut` for `CString`
rust/kernel/alloc/kbox.rs | 61 ++++++++++++++++++++++++++++++++++
rust/kernel/alloc/kvec.rs | 57 ++++++++++++++++++++++++++++++++
rust/kernel/str.rs | 49 +++++++++++++++++++++++++++
rust/kernel/sync/arc.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 250 insertions(+), 1 deletion(-)
---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20250531-borrow_impls-8dfef3fcee93
Best regards,
--
Alexandre Courbot <acourbot@nvidia.com>
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
2025-06-13 13:46 [PATCH v2 0/4] rust: a few common Borrow/BorrowMut implementations Alexandre Courbot
@ 2025-06-13 13:46 ` Alexandre Courbot
2025-06-14 19:19 ` Benno Lossin
2025-06-13 13:46 ` [PATCH v2 2/4] rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types Alexandre Courbot
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Alexandre Courbot @ 2025-06-13 13:46 UTC (permalink / raw)
To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: rust-for-linux, linux-kernel, Alexandre Courbot
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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 2/4] rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types
2025-06-13 13:46 [PATCH v2 0/4] rust: a few common Borrow/BorrowMut implementations Alexandre Courbot
2025-06-13 13:46 ` [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` Alexandre Courbot
@ 2025-06-13 13:46 ` Alexandre Courbot
2025-06-14 19:20 ` Benno Lossin
2025-06-13 13:46 ` [PATCH v2 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox` Alexandre Courbot
2025-06-13 13:46 ` [PATCH v2 4/4] rust: str: implement `Borrow` and `BorrowMut` for `CString` Alexandre Courbot
3 siblings, 1 reply; 12+ messages in thread
From: Alexandre Courbot @ 2025-06-13 13:46 UTC (permalink / raw)
To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: rust-for-linux, linux-kernel, Alexandre Courbot
Implement `Borrow<T>` and `BorrowMut<T>` for `UniqueArc<T>`, and
`Borrow<T>` for `Arc<T>`. This allows these containers to be used in
generic APIs asking for types implementing those traits. `T` and `&mut
T` also implement those traits allowing users to use either owned,
shared or borrowed values.
`ForeignOwnable` makes a call to its own `borrow` method which must be
disambiguated.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
rust/kernel/sync/arc.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 83 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index c7af0aa48a0a049bfeeba3a81080355f4d381738..c7832e18b6f129ea00f57fb8e38da68ebc209626 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -25,6 +25,7 @@
};
use core::{
alloc::Layout,
+ borrow::{Borrow, BorrowMut},
fmt,
marker::PhantomData,
mem::{ManuallyDrop, MaybeUninit},
@@ -406,7 +407,7 @@ unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> ArcBorrow<'a, T> {
unsafe fn borrow_mut<'a>(ptr: *mut Self::PointedTo) -> ArcBorrow<'a, T> {
// SAFETY: The safety requirements for `borrow_mut` are a superset of the safety
// requirements for `borrow`.
- unsafe { Self::borrow(ptr) }
+ unsafe { <Self as ForeignOwnable>::borrow(ptr) }
}
}
@@ -426,6 +427,33 @@ fn as_ref(&self) -> &T {
}
}
+/// Allows `Arc<T>` to be used as a `Borrow<T>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::Borrow;
+/// # use kernel::sync::Arc;
+/// struct Foo<B: Borrow<u32>>(B);
+///
+/// // Owned instance.
+/// let foo_owned = Foo(1);
+///
+/// // Shared instance.
+/// let arc = Arc::new(1, GFP_KERNEL)?;
+/// let foo_shared = Foo(arc.clone());
+///
+/// let i = 1;
+/// // Borrowed from `i`.
+/// let foo_borrowed = Foo(&i);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T: ?Sized> Borrow<T> for Arc<T> {
+ fn borrow(&self) -> &T {
+ self.deref()
+ }
+}
+
impl<T: ?Sized> Clone for Arc<T> {
fn clone(&self) -> Self {
// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
@@ -834,6 +862,60 @@ fn deref_mut(&mut self) -> &mut Self::Target {
}
}
+/// Allows `UniqueArc<T>` to be used as a `Borrow<T>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::Borrow;
+/// # use kernel::sync::UniqueArc;
+/// struct Foo<B: Borrow<u32>>(B);
+///
+/// // Owned instance.
+/// let foo_owned = Foo(1);
+///
+/// // Owned instance using `UniqueArc`.
+/// let arc = UniqueArc::new(1, GFP_KERNEL)?;
+/// let foo_shared = Foo(arc);
+///
+/// let i = 1;
+/// // Borrowed from `i`.
+/// let foo_borrowed = Foo(&i);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T: ?Sized> Borrow<T> for UniqueArc<T> {
+ fn borrow(&self) -> &T {
+ self.deref()
+ }
+}
+
+/// Allows `UniqueArc<T>` to be used as a `BorrowMut<T>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::BorrowMut;
+/// # use kernel::sync::UniqueArc;
+/// struct Foo<B: BorrowMut<u32>>(B);
+///
+/// // Owned instance.
+/// let foo_owned = Foo(1);
+///
+/// // Owned instance using `UniqueArc`.
+/// let arc = UniqueArc::new(1, GFP_KERNEL)?;
+/// let foo_shared = Foo(arc);
+///
+/// let mut i = 1;
+/// // Borrowed from `i`.
+/// let foo_borrowed = Foo(&mut i);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T: ?Sized> BorrowMut<T> for UniqueArc<T> {
+ fn borrow_mut(&mut self) -> &mut T {
+ self.deref_mut()
+ }
+}
+
impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.deref(), f)
--
2.49.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox`
2025-06-13 13:46 [PATCH v2 0/4] rust: a few common Borrow/BorrowMut implementations Alexandre Courbot
2025-06-13 13:46 ` [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` Alexandre Courbot
2025-06-13 13:46 ` [PATCH v2 2/4] rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types Alexandre Courbot
@ 2025-06-13 13:46 ` Alexandre Courbot
2025-06-14 19:20 ` Benno Lossin
2025-06-13 13:46 ` [PATCH v2 4/4] rust: str: implement `Borrow` and `BorrowMut` for `CString` Alexandre Courbot
3 siblings, 1 reply; 12+ messages in thread
From: Alexandre Courbot @ 2025-06-13 13:46 UTC (permalink / raw)
To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: rust-for-linux, linux-kernel, Alexandre Courbot
Implement `Borrow<T>` and `BorrowMut<T>` for `KBox<T>`. This allows
`KBox<T>` to be used in generic APIs asking for types implementing those
traits. `T` and `&mut T` also implement those traits allowing users to
use either owned, borrowed and heap-owned values.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
rust/kernel/alloc/kbox.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
index c386ff771d506a2eb4c211a93ea9b59bc04c93f5..0e3cbe809a878766ee8f8508ed0d30b4f8afaa07 100644
--- a/rust/kernel/alloc/kbox.rs
+++ b/rust/kernel/alloc/kbox.rs
@@ -6,6 +6,7 @@
use super::allocator::{KVmalloc, Kmalloc, Vmalloc};
use super::{AllocError, Allocator, Flags};
use core::alloc::Layout;
+use core::borrow::{Borrow, BorrowMut};
use core::fmt;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
@@ -499,6 +500,66 @@ fn deref_mut(&mut self) -> &mut T {
}
}
+/// Allows `Box<T>` to be used as a `Borrow<T>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::Borrow;
+/// # use kernel::alloc::KBox;
+/// struct Foo<B: Borrow<u32>>(B);
+///
+/// // Owned instance.
+/// let foo_owned = Foo(1);
+///
+/// // Owned instance using `KBox`.
+/// let foo_box = Foo(KBox::new(1, GFP_KERNEL)?);
+///
+/// let i = 1;
+/// // Borrowed from `i`.
+/// let foo_borrowed = Foo(&i);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T, A> Borrow<T> for Box<T, A>
+where
+ T: ?Sized,
+ A: Allocator,
+{
+ fn borrow(&self) -> &T {
+ self.deref()
+ }
+}
+
+/// Allows `Box<T>` to be used as a `BorrowMut<T>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::BorrowMut;
+/// # use kernel::alloc::KBox;
+/// struct Foo<B: BorrowMut<u32>>(B);
+///
+/// // Owned instance.
+/// let foo_owned = Foo(1);
+///
+/// // Owned instance using `KBox`.
+/// let foo_box = Foo(KBox::new(1, GFP_KERNEL)?);
+///
+/// let mut i = 1;
+/// // Borrowed from `i`.
+/// let foo_borrowed = Foo(&mut i);
+/// # Ok::<(), Error>(())
+/// ```
+impl<T, A> BorrowMut<T> for Box<T, A>
+where
+ T: ?Sized,
+ A: Allocator,
+{
+ fn borrow_mut(&mut self) -> &mut T {
+ self.deref_mut()
+ }
+}
+
impl<T, A> fmt::Display for Box<T, A>
where
T: ?Sized + fmt::Display,
--
2.49.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 4/4] rust: str: implement `Borrow` and `BorrowMut` for `CString`
2025-06-13 13:46 [PATCH v2 0/4] rust: a few common Borrow/BorrowMut implementations Alexandre Courbot
` (2 preceding siblings ...)
2025-06-13 13:46 ` [PATCH v2 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox` Alexandre Courbot
@ 2025-06-13 13:46 ` Alexandre Courbot
2025-06-14 19:21 ` Benno Lossin
3 siblings, 1 reply; 12+ messages in thread
From: Alexandre Courbot @ 2025-06-13 13:46 UTC (permalink / raw)
To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: rust-for-linux, linux-kernel, Alexandre Courbot
Implement `Borrow<CStr>` and `BorrowMut<CStr>` for `CString`. This
allows `CString` to be used in generic APIs asking for types
implementing those traits. `&CStr` and `&mut CStr` also implement those
traits allowing users to use either owned or borrowed values.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
rust/kernel/str.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index a927db8e079c3597860880947a03959e1d6d712e..36d46443c269a73fa1246c52ab8558bae3e439ee 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -3,6 +3,7 @@
//! String representations.
use crate::alloc::{flags::*, AllocError, KVec};
+use core::borrow::{Borrow, BorrowMut};
use core::fmt::{self, Write};
use core::ops::{self, Deref, DerefMut, Index};
@@ -911,6 +912,54 @@ fn deref_mut(&mut self) -> &mut Self::Target {
}
}
+/// Allows `CString` to be used as a `Borrow<CStr>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::Borrow;
+/// # use kernel::str::{CStr, CString};
+/// # use kernel::fmt;
+/// struct Foo<B: Borrow<CStr>>(B);
+///
+/// // Owned instance using `CString`.
+/// let foo_owned = Foo(CString::try_from_fmt(fmt!("{}", "abc"))?);
+///
+/// let str_data = b"abc\0";
+/// // Borrowed from `str_data`.
+/// let foo_borrowed = Foo(CStr::from_bytes_with_nul(str_data)?);
+/// # Ok::<(), Error>(())
+/// ```
+impl Borrow<CStr> for CString {
+ fn borrow(&self) -> &CStr {
+ self.deref()
+ }
+}
+
+/// Allows `CString` to be used as a `BorrowMut<CStr>`.
+///
+/// # Examples
+///
+/// ```
+/// # use core::borrow::BorrowMut;
+/// # use kernel::str::{CStr, CString};
+/// # use kernel::fmt;
+/// struct Foo<B: BorrowMut<CStr>>(B);
+///
+/// // Owned instance using `CString`.
+/// let foo_owned = Foo(CString::try_from_fmt(fmt!("{}", "abc"))?);
+///
+/// let mut str_data = [b'a', b'b', b'c', 0];
+/// // Borrowed from `str_data`.
+/// let foo_borrowed = Foo(unsafe { CStr::from_bytes_with_nul_unchecked_mut(&mut str_data) });
+/// # Ok::<(), Error>(())
+/// ```
+impl BorrowMut<CStr> for CString {
+ fn borrow_mut(&mut self) -> &mut CStr {
+ self.deref_mut()
+ }
+}
+
impl<'a> TryFrom<&'a CStr> for CString {
type Error = AllocError;
--
2.49.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
2025-06-13 13:46 ` [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` Alexandre Courbot
@ 2025-06-14 19:19 ` Benno Lossin
2025-06-15 12:36 ` Alexandre Courbot
0 siblings, 1 reply; 12+ messages in thread
From: Benno Lossin @ 2025-06-14 19:19 UTC (permalink / raw)
To: Alexandre Courbot, Danilo Krummrich, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
Alice Ryhl, Trevor Gross
Cc: rust-for-linux, linux-kernel
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>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 2/4] rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types
2025-06-13 13:46 ` [PATCH v2 2/4] rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types Alexandre Courbot
@ 2025-06-14 19:20 ` Benno Lossin
0 siblings, 0 replies; 12+ messages in thread
From: Benno Lossin @ 2025-06-14 19:20 UTC (permalink / raw)
To: Alexandre Courbot, Danilo Krummrich, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
Alice Ryhl, Trevor Gross
Cc: rust-for-linux, linux-kernel
On Fri Jun 13, 2025 at 3:46 PM CEST, Alexandre Courbot wrote:
> Implement `Borrow<T>` and `BorrowMut<T>` for `UniqueArc<T>`, and
> `Borrow<T>` for `Arc<T>`. This allows these containers to be used in
> generic APIs asking for types implementing those traits. `T` and `&mut
> T` also implement those traits allowing users to use either owned,
> shared or borrowed values.
>
> `ForeignOwnable` makes a call to its own `borrow` method which must be
> disambiguated.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
With the doc comments fixed as detailed in the other thread:
Reviewed-by: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
> ---
> rust/kernel/sync/arc.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 83 insertions(+), 1 deletion(-)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox`
2025-06-13 13:46 ` [PATCH v2 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox` Alexandre Courbot
@ 2025-06-14 19:20 ` Benno Lossin
0 siblings, 0 replies; 12+ messages in thread
From: Benno Lossin @ 2025-06-14 19:20 UTC (permalink / raw)
To: Alexandre Courbot, Danilo Krummrich, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
Alice Ryhl, Trevor Gross
Cc: rust-for-linux, linux-kernel
On Fri Jun 13, 2025 at 3:46 PM CEST, Alexandre Courbot wrote:
> Implement `Borrow<T>` and `BorrowMut<T>` for `KBox<T>`. This allows
> `KBox<T>` to be used in generic APIs asking for types implementing those
> traits. `T` and `&mut T` also implement those traits allowing users to
> use either owned, borrowed and heap-owned values.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
With the doc comments fixed as detailed in the other thread:
Reviewed-by: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
> ---
> rust/kernel/alloc/kbox.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 61 insertions(+)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 4/4] rust: str: implement `Borrow` and `BorrowMut` for `CString`
2025-06-13 13:46 ` [PATCH v2 4/4] rust: str: implement `Borrow` and `BorrowMut` for `CString` Alexandre Courbot
@ 2025-06-14 19:21 ` Benno Lossin
0 siblings, 0 replies; 12+ messages in thread
From: Benno Lossin @ 2025-06-14 19:21 UTC (permalink / raw)
To: Alexandre Courbot, Danilo Krummrich, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
Alice Ryhl, Trevor Gross
Cc: rust-for-linux, linux-kernel
On Fri Jun 13, 2025 at 3:46 PM CEST, Alexandre Courbot wrote:
> Implement `Borrow<CStr>` and `BorrowMut<CStr>` for `CString`. This
> allows `CString` to be used in generic APIs asking for types
> implementing those traits. `&CStr` and `&mut CStr` also implement those
> traits allowing users to use either owned or borrowed values.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
With the doc comments fixed as detailed in the other thread:
Reviewed-by: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
> ---
> rust/kernel/str.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 49 insertions(+)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
2025-06-14 19:19 ` Benno Lossin
@ 2025-06-15 12:36 ` Alexandre Courbot
2025-06-15 13:16 ` Miguel Ojeda
0 siblings, 1 reply; 12+ messages in thread
From: Alexandre Courbot @ 2025-06-15 12:36 UTC (permalink / raw)
To: Benno Lossin, Danilo Krummrich, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
Alice Ryhl, Trevor Gross
Cc: rust-for-linux, linux-kernel
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!
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
2025-06-15 12:36 ` Alexandre Courbot
@ 2025-06-15 13:16 ` Miguel Ojeda
2025-06-15 15:28 ` Benno Lossin
0 siblings, 1 reply; 12+ messages in thread
From: Miguel Ojeda @ 2025-06-15 13:16 UTC (permalink / raw)
To: Alexandre Courbot
Cc: Benno Lossin, Danilo Krummrich, Miguel Ojeda, Alex Gaynor,
Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
Alice Ryhl, Trevor Gross, rust-for-linux, linux-kernel
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
2025-06-15 13:16 ` Miguel Ojeda
@ 2025-06-15 15:28 ` Benno Lossin
0 siblings, 0 replies; 12+ messages in thread
From: Benno Lossin @ 2025-06-15 15:28 UTC (permalink / raw)
To: Miguel Ojeda, Alexandre Courbot
Cc: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
rust-for-linux, linux-kernel
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
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2025-06-15 15:28 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-13 13:46 [PATCH v2 0/4] rust: a few common Borrow/BorrowMut implementations Alexandre Courbot
2025-06-13 13:46 ` [PATCH v2 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` Alexandre Courbot
2025-06-14 19:19 ` Benno Lossin
2025-06-15 12:36 ` Alexandre Courbot
2025-06-15 13:16 ` Miguel Ojeda
2025-06-15 15:28 ` Benno Lossin
2025-06-13 13:46 ` [PATCH v2 2/4] rust: sync: implement `Borrow` and `BorrowMut` for `Arc` types Alexandre Courbot
2025-06-14 19:20 ` Benno Lossin
2025-06-13 13:46 ` [PATCH v2 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox` Alexandre Courbot
2025-06-14 19:20 ` Benno Lossin
2025-06-13 13:46 ` [PATCH v2 4/4] rust: str: implement `Borrow` and `BorrowMut` for `CString` Alexandre Courbot
2025-06-14 19:21 ` Benno Lossin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).