[PATCH 0/4] rust: a few common Borrow/BorrowMut implementations

Alexandre Courbot posted 4 patches 8 months, 1 week ago
There is a newer version of this series
rust/kernel/alloc/kbox.rs | 21 +++++++++++++++++++++
rust/kernel/alloc/kvec.rs | 19 +++++++++++++++++++
rust/kernel/str.rs        | 13 +++++++++++++
rust/kernel/sync/arc.rs   | 21 ++++++++++++++++++++-
4 files changed, 73 insertions(+), 1 deletion(-)
[PATCH 0/4] rust: a few common Borrow/BorrowMut implementations
Posted by Alexandre Courbot 8 months, 1 week ago
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 series provides just the `Borrow` and `BorrowMut` implementations,
but another common use of `Borrow` in the standard library is to use the
borrowed type for key lookups in collections. For this to work, a few
consistency traits (`Eq`, `Hash`, and `Ord`) need to be implemented. I
am not sure whether we want this on kernel types as well, but please let
me know if we do and I will add them in the next revision.

[1] https://lore.kernel.org/rust-for-linux/DA9JTYA0EQU8.26M0ZX80FOBWY@nvidia.com/

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Alexandre Courbot (4):
      rust: alloc: implement `Borrow` and `BorrowMut` for `Vec`
      rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types
      rust: alloc: implement `Borrow` and `BorrowMut` for `KBox`
      rust: alloc: implement `Borrow` and `BorrowMut` for `CString`

 rust/kernel/alloc/kbox.rs | 21 +++++++++++++++++++++
 rust/kernel/alloc/kvec.rs | 19 +++++++++++++++++++
 rust/kernel/str.rs        | 13 +++++++++++++
 rust/kernel/sync/arc.rs   | 21 ++++++++++++++++++++-
 4 files changed, 73 insertions(+), 1 deletion(-)
---
base-commit: 7a17bbc1d952057898cb0739e60665908fbb8c72
change-id: 20250531-borrow_impls-8dfef3fcee93

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH 0/4] rust: a few common Borrow/BorrowMut implementations
Posted by Alice Ryhl 8 months, 1 week ago
On Sun, Jun 01, 2025 at 12:00:38PM +0900, Alexandre Courbot wrote:
> 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 series provides just the `Borrow` and `BorrowMut` implementations,
> but another common use of `Borrow` in the standard library is to use the
> borrowed type for key lookups in collections. For this to work, a few
> consistency traits (`Eq`, `Hash`, and `Ord`) need to be implemented. I
> am not sure whether we want this on kernel types as well, but please let
> me know if we do and I will add them in the next revision.
> 
> [1] https://lore.kernel.org/rust-for-linux/DA9JTYA0EQU8.26M0ZX80FOBWY@nvidia.com/
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

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