From: Daniel Almeida <daniel.almeida@collabora.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: "Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
lyude@redhat.com, "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>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Valentin Obst" <kernel@valentinobst.de>,
"open list" <linux-kernel@vger.kernel.org>,
"Marek Szyprowski" <m.szyprowski@samsung.com>,
"Robin Murphy" <robin.murphy@arm.com>,
airlied@redhat.com, rust-for-linux@vger.kernel.org,
"open list:DMA MAPPING HELPERS" <iommu@lists.linux.dev>,
"Petr Tesarik" <petr@tesarici.cz>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Herbert Xu" <herbert@gondor.apana.org.au>,
"Sui Jingfeng" <sui.jingfeng@linux.dev>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Michael Kelley" <mhklinux@outlook.com>
Subject: Re: [RFC PATCH 1/2] rust: add initial scatterlist bindings
Date: Fri, 16 May 2025 13:57:59 -0300 [thread overview]
Message-ID: <E2F129BE-777A-4DBD-BC3E-44033BCD11E4@collabora.com> (raw)
In-Reply-To: <aCZYcs6Aj-cz81qs@pollux>
Hi Danilo,
Replying to you and Lyude here at the same time.
> On 15 May 2025, at 18:11, Danilo Krummrich <dakr@kernel.org> wrote:
>
> On Mon, May 12, 2025 at 08:39:36AM -0300, Daniel Almeida wrote:
>>> On 12 May 2025, at 06:53, Abdiel Janulgue <abdiel.janulgue@gmail.com> wrote:
>>> +impl SGEntry {
>>> + /// Convert a raw `struct scatterlist *` to a `&'a SGEntry`.
>>> + ///
>>> + /// # Safety
>>> + ///
>>> + /// Callers must ensure that the `struct scatterlist` pointed to by `ptr` is valid for the lifetime
>>> + /// of the returned reference.
>>> + pub unsafe fn as_ref<'a>(ptr: *mut bindings::scatterlist) -> &'a Self {
>>> + // SAFETY: The pointer is valid and guaranteed by the safety requirements of the function.
>>> + unsafe { &*ptr.cast() }
>>> + }
>>
>> Hmm, this name is not good. When people see as_ref() they will think of the
>> standard library where it is used to convert from &T to &U. This is not what is
>> happening here. Same in other places where as_ref() is used in this patch.
>
> as_ref() is fine, we use this exact way commonly in the kernel, e.g. for Device,
> GlobalLockedBy, Cpumask and for various DRM types.
>
> Rust std does the same, e.g. in [1].
>
> I think I also asked for this in your Resource patch for consistency, where you
> call this from_ptr() instead.
>
> [1] https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.as_ref
>
That is not the same thing. What you've linked to still takes &self and returns
&T. In order words, it converts *from* &self to another type:
pub trait AsRef<T>
where
T: ?Sized,
{
// Required method
fn as_ref(&self) -> &T;
}
The signature in the kernel is different, i.e.:
fn as_ref<'a>(ptr: *mut bindings::scatterlist) -> &'a Self
This takes a pointer and converts *to* &self, which is a bit in the wrong
direction IMHO. You also have to provide `ptr`, which is a departure from the
syntax used elsewhere in Rust, i.e.:
// no explicit arguments:
let bar: &Bar = foo.as_ref();
vs
// slightly confusing:
//
// Bar::as_ref() creates &Bar instead of taking it as an argument to return something else
let bar = Bar::as_ref(foo_ptr);
Which is more akin to how the "from" prefix works, starting from the From trait
itself, i.e.:
let bar = Bar::from(...); // Ok: creates Bar,
...to other similar nomenclatures:
let bar = Bar::from_ptr(foo_ptr); // Ok, creates &Bar
So, IMHO, the problem is not conflicting with the std AsRef, in the sense that the
code might not compile because of it. The problem is taking a very well
known name and then changing its semantics.
Anyways, this is just a small observation. I'll drop my case, specially
considering that the current as_ref() is already prevalent in a lot of code upstream :)
— Daniel
next prev parent reply other threads:[~2025-05-16 16:59 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-12 9:53 [RFC PATCH 0/2] scatterlist rust bindings Abdiel Janulgue
2025-05-12 9:53 ` [RFC PATCH 1/2] rust: add initial scatterlist bindings Abdiel Janulgue
2025-05-12 11:39 ` Daniel Almeida
2025-05-15 19:26 ` Lyude Paul
2025-05-15 21:11 ` Danilo Krummrich
2025-05-16 16:57 ` Daniel Almeida [this message]
2025-05-16 17:55 ` Danilo Krummrich
2025-05-12 16:42 ` Jason Gunthorpe
2025-05-12 20:01 ` Daniel Almeida
2025-05-12 20:10 ` Danilo Krummrich
2025-05-14 8:29 ` Alexandre Courbot
2025-05-14 12:50 ` Alexandre Courbot
2025-05-16 7:52 ` Abdiel Janulgue
2025-05-15 20:01 ` Lyude Paul
2025-05-16 7:52 ` Abdiel Janulgue
2025-05-26 13:04 ` Abdiel Janulgue
2025-05-12 9:53 ` [RFC PATCH 2/2] samples: rust: add sample code for " Abdiel Janulgue
2025-05-12 11:19 ` [RFC PATCH 0/2] scatterlist rust bindings Daniel Almeida
2025-05-14 7:00 ` Abdiel Janulgue
2025-05-14 12:12 ` Marek Szyprowski
2025-05-16 7:47 ` Abdiel Janulgue
2025-05-13 2:19 ` Herbert Xu
2025-05-13 5:50 ` Christoph Hellwig
2025-05-13 7:38 ` Petr Tesařík
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=E2F129BE-777A-4DBD-BC3E-44033BCD11E4@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=airlied@redhat.com \
--cc=akpm@linux-foundation.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=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=herbert@gondor.apana.org.au \
--cc=iommu@lists.linux.dev \
--cc=kernel@valentinobst.de \
--cc=linux-kernel@vger.kernel.org \
--cc=lyude@redhat.com \
--cc=m.szyprowski@samsung.com \
--cc=mhklinux@outlook.com \
--cc=ojeda@kernel.org \
--cc=petr@tesarici.cz \
--cc=rdunlap@infradead.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=sui.jingfeng@linux.dev \
--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
all inboxes | Powered by JetHome®