From: Alexandre Courbot <acourbot@nvidia.com>
To: "Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Alexandre Courbot <acourbot@nvidia.com>
Subject: [PATCH 2/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types
Date: Sun, 01 Jun 2025 12:00:40 +0900 [thread overview]
Message-ID: <20250601-borrow_impls-v1-2-e1caeb428db4@nvidia.com> (raw)
In-Reply-To: <20250601-borrow_impls-v1-0-e1caeb428db4@nvidia.com>
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
next prev parent reply other threads:[~2025-06-01 3:00 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-01 3:00 [PATCH 0/4] rust: a few common Borrow/BorrowMut implementations Alexandre Courbot
2025-06-01 3:00 ` [PATCH 1/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` Alexandre Courbot
2025-06-01 16:11 ` Benno Lossin
2025-06-02 1:13 ` Alexandre Courbot
2025-06-02 15:06 ` Boqun Feng
2025-06-02 20:21 ` Benno Lossin
2025-06-02 20:45 ` Boqun Feng
2025-06-13 6:15 ` Alexandre Courbot
2025-06-04 7:34 ` Benno Lossin
2025-06-13 5:35 ` Alexandre Courbot
2025-06-13 7:45 ` Benno Lossin
2025-06-01 3:00 ` Alexandre Courbot [this message]
2025-06-01 3:06 ` [PATCH 2/4] rust: alloc: implement `Borrow` and `BorrowMut` for `Arc` types Alexandre Courbot
2025-06-01 16:17 ` Benno Lossin
2025-06-01 3:00 ` [PATCH 3/4] rust: alloc: implement `Borrow` and `BorrowMut` for `KBox` Alexandre Courbot
2025-06-01 3:00 ` [PATCH 4/4] rust: alloc: implement `Borrow` and `BorrowMut` for `CString` Alexandre Courbot
2025-06-02 8:49 ` [PATCH 0/4] rust: a few common Borrow/BorrowMut implementations Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250601-borrow_impls-v1-2-e1caeb428db4@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).