From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
"Christian S. Lima" <christiansantoslima21@gmail.com>
Cc: "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 v10] rust: transmute: Add methods for FromBytes trait
Date: Thu, 28 Aug 2025 15:26:47 +0900 [thread overview]
Message-ID: <DCDUIFACG791.3SCF7302W8JZ8@nvidia.com> (raw)
In-Reply-To: <CANiq72kGh2PS-c1n2Q1g0t24J-b3ctB0kN3GK8RDXhtQMGAtTA@mail.gmail.com>
Thanks for the feedback Miguel! For the record, here are the changes I am
planning on applying on top of this version:
On Mon Aug 25, 2025 at 11:51 PM JST, Miguel Ojeda wrote:
> On Sun, Aug 24, 2025 at 11:31 PM Christian S. Lima
> <christiansantoslima21@gmail.com> wrote:
>>
>> Since the `is_aligned` method for pointer types has been stabilized in
>> `1.79` version and is being used in this patch. I'm enabling the
>> feature.
>
> Period -> comma? No need to backquote version numbers.
>
>> Even enabling in `rust/kernel/lib.rs` when compiling with `make LLVM=1
>> CLIPPY=1` a warning is issued, so in order to compile, it was used
>> locally the `#[allow(clippy::incompatible_msrv)]`.
>
> I would perhaps try to reword this a bit.
Reworded this to:
Also add `#[allow(clippy::incompatible_msrv)]` where needed until the
MSRV is updated to `1.79`.
>
>> Link: https://github.com/Rust-for-Linux/linux/issues/1119
>> Suggested-by: Alexandre Courbot <acourbot@nvidia.com>
>
> Please add a link to the original suggestion if possible. If it is the
> link above, then the tags should be in the opposite order.
>
> Did Benno and Alexandre both suggest it?
>
>> +/// fn test() -> Option<()> {
>> +/// let raw = [1, 2, 3, 4];
>> +///
>> +/// let result = u32::from_bytes(&raw)?;
>> +///
>> +/// #[cfg(target_endian = "little")]
>> +/// assert_eq!(*result, 0x4030201);
>> +///
>> +/// #[cfg(target_endian = "big")]
>> +/// assert_eq!(*result, 0x1020304);
>> +///
>> +/// Some(())
>> +/// }
>
> Should the function be called? Otherwise, we are only build-testing this.
>
> Should we just remove the function and to it directly at the top-level?
Changed this to call the function and hide the unneeded parts:
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -16,19 +16,20 @@
/// ```
/// use kernel::transmute::FromBytes;
///
-/// fn test() -> Option<()> {
-/// let raw = [1, 2, 3, 4];
+/// # fn test() -> Option<()> {
+/// let raw = [1, 2, 3, 4];
///
-/// let result = u32::from_bytes(&raw)?;
+/// let result = u32::from_bytes(&raw)?;
///
-/// #[cfg(target_endian = "little")]
-/// assert_eq!(*result, 0x4030201);
+/// #[cfg(target_endian = "little")]
+/// assert_eq!(*result, 0x4030201);
///
-/// #[cfg(target_endian = "big")]
-/// assert_eq!(*result, 0x1020304);
+/// #[cfg(target_endian = "big")]
+/// assert_eq!(*result, 0x1020304);
///
-/// Some(())
-/// }
+/// # Some(()) }
+/// # test().ok_or(EINVAL)?;
+/// # Ok::<(), Error>(())
>
>> + /// Converts a slice of bytes to a reference to `Self`.
>
> An intra-doc link may work here.
Wasn't sure what a link to `Self` might link to in a trait declaration, so left
this one as-is. :)
>
>> + /// In another case, it will return `None`.
>
> Ditto (also elsewhere).
>
> I would perhaps just say "Otherwise," to simplify.
>
>> + #[allow(clippy::incompatible_msrv)]
>
> If this can be made more local, then please do so; otherwise, no big deal.
Moved this closer to where it is needed:
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -41,13 +41,14 @@ pub unsafe trait FromBytes {
/// and is different from zero.
///
/// In another case, it will return `None`.
- #[allow(clippy::incompatible_msrv)]
fn from_bytes(bytes: &[u8]) -> Option<&Self>
where
Self: Sized,
{
let slice_ptr = bytes.as_ptr().cast::<Self>();
let size = size_of::<Self>();
+
+ #[allow(clippy::incompatible_msrv)]
if bytes.len() == size && slice_ptr.is_aligned() {
// SAFETY: Checking for size and alignment ensure
// that the conversion to a type is valid
@@ -63,13 +64,14 @@ fn from_bytes(bytes: &[u8]) -> Option<&Self>
/// is equal to that of `T`and is different from zero.
///
/// In another case, it will return `None`.
- #[allow(clippy::incompatible_msrv)]
fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
where
Self: AsBytes + Sized,
{
let slice_ptr = bytes.as_mut_ptr().cast::<Self>();
let size = size_of::<Self>();
+
+ #[allow(clippy::incompatible_msrv)]
if bytes.len() == size && slice_ptr.is_aligned() {
// SAFETY: Checking for size and alignment ensure
>
>> + // SAFETY: Checking for size and alignment ensure
>> + // that the conversion to a type is valid
>
> Period at the end. I would probably say "Size and alignment were just
> checked, thus ...".
>
>> + /// Converts a mutable slice of bytes to a reference to `Self`
>
> Period at the end, intra-doc link if possible (ditto elsewhere).
Changed the doc/comments as follows:
@@ -38,10 +38,10 @@
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.
+ /// Succeeds if the reference is properly aligned, and the size of `bytes` is equal to that of
+ /// `T` and different from zero.
///
- /// In another case, it will return `None`.
+ /// Otherwise, returns [`None`].
fn from_bytes(bytes: &[u8]) -> Option<&Self>
where
Self: Sized,
@@ -51,20 +51,19 @@ fn from_bytes(bytes: &[u8]) -> Option<&Self>
#[allow(clippy::incompatible_msrv)]
if bytes.len() == size && slice_ptr.is_aligned() {
- // SAFETY: Checking for size and alignment ensure
- // that the conversion to a type is valid
+ // SAFETY: Size and alignment were just checked.
unsafe { Some(&*slice_ptr) }
} else {
None
}
}
- /// Converts a mutable slice of bytes to a reference to `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.
+ /// Succeeds if the reference is properly aligned, and the size of `bytes` is equal to that of
+ /// `T` and different from zero.
///
- /// In another case, it will return `None`.
+ /// Otherwise, returns [`None`].
fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
where
Self: AsBytes + Sized,
@@ -74,8 +73,7 @@ fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
#[allow(clippy::incompatible_msrv)]
if bytes.len() == size && slice_ptr.is_aligned() {
- // SAFETY: Checking for size and alignment ensure
- // that the conversion to a type is valid
+ // SAFETY: Size and alignment were just checked.
unsafe { Some(&mut *slice_ptr) }
} else {
None
Also, as suggested by Alice I am re-enabling the implementation on slices:
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -94,6 +94,7 @@ macro_rules! impl_frombytes {
// 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.
+ {<T: FromBytes>} [T],
{<T: FromBytes, const N: usize>} [T; N],
}
Unless someone says something by then, I plan on pushing this to nova-next
tomorrow.
next prev parent reply other threads:[~2025-08-28 6:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-24 21:31 [PATCH v10] rust: transmute: Add methods for FromBytes trait Christian S. Lima
2025-08-25 10:24 ` Alice Ryhl
2025-08-25 18:51 ` Christian
2025-08-25 10:39 ` Alexandre Courbot
2025-08-25 14:52 ` Miguel Ojeda
2025-08-25 14:51 ` Miguel Ojeda
2025-08-25 19:51 ` Christian
2025-08-28 0:17 ` Alexandre Courbot
2025-08-28 6:26 ` Alexandre Courbot [this message]
2025-08-29 1:50 ` Alexandre Courbot
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=DCDUIFACG791.3SCF7302W8JZ8@nvidia.com \
--to=acourbot@nvidia.com \
--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=miguel.ojeda.sandonis@gmail.com \
--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).