[PATCH 2/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types

Alexandre Courbot posted 4 patches 8 months, 1 week ago
There is a newer version of this series
[PATCH 2/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types
Posted by Alexandre Courbot 8 months, 1 week ago
Implement these two common traits, which allow generic types to store
either an owned value or a reference to it, leveraging the `Deref`
implementation.

`Arc` can only implement `Borrow`, but `UniqueArc` is able to support
both.

`ForeignOwnable` makes a call to its own `borrow` method which must be
disambiguated.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/sync/arc.rs | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index c7af0aa48a0a049bfeeba3a81080355f4d381738..40507e8901c349ba5f782457c203ff4cc181dd6c 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,12 @@ fn as_ref(&self) -> &T {
     }
 }
 
+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 +841,18 @@ fn deref_mut(&mut self) -> &mut Self::Target {
     }
 }
 
+impl<T: ?Sized> Borrow<T> for UniqueArc<T> {
+    fn borrow(&self) -> &T {
+        self.deref()
+    }
+}
+
+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
Re: [PATCH 2/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types
Posted by Benno Lossin 8 months, 1 week ago
On Sun Jun 1, 2025 at 5:00 AM CEST, Alexandre Courbot wrote:
> Implement these two common traits, which allow generic types to store
> either an owned value or a reference to it, leveraging the `Deref`
> implementation.

Same here.

---
Cheers,
Benno

>
> `Arc` can only implement `Borrow`, but `UniqueArc` is able to support
> both.
>
> `ForeignOwnable` makes a call to its own `borrow` method which must be
> disambiguated.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/sync/arc.rs | 21 ++++++++++++++++++++-
>  1 file changed, 20 insertions(+), 1 deletion(-)
Re: [PATCH 2/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types
Posted by Alexandre Courbot 8 months, 1 week ago
Typical noticed-as-I-sent mistake: the prefix should be "rust: sync:",
not "rust: alloc:" (also wrong on patch 4). Not resending just for that,
will fix for v2.