[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Z8lsetLbHvn-6cai@google.com>
Date: Thu, 6 Mar 2025 09:35:54 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: Andreas Hindborg <a.hindborg@...nel.org>
Cc: Oliver Mangold <oliver.mangold@...me>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <benno.lossin@...ton.me>,
Trevor Gross <tmgross@...ch.edu>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v4] rust: adding UniqueRefCounted and UniqueRef types
On Wed, Mar 05, 2025 at 06:24:56PM +0100, Andreas Hindborg wrote:
> "Alice Ryhl" <aliceryhl@...gle.com> writes:
>
> > On Wed, Mar 5, 2025 at 4:39 PM Andreas Hindborg <a.hindborg@...nel.org> wrote:
> >>
> >> "Alice Ryhl" <aliceryhl@...gle.com> writes:
> >>
> >> > On Wed, Mar 5, 2025 at 3:56 PM Oliver Mangold <oliver.mangold@...me> wrote:
> >> >>
> >> >> Hi Alice,
> >> >>
> >> >> On 250305 1339, Alice Ryhl wrote:
> >> >> > On Wed, Mar 05, 2025 at 11:31:44AM +0000, Oliver Mangold wrote:
> >> >> >
> >> >> > > +impl<T: UniqueRefCounted> Deref for UniqueRef<T> {
> >> >> > > + type Target = T;
> >> >> > > +
> >> >> > > + fn deref(&self) -> &Self::Target {
> >> >> > > + // SAFETY: The type invariants guarantee that the object is valid.
> >> >> > > + unsafe { self.ptr.as_ref() }
> >> >> > > + }
> >> >> > > +}
> >> >> >
> >> >> > What stops people from doing this?
> >> >> >
> >> >> > let my_unique: UniqueRef<T> = ...;
> >> >> > let my_ref: &T = &*my_unique;
> >> >> > let my_shared: ARef<T> = ARef::from(my_ref);
> >> >> >
> >> >> > Now it is no longer unique.
> >> >> >
> >> >> Oh, indeed. That's a serious problem. I see 2 options to deal with that:
> >> >>
> >> >> 1. remove ARef::From<&T>
> >> >>
> >> >> I checked the users of this, and it looks to me like there is rather
> >> >> a limited number and they are easy to fix by replacing the &T with ARef<T>.
> >> >> But I assume that wouldn't be welcome as it is intrusive nonetheless
> >> >> and of course there is ergonomic value in having the function around.
> >> >
> >> > Definitely not an option. There are many users of this function that
> >> > are in the process of being upstreamed. The ability to go &T ->
> >> > ARef<T> is pretty fundamental for ARef.
> >>
> >> Not having `impl From<&T> for UniqueArc` seems to work out fine.
> >>
> >> It would be unfortunate if `impl From<&T> for ARef<T>` would prevent us
> >> from having a unique version of `ARef`. I would say that is a valid
> >> reason to consider removing that impl.
> >
> > I think the impl is really important. It's required to do things such as:
> >
> > let mm = ARef::from(&*current!().mm());
> >
> > Without the impl (or something equivalent), it's not possible to
> > increment the refcount of the &Mm returned by `current!().mm()`. There
> > are many other examples of this.
>
> Right. Let's see what we can figure out of other solutions then.
Ultimately, if a struct implements AlwaysRefcounted, then you can always
increments its refcount. If you want a version of the struct where that
is not the case, then you need a different struct that does *not*
implement AlwaysRefcounted.
I do things like that in the mm_struct series. The VmaNew struct is an
example of that.
Alice
Powered by blists - more mailing lists