From: Oliver Mangold <oliver.mangold@pm.me>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "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" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] rust: adding UniqueRefCounted and UniqueRef types
Date: Wed, 05 Mar 2025 16:15:24 +0000 [thread overview]
Message-ID: <Z8h4mO_deAi_V3mf@laptop> (raw)
In-Reply-To: <CAH5fLgjFBknTmhxQBPUdB-iNMjEkcyuLiu22-Nj-DGB1Gb7NkA@mail.gmail.com>
On 250305 1613, Alice Ryhl wrote:
> On Wed, Mar 5, 2025 at 3:56 PM Oliver Mangold <oliver.mangold@pm.me> wrote:
> > >
> > > 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.
Ok. Suspected something like that.
>
> I do think that you essentially need two structs to use this at all -
> one for the shared and one for the unique case. Sounds pretty
> unergonomic.
>
Sorry, but can you explain? Why does one need structs?
> What is the use-case for these abstractions?
>
It came up in the block subsystem. For mq::Request it is rather essential
to be able to ensure that one has a unique reference.
Maybe Andreas can explain a bit more.
We can work around that there, with special wrappers specific for block,
sure.
But are you sure it isn't much more widely useful? Correct me if I'm wrong,
but as I understand, currently for AlwaysRefCounted objects you can never
safely obtain an &mut. So you cannot use the static safety checks
that come with '&' vs. '&mut'. This means you have to rely on interior
mutability, requiring runtime checks in the wrapper functions, no?
Isn't that, for example, the reason that Page is not AlwaysRefCounted,
as write access is protected by requiring an '&mut'?
Best,
Oliver
prev parent reply other threads:[~2025-03-05 16:15 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-05 11:31 [PATCH v4] rust: adding UniqueRefCounted and UniqueRef types Oliver Mangold
2025-03-05 13:39 ` Alice Ryhl
2025-03-05 14:56 ` Oliver Mangold
2025-03-05 15:13 ` Alice Ryhl
2025-03-05 15:38 ` Andreas Hindborg
2025-03-05 16:02 ` Alice Ryhl
2025-03-05 17:24 ` Andreas Hindborg
2025-03-06 9:35 ` Alice Ryhl
2025-03-06 9:48 ` Andreas Hindborg
2025-03-06 10:14 ` Oliver Mangold
2025-03-06 11:31 ` Alice Ryhl
2025-03-06 12:03 ` Oliver Mangold
2025-03-06 12:08 ` Alice Ryhl
2025-03-05 15:42 ` Andreas Hindborg
2025-03-05 16:15 ` Oliver Mangold [this message]
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=Z8h4mO_deAi_V3mf@laptop \
--to=oliver.mangold@pm.me \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.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).