lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DBL4KH9KRMW4.2CG55BFPM0RXN@nvidia.com>
Date: Fri, 25 Jul 2025 21:12:21 +0900
From: "Alexandre Courbot" <acourbot@...dia.com>
To: "Alice Ryhl" <aliceryhl@...gle.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:52 PM JST, Alice Ryhl wrote:
> 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) }
>     }
> }

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. ^_^;

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ