public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Gary Guo <gary@garyguo.net>
Cc: "Emilio Cobos Álvarez" <emilio@crisal.io>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	rust-for-linux@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/7] rust: sync: introduce `ArcBorrow`
Date: Fri, 20 Jan 2023 16:44:42 -0800	[thread overview]
Message-ID: <Y8s1evi1wY1XlVgG@boqun-archlinux> (raw)
In-Reply-To: <20230116220736.6d5c8ea3.gary@garyguo.net>

Hi,

On Mon, Jan 16, 2023 at 10:07:36PM +0000, Gary Guo wrote:
> On Wed, 4 Jan 2023 17:06:50 +0100
> Emilio Cobos Álvarez <emilio@crisal.io> wrote:
> 
> > Sorry for the drive-by comment, but maybe it saves some work.
> > 
> > On 1/4/23 16:29, Wedson Almeida Filho wrote:
> > > On Sat, 31 Dec 2022 at 19:43, Gary Guo <gary@garyguo.net> wrote:  
> > >>
> > >> On Wed, 28 Dec 2022 06:03:43 +0000
> > >> Wedson Almeida Filho <wedsonaf@gmail.com> wrote:
> > >>> +pub struct ArcBorrow<'a, T: ?Sized + 'a> {
> > >>> +    inner: NonNull<ArcInner<T>>,
> > >>> +    _p: PhantomData<&'a ()>,
> > >>> +}
> > >>> +
> > >>> +impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
> > >>> +    fn clone(&self) -> Self {
> > >>> +        *self
> > >>> +    }
> > >>> +}
> > >>> +
> > >>> +impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}  
> > >>
> > >> Couldn't this just be derived `Clone` and `Copy`?  
> > > 
> > > Indeed. I'll send a v2 with this.  
> > 
> > I'm not sure this is true. Deriving will add the T: Copy and T: Clone 
> > bound, which I think is not what you want here.
> > 
> > i.e., I assume you want an ArcBorrow to be Copy even if the underlying T 
> > is not.
> 
> Thanks for pointing out, I neglected that.
> 

Somehow I run into this code again, and after a few thoughts, I'm
wondering: isn't ArcBorrow just &ArcInner<T>?

I've tried the following diff, and it seems working. The better parts
are 1) #[derive(Clone, Copy)] works and 2) I'm able to remove a few code
including one "unsafe" in ArcBorrow::deref().

But sure, more close look is needed. Thoughts?

Regards,
Boqun

> In this case:
> 
> Reviewed-by: Gary Guo <gary@garyguo.net>

--------------------------------->8
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index ff73f9240ca1..48f919878f44 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -185,7 +185,7 @@ impl<T: ?Sized> Arc<T> {
         // SAFETY: The constraint that the lifetime of the shared reference must outlive that of
         // the returned `ArcBorrow` ensures that the object remains alive and that no mutable
         // reference can be created.
-        unsafe { ArcBorrow::new(self.ptr) }
+        ArcBorrow(unsafe { self.ptr.as_ref() })
     }
 }
 
@@ -298,52 +298,25 @@ impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
 /// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
 /// obj.as_arc_borrow().use_reference();
 /// ```
-pub struct ArcBorrow<'a, T: ?Sized + 'a> {
-    inner: NonNull<ArcInner<T>>,
-    _p: PhantomData<&'a ()>,
-}
+#[derive(Clone, Copy)]
+pub struct ArcBorrow<'a, T: ?Sized>(&'a ArcInner<T>);
 
 // This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
 impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
 
 // This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
 // `ArcBorrow<U>`.
-impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'_, U>>
-    for ArcBorrow<'_, T>
+impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'a, U>>
+    for ArcBorrow<'a, T>
 {
 }
 
-impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
-    fn clone(&self) -> Self {
-        *self
-    }
-}
-
-impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
-
-impl<T: ?Sized> ArcBorrow<'_, T> {
-    /// Creates a new [`ArcBorrow`] instance.
-    ///
-    /// # Safety
-    ///
-    /// Callers must ensure the following for the lifetime of the returned [`ArcBorrow`] instance:
-    /// 1. That `inner` remains valid;
-    /// 2. That no mutable references to `inner` are created.
-    unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
-        // INVARIANT: The safety requirements guarantee the invariants.
-        Self {
-            inner,
-            _p: PhantomData,
-        }
-    }
-}
-
 impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
     fn from(b: ArcBorrow<'_, T>) -> Self {
         // SAFETY: The existence of `b` guarantees that the refcount is non-zero. `ManuallyDrop`
         // guarantees that `drop` isn't called, so it's ok that the temporary `Arc` doesn't own the
         // increment.
-        ManuallyDrop::new(unsafe { Arc::from_inner(b.inner) })
+        ManuallyDrop::new(unsafe { Arc::from_inner(b.0.into()) })
             .deref()
             .clone()
     }
@@ -353,9 +326,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
     type Target = T;
 
     fn deref(&self) -> &Self::Target {
-        // SAFETY: By the type invariant, the underlying object is still alive with no mutable
-        // references to it, so it is safe to create a shared reference.
-        unsafe { &self.inner.as_ref().data }
+        &self.0.data
     }
 }
 

  reply	other threads:[~2023-01-21  0:45 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-28  6:03 [PATCH 1/7] rust: sync: add `Arc` for ref-counted allocations Wedson Almeida Filho
2022-12-28  6:03 ` [PATCH 2/7] rust: sync: allow type of `self` to be `Arc<T>` or variants Wedson Almeida Filho
2022-12-28 10:03   ` Alice Ryhl
2022-12-31 19:37   ` Gary Guo
2023-01-04 16:12   ` Vincenzo
2022-12-28  6:03 ` [PATCH 3/7] rust: sync: allow coercion from `Arc<T>` to `Arc<U>` Wedson Almeida Filho
2022-12-28 10:03   ` Alice Ryhl
2022-12-31 19:37   ` Gary Guo
2023-01-04 16:13   ` Vincenzo
2022-12-28  6:03 ` [PATCH 4/7] rust: sync: introduce `ArcBorrow` Wedson Almeida Filho
2022-12-28  7:15   ` Laine Taffin Altman
2022-12-28 10:03   ` Alice Ryhl
2022-12-31 19:43   ` Gary Guo
2023-01-04 15:29     ` Wedson Almeida Filho
2023-01-04 16:06       ` Emilio Cobos Álvarez
2023-01-04 17:52         ` Wedson Almeida Filho
2023-01-16 22:07         ` Gary Guo
2023-01-21  0:44           ` Boqun Feng [this message]
2023-01-16 22:42   ` Vincenzo Palazzo
2022-12-28  6:03 ` [PATCH 5/7] rust: sync: allow type of `self` to be `ArcBorrow<T>` Wedson Almeida Filho
2022-12-28 10:04   ` Alice Ryhl
2022-12-31 19:44   ` Gary Guo
2023-01-04 16:18   ` Vincenzo
2022-12-28  6:03 ` [PATCH 6/7] rust: sync: introduce `UniqueArc` Wedson Almeida Filho
2022-12-28  7:19   ` Laine Taffin Altman
2022-12-31 19:46     ` Gary Guo
2022-12-28 10:04   ` Alice Ryhl
2022-12-31 19:47   ` Gary Guo
2023-01-04 16:21   ` Vincenzo
2022-12-28  6:03 ` [PATCH 7/7] rust: sync: add support for dispatching on Arc and ArcBorrow Wedson Almeida Filho
2022-12-28  7:24   ` Laine Taffin Altman
2022-12-31 19:51     ` Gary Guo
2022-12-28 10:04   ` Alice Ryhl
2022-12-31 19:52   ` Gary Guo
2023-01-04 16:26   ` Vincenzo
2022-12-28  7:09 ` [PATCH 1/7] rust: sync: add `Arc` for ref-counted allocations Laine Taffin Altman
2022-12-28  7:27   ` Wedson Almeida Filho
2022-12-28  7:32     ` Laine Taffin Altman
2022-12-28 10:03 ` Alice Ryhl
2022-12-28 10:14   ` Wedson Almeida Filho
2022-12-28 10:38     ` Alice Ryhl
2022-12-31 19:55 ` Gary Guo
2023-01-04 16:08 ` Vincenzo
2023-01-10 20:22 ` Boqun Feng
2023-01-10 21:20   ` Peter Zijlstra
2023-01-10 21:36     ` Boqun Feng
2023-01-10 21:54       ` Peter Zijlstra
2023-01-16 21:06   ` Boqun Feng
2023-01-16 23:34 ` Miguel Ojeda

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=Y8s1evi1wY1XlVgG@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=emilio@crisal.io \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /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