From: "Benno Lossin" <lossin@kernel.org>
To: "Christian S. Lima" <christiansantoslima21@gmail.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>,
"Danilo Krummrich" <dakr@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
~lkcamp/patches@lists.sr.ht, richard120310@gmail.com
Subject: Re: [PATCH v9] rust: transmute: Add methods for FromBytes trait
Date: Thu, 14 Aug 2025 10:06:50 +0200 [thread overview]
Message-ID: <DC1ZVE8AM8GJ.3UZBQBW7HOFE3@kernel.org> (raw)
In-Reply-To: <20250811213851.65644-1-christiansantoslima21@gmail.com>
On Mon Aug 11, 2025 at 11:38 PM CEST, Christian S. Lima wrote:
> @@ -9,27 +11,136 @@
> ///
> /// It's okay for the type to have padding, as initializing those bytes has no effect.
> ///
> +/// # Examples
> +///
> +/// ```
> +/// use kernel::transmute::FromBytes;
> +///
> +/// let foo = [1, 2, 3, 4];
> +///
> +/// let result = u32::from_bytes(&foo)?;
> +///
> +/// #[cfg(target_endian = "little")]
> +/// assert_eq!(*result, 0x4030201);
> +///
> +/// #[cfg(target_endian = "big")]
> +/// assert_eq!(*result, 0x1020304);
> +/// ```
> +///
> +/// # Safety
> +///
> +/// All bit-patterns must be valid for this type. This type must not have interior mutability.
> +pub unsafe trait FromBytes {
> + /// Converts a slice of bytes to a reference to `Self` when the reference
> + /// is properly aligned and the size of slice is equal to that of `T`
> + /// and is different from zero. In another case, it will return
> + ///`None`.
The first line in any documentation should be a single, short sentence.
Also separate it from the contents by a newline, otherwise markdown will
render it as a single paragraph.
> + fn from_bytes(bytes: &[u8]) -> Option<&Self>;
> +
> + /// Converts a mutable slice of bytes to a reference to `Self`
> + /// when the reference is properly aligned and the size of slice
> + /// is equal to that of `T` and is different from zero. In another
> + /// case, it will return `None`.
> + fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
> + where
> + Self: AsBytes;
> +}
> +
> +// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
> +// patterns are also acceptable for arrays of that type.
> +unsafe impl<T: FromBytes> FromBytes for [T] {
> + fn from_bytes(bytes: &[u8]) -> Option<&Self> {
> + let size = ::core::mem::size_of::<T>();
> + build_assert!(size == 0, "Can't create a slice with zero elements");
This message is wrong, it sounds as if the size of `bytes` was zero and
you can definitely create a slice with zero elements.
> + let slice_ptr = bytes.as_ptr().cast::<T>();
> + if bytes.len() % size == 0 && slice_ptr.is_aligned() {
> + // SAFETY: Since the number of elements is different from
> + // zero and the pointer is aligned, the slice is valid.
Please take a look at the documentation of `from_raw_parts`, there are
several bullet points of safety requirements. Please provide a
justification for each one and also place them in a bullet point list.
---
Cheers,
Benno
> + unsafe { Some(::core::slice::from_raw_parts(slice_ptr, bytes.len() / size)) }
> + } else {
> + None
> + }
> + }
> +
> + fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
> + where
> + Self: AsBytes,
> + {
> + let size = ::core::mem::size_of::<T>();
> + build_assert!(size == 0, "Can't create a slice with zero elements");
> + let slice_ptr = bytes.as_mut_ptr().cast::<T>();
> + if bytes.len() % size == 0 && slice_ptr.is_aligned() {
> + // SAFETY: Since the number of elements is different from
> + // zero and the pointer is aligned, the slice is valid.
> + unsafe {
> + Some(::core::slice::from_raw_parts_mut(
> + slice_ptr,
> + bytes.len() / size,
> + ))
> + }
> + } else {
> + None
> + }
> + }
> }
>
> /// Types that can be viewed as an immutable slice of initialized bytes.
prev parent reply other threads:[~2025-08-14 8:06 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-11 21:38 [PATCH v9] rust: transmute: Add methods for FromBytes trait Christian S. Lima
2025-08-12 14:24 ` Alexandre Courbot
2025-08-12 18:00 ` Christian
2025-08-18 11:28 ` Alexandre Courbot
2025-08-18 12:20 ` Alice Ryhl
2025-08-18 19:04 ` Christian
2025-08-21 11:13 ` Alexandre Courbot
2025-08-21 13:50 ` Miguel Ojeda
2025-08-22 3:09 ` Alexandre Courbot
2025-08-22 8:07 ` Miguel Ojeda
2025-08-22 13:49 ` Alexandre Courbot
2025-08-22 20:08 ` Miguel Ojeda
2025-08-13 1:06 ` kernel test robot
2025-08-14 8:06 ` Benno Lossin [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=DC1ZVE8AM8GJ.3UZBQBW7HOFE3@kernel.org \
--to=lossin@kernel.org \
--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=christiansantoslima21@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=richard120310@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=~lkcamp/patches@lists.sr.ht \
/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).