mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: "Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Andreas Hindborg" <a.hindborg@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>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Christian S. Lima" <christiansantoslima21@gmail.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: transmute: add `as_bytes` method for `AsBytes` trait
Date: Fri, 25 Jul 2025 21:12:21 +0900	[thread overview]
Message-ID: <DBL4KH9KRMW4.2CG55BFPM0RXN@nvidia.com> (raw)
In-Reply-To: <CAH5fLgj-crrKyZbgPBLyWNmzw_oB6ZVc==rqAFt5jr6402nHLA@mail.gmail.com>

On Fri Jul 25, 2025 at 4:52 PM JST, Alice Ryhl wrote:
> On Fri, Jul 25, 2025 at 4:11 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> Every time that implements `AsBytes` should be able to provide its byte
>> representation. Introduce the `as_bytes` method that returns the
>> implementer as a stream of bytes.
>>
>> Since types implementing `Sized` can trivially be represented as a
>> stream of bytes, introduce the `AsBytesSized` proxy trait that can be
>> implemented for any `Sized` type and provides an `AsBytes`
>> implementation suitable for such types. Types that are not `Sized` need
>> to implement `AsBytes` directly and provide a method implementation.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>
> Is the AsBytesSized trait necessary? Can't we just do this?
>
> pub unsafe trait AsBytes {
>     /// Returns `self` as a slice of bytes.
>     fn as_bytes(&self) -> &[u8] {
>         let size = size_of_val(self);
>         let ptr = self as *const Self as *const u8;
>         unsafe { slice::from_raw_parts(ptr, size) }
>     }
> }

Wow, I was completely oblivious of the fact that `size_of_val` also
worked with non-Sized types! Yes, this works perfectly well and is much
less intrusive and overall way better. Thanks a lot.

... now it also makes me anxious about why we didn't include this
method when the trait was introduced, since it is so simple. ^_^;

      reply	other threads:[~2025-07-25 12:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-25  2:11 Alexandre Courbot
2025-07-25  7:52 ` Alice Ryhl
2025-07-25 12:12   ` Alexandre Courbot [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=DBL4KH9KRMW4.2CG55BFPM0RXN@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=christiansantoslima21@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --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

all inboxes | Powered by JetHome®