[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLgj-crrKyZbgPBLyWNmzw_oB6ZVc==rqAFt5jr6402nHLA@mail.gmail.com>
Date: Fri, 25 Jul 2025 09:52:44 +0200
From: Alice Ryhl <aliceryhl@...gle.com>
To: Alexandre Courbot <acourbot@...dia.com>
Cc: Abdiel Janulgue <abdiel.janulgue@...il.com>, Danilo Krummrich <dakr@...nel.org>,
Daniel Almeida <daniel.almeida@...labora.com>, Robin Murphy <robin.murphy@....com>,
Andreas Hindborg <a.hindborg@...nel.org>, 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 <lossin@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
"Christian S. Lima" <christiansantoslima21@...il.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: transmute: add `as_bytes` method for `AsBytes` trait
On Fri, Jul 25, 2025 at 4:11 AM Alexandre Courbot <acourbot@...dia.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@...dia.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) }
}
}
Alice
Powered by blists - more mailing lists