rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait
@ 2025-07-28 12:47 Alexandre Courbot
  2025-07-28 12:47 ` [PATCH v4 1/2] rust: transmute: add `as_bytes` method " Alexandre Courbot
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Alexandre Courbot @ 2025-07-28 12:47 UTC (permalink / raw)
  To: Abdiel Janulgue, Danilo Krummrich, Daniel Almeida, Robin Murphy,
	Andreas Hindborg, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross
  Cc: Christian S. Lima, rust-for-linux, linux-kernel,
	Alexandre Courbot

This is the sister patch of [1], providing an `as_bytes` method for
`AsBytes`, and an `as_bytes_mut` accessor for types also implementing
`FromBytes`.

It is going to be used in Nova, but should also be universally useful -
if anything, it felt a bit strange that `AsBytes` did not provide this
so far.

[1] https://lore.kernel.org/rust-for-linux/20250624042802.105623-1-christiansantoslima21@gmail.com/

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Changes in v4:
- Add the `as_bytes_mut` method for types also implementing `FromBytes`.
  (thanks Alice!)
- Link to v3: https://lore.kernel.org/r/20250726-as_bytes-v3-1-eb7514faab28@nvidia.com

Changes in v3:
- Use `ptr::from_ref` instead of `as *const T`.
- Link to v2: https://lore.kernel.org/r/20250725-as_bytes-v2-1-c6584c211a6c@nvidia.com

Changes in v2:
- Use `size_of_val` to provide a default implementation for both `Sized`
  and non-`Sized` types, and remove `AsBytesSized`. (thanks Alice!)
- Link to v1: https://lore.kernel.org/r/20250725-as_bytes-v1-1-6f06a3744f69@nvidia.com

---
Alexandre Courbot (2):
      rust: transmute: add `as_bytes` method for `AsBytes` trait
      rust: transmute: add `as_bytes_mut` method to `AsBytes` trait

 rust/kernel/transmute.rs | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
---
base-commit: 14ae91a81ec8fa0bc23170d4aa16dd2a20d54105
change-id: 20250725-as_bytes-6cbc11f2e8c3

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v4 1/2] rust: transmute: add `as_bytes` method for `AsBytes` trait
  2025-07-28 12:47 [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait Alexandre Courbot
@ 2025-07-28 12:47 ` Alexandre Courbot
  2025-07-28 14:30   ` Benno Lossin
  2025-07-28 12:47 ` [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to " Alexandre Courbot
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Alexandre Courbot @ 2025-07-28 12:47 UTC (permalink / raw)
  To: Abdiel Janulgue, Danilo Krummrich, Daniel Almeida, Robin Murphy,
	Andreas Hindborg, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross
  Cc: Christian S. Lima, rust-for-linux, linux-kernel,
	Alexandre Courbot

Every type 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, and provide a default implementation
that should be suitable for any type that satisfies `AsBytes`'s safety
requirements.

Reviewed-by: Danilo Krummrich <dakr@kernel.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/transmute.rs | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index 1c7d43771a37b90150de86699f114a2ffb84db91..d541c9960904d8c7f755351f22d06e4e8dbd546a 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -47,7 +47,16 @@ macro_rules! impl_frombytes {
 ///
 /// Values of this type may not contain any uninitialized bytes. This type must not have interior
 /// mutability.
-pub unsafe trait AsBytes {}
+pub unsafe trait AsBytes {
+    /// Returns `self` as a slice of bytes.
+    fn as_bytes(&self) -> &[u8] {
+        let data = core::ptr::from_ref(self).cast::<u8>();
+        let len = size_of_val(self);
+
+        // SAFETY: `data` is non-null and valid for reads over `len * sizeof::<u8>()` bytes.
+        unsafe { core::slice::from_raw_parts(data, len) }
+    }
+}
 
 macro_rules! impl_asbytes {
     ($($({$($generics:tt)*})? $t:ty, )*) => {

-- 
2.50.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to `AsBytes` trait
  2025-07-28 12:47 [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait Alexandre Courbot
  2025-07-28 12:47 ` [PATCH v4 1/2] rust: transmute: add `as_bytes` method " Alexandre Courbot
@ 2025-07-28 12:47 ` Alexandre Courbot
  2025-07-28 13:25   ` Danilo Krummrich
  2025-07-28 14:53   ` Benno Lossin
  2025-07-28 12:53 ` [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for " Alice Ryhl
  2025-07-28 13:50 ` Gary Guo
  3 siblings, 2 replies; 10+ messages in thread
From: Alexandre Courbot @ 2025-07-28 12:47 UTC (permalink / raw)
  To: Abdiel Janulgue, Danilo Krummrich, Daniel Almeida, Robin Murphy,
	Andreas Hindborg, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross
  Cc: Christian S. Lima, rust-for-linux, linux-kernel,
	Alexandre Courbot

Types that implement both `AsBytes` and `FromBytes` can be safely
modified as a slice of bytes. Add a `as_bytes_mut` method for that
purpose.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/transmute.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index d541c9960904d8c7f755351f22d06e4e8dbd546a..000fda2a78f8e6e8378bbe93cddd8a5008db20cc 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -56,6 +56,19 @@ fn as_bytes(&self) -> &[u8] {
         // SAFETY: `data` is non-null and valid for reads over `len * sizeof::<u8>()` bytes.
         unsafe { core::slice::from_raw_parts(data, len) }
     }
+
+    /// Returns `self` as a mutable slice of bytes.
+    fn as_bytes_mut(&mut self) -> &mut [u8]
+    where
+        Self: FromBytes,
+    {
+        let data = core::ptr::from_mut(self).cast::<u8>();
+        let len = size_of_val(self);
+
+        // SAFETY: `data` is non-null and valid for read and writes over `len * sizeof::<u8>()`
+        // bytes. Since `Self` implements `FromBytes` it can be represented by any value.
+        unsafe { core::slice::from_raw_parts_mut(data, len) }
+    }
 }
 
 macro_rules! impl_asbytes {

-- 
2.50.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait
  2025-07-28 12:47 [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait Alexandre Courbot
  2025-07-28 12:47 ` [PATCH v4 1/2] rust: transmute: add `as_bytes` method " Alexandre Courbot
  2025-07-28 12:47 ` [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to " Alexandre Courbot
@ 2025-07-28 12:53 ` Alice Ryhl
  2025-07-28 13:50 ` Gary Guo
  3 siblings, 0 replies; 10+ messages in thread
From: Alice Ryhl @ 2025-07-28 12:53 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Abdiel Janulgue, Danilo Krummrich, Daniel Almeida, Robin Murphy,
	Andreas Hindborg, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Trevor Gross,
	Christian S. Lima, rust-for-linux, linux-kernel

On Mon, Jul 28, 2025 at 09:47:50PM +0900, Alexandre Courbot wrote:
> This is the sister patch of [1], providing an `as_bytes` method for
> `AsBytes`, and an `as_bytes_mut` accessor for types also implementing
> `FromBytes`.
> 
> It is going to be used in Nova, but should also be universally useful -
> if anything, it felt a bit strange that `AsBytes` did not provide this
> so far.
> 
> [1] https://lore.kernel.org/rust-for-linux/20250624042802.105623-1-christiansantoslima21@gmail.com/
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> Changes in v4:
> - Add the `as_bytes_mut` method for types also implementing `FromBytes`.
>   (thanks Alice!)
> - Link to v3: https://lore.kernel.org/r/20250726-as_bytes-v3-1-eb7514faab28@nvidia.com
> 
> Changes in v3:
> - Use `ptr::from_ref` instead of `as *const T`.
> - Link to v2: https://lore.kernel.org/r/20250725-as_bytes-v2-1-c6584c211a6c@nvidia.com
> 
> Changes in v2:
> - Use `size_of_val` to provide a default implementation for both `Sized`
>   and non-`Sized` types, and remove `AsBytesSized`. (thanks Alice!)
> - Link to v1: https://lore.kernel.org/r/20250725-as_bytes-v1-1-6f06a3744f69@nvidia.com
> 
> ---
> Alexandre Courbot (2):
>       rust: transmute: add `as_bytes` method for `AsBytes` trait
>       rust: transmute: add `as_bytes_mut` method to `AsBytes` trait

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to `AsBytes` trait
  2025-07-28 12:47 ` [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to " Alexandre Courbot
@ 2025-07-28 13:25   ` Danilo Krummrich
  2025-07-28 14:53   ` Benno Lossin
  1 sibling, 0 replies; 10+ messages in thread
From: Danilo Krummrich @ 2025-07-28 13:25 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Abdiel Janulgue, Daniel Almeida, Robin Murphy, Andreas Hindborg,
	Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross,
	Christian S. Lima, rust-for-linux, linux-kernel

On 7/28/25 2:47 PM, Alexandre Courbot wrote:
> Types that implement both `AsBytes` and `FromBytes` can be safely
> modified as a slice of bytes. Add a `as_bytes_mut` method for that
> purpose.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Reviewed-by: Danilo Krummrich <dakr@kernel.org>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait
  2025-07-28 12:47 [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait Alexandre Courbot
                   ` (2 preceding siblings ...)
  2025-07-28 12:53 ` [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for " Alice Ryhl
@ 2025-07-28 13:50 ` Gary Guo
  3 siblings, 0 replies; 10+ messages in thread
From: Gary Guo @ 2025-07-28 13:50 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Abdiel Janulgue, Danilo Krummrich, Daniel Almeida, Robin Murphy,
	Andreas Hindborg, Miguel Ojeda, Alex Gaynor, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross,
	Christian S. Lima, rust-for-linux, linux-kernel

On Mon, 28 Jul 2025 21:47:50 +0900
Alexandre Courbot <acourbot@nvidia.com> wrote:

> This is the sister patch of [1], providing an `as_bytes` method for
> `AsBytes`, and an `as_bytes_mut` accessor for types also implementing
> `FromBytes`.
> 
> It is going to be used in Nova, but should also be universally useful -
> if anything, it felt a bit strange that `AsBytes` did not provide this
> so far.
> 
> [1] https://lore.kernel.org/rust-for-linux/20250624042802.105623-1-christiansantoslima21@gmail.com/
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> Changes in v4:
> - Add the `as_bytes_mut` method for types also implementing `FromBytes`.
>   (thanks Alice!)
> - Link to v3: https://lore.kernel.org/r/20250726-as_bytes-v3-1-eb7514faab28@nvidia.com
> 
> Changes in v3:
> - Use `ptr::from_ref` instead of `as *const T`.
> - Link to v2: https://lore.kernel.org/r/20250725-as_bytes-v2-1-c6584c211a6c@nvidia.com
> 
> Changes in v2:
> - Use `size_of_val` to provide a default implementation for both `Sized`
>   and non-`Sized` types, and remove `AsBytesSized`. (thanks Alice!)
> - Link to v1: https://lore.kernel.org/r/20250725-as_bytes-v1-1-6f06a3744f69@nvidia.com
> 
> ---
> Alexandre Courbot (2):
>       rust: transmute: add `as_bytes` method for `AsBytes` trait
>       rust: transmute: add `as_bytes_mut` method to `AsBytes` trait
> 
>  rust/kernel/transmute.rs | 24 +++++++++++++++++++++++-
>  1 file changed, 23 insertions(+), 1 deletion(-)
> ---
> base-commit: 14ae91a81ec8fa0bc23170d4aa16dd2a20d54105
> change-id: 20250725-as_bytes-6cbc11f2e8c3
> 
> Best regards,

Reviewed-by: Gary Guo <gary@garyguo.net>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 1/2] rust: transmute: add `as_bytes` method for `AsBytes` trait
  2025-07-28 12:47 ` [PATCH v4 1/2] rust: transmute: add `as_bytes` method " Alexandre Courbot
@ 2025-07-28 14:30   ` Benno Lossin
  0 siblings, 0 replies; 10+ messages in thread
From: Benno Lossin @ 2025-07-28 14:30 UTC (permalink / raw)
  To: Alexandre Courbot, Abdiel Janulgue, Danilo Krummrich,
	Daniel Almeida, Robin Murphy, Andreas Hindborg, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Alice Ryhl, Trevor Gross
  Cc: Christian S. Lima, rust-for-linux, linux-kernel

On Mon Jul 28, 2025 at 2:47 PM CEST, Alexandre Courbot wrote:
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index 1c7d43771a37b90150de86699f114a2ffb84db91..d541c9960904d8c7f755351f22d06e4e8dbd546a 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -47,7 +47,16 @@ macro_rules! impl_frombytes {
>  ///
>  /// Values of this type may not contain any uninitialized bytes. This type must not have interior
>  /// mutability.
> -pub unsafe trait AsBytes {}
> +pub unsafe trait AsBytes {
> +    /// Returns `self` as a slice of bytes.
> +    fn as_bytes(&self) -> &[u8] {
> +        let data = core::ptr::from_ref(self).cast::<u8>();

Let's add a `CAST` comment above this that says:

    // CAST: `Self` implements `AsBytes` thus all bytes of `self` are initialized.

> +        let len = size_of_val(self);
> +
> +        // SAFETY: `data` is non-null and valid for reads over `len * sizeof::<u8>()` bytes.

s/over/of/

---
Cheers,
Benno

> +        unsafe { core::slice::from_raw_parts(data, len) }
> +    }
> +}
>  
>  macro_rules! impl_asbytes {
>      ($($({$($generics:tt)*})? $t:ty, )*) => {


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to `AsBytes` trait
  2025-07-28 12:47 ` [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to " Alexandre Courbot
  2025-07-28 13:25   ` Danilo Krummrich
@ 2025-07-28 14:53   ` Benno Lossin
  2025-07-28 14:59     ` Boqun Feng
  1 sibling, 1 reply; 10+ messages in thread
From: Benno Lossin @ 2025-07-28 14:53 UTC (permalink / raw)
  To: Alexandre Courbot, Abdiel Janulgue, Danilo Krummrich,
	Daniel Almeida, Robin Murphy, Andreas Hindborg, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Alice Ryhl, Trevor Gross
  Cc: Christian S. Lima, rust-for-linux, linux-kernel

On Mon Jul 28, 2025 at 2:47 PM CEST, Alexandre Courbot wrote:
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index d541c9960904d8c7f755351f22d06e4e8dbd546a..000fda2a78f8e6e8378bbe93cddd8a5008db20cc 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -56,6 +56,19 @@ fn as_bytes(&self) -> &[u8] {
>          // SAFETY: `data` is non-null and valid for reads over `len * sizeof::<u8>()` bytes.
>          unsafe { core::slice::from_raw_parts(data, len) }
>      }
> +
> +    /// Returns `self` as a mutable slice of bytes.
> +    fn as_bytes_mut(&mut self) -> &mut [u8]
> +    where
> +        Self: FromBytes,
> +    {
> +        let data = core::ptr::from_mut(self).cast::<u8>();

Also add a `CAST` comment above this line:

    // CAST: `Self` implements both `AsBytes` and `FromBytes` thus making `Self` bi-directionally
    // transmutable to `[u8; size_of_val(self)]`.

On that note, `FromBytes` probably entails `Sized`, or does someone have
a use-case in mind? (because then we could replace the
`size_of_val(self)` above with `size_of::<Self>()` which I think makes
it simpler to understand)

> +        let len = size_of_val(self);
> +
> +        // SAFETY: `data` is non-null and valid for read and writes over `len * sizeof::<u8>()`

s/over/of/

> +        // bytes. Since `Self` implements `FromBytes` it can be represented by any value.

Don't need the second sentence due to the `CAST` comment above.

---
Cheers,
Benno

> +        unsafe { core::slice::from_raw_parts_mut(data, len) }
> +    }
>  }
>  
>  macro_rules! impl_asbytes {


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to `AsBytes` trait
  2025-07-28 14:53   ` Benno Lossin
@ 2025-07-28 14:59     ` Boqun Feng
  2025-07-28 15:52       ` Benno Lossin
  0 siblings, 1 reply; 10+ messages in thread
From: Boqun Feng @ 2025-07-28 14:59 UTC (permalink / raw)
  To: Benno Lossin
  Cc: Alexandre Courbot, Abdiel Janulgue, Danilo Krummrich,
	Daniel Almeida, Robin Murphy, Andreas Hindborg, Miguel Ojeda,
	Alex Gaynor, Gary Guo, Björn Roy Baron, Alice Ryhl,
	Trevor Gross, Christian S. Lima, rust-for-linux, linux-kernel

On Mon, Jul 28, 2025 at 04:53:31PM +0200, Benno Lossin wrote:
> On Mon Jul 28, 2025 at 2:47 PM CEST, Alexandre Courbot wrote:
> > diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> > index d541c9960904d8c7f755351f22d06e4e8dbd546a..000fda2a78f8e6e8378bbe93cddd8a5008db20cc 100644
> > --- a/rust/kernel/transmute.rs
> > +++ b/rust/kernel/transmute.rs
> > @@ -56,6 +56,19 @@ fn as_bytes(&self) -> &[u8] {
> >          // SAFETY: `data` is non-null and valid for reads over `len * sizeof::<u8>()` bytes.
> >          unsafe { core::slice::from_raw_parts(data, len) }
> >      }
> > +
> > +    /// Returns `self` as a mutable slice of bytes.
> > +    fn as_bytes_mut(&mut self) -> &mut [u8]
> > +    where
> > +        Self: FromBytes,
> > +    {
> > +        let data = core::ptr::from_mut(self).cast::<u8>();
> 
> Also add a `CAST` comment above this line:
> 
>     // CAST: `Self` implements both `AsBytes` and `FromBytes` thus making `Self` bi-directionally
>     // transmutable to `[u8; size_of_val(self)]`.
> 
> On that note, `FromBytes` probably entails `Sized`, or does someone have
> a use-case in mind? (because then we could replace the

Because if `T` is `FromBytes` then we want `[T]` is `FromBytes` too?
Also it would be weird if `[u8]` itself doesn't implement `FromBytes`.

Regards,
Boqun

> `size_of_val(self)` above with `size_of::<Self>()` which I think makes
> it simpler to understand)
> 
[...]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to `AsBytes` trait
  2025-07-28 14:59     ` Boqun Feng
@ 2025-07-28 15:52       ` Benno Lossin
  0 siblings, 0 replies; 10+ messages in thread
From: Benno Lossin @ 2025-07-28 15:52 UTC (permalink / raw)
  To: Boqun Feng
  Cc: Alexandre Courbot, Abdiel Janulgue, Danilo Krummrich,
	Daniel Almeida, Robin Murphy, Andreas Hindborg, Miguel Ojeda,
	Alex Gaynor, Gary Guo, Björn Roy Baron, Alice Ryhl,
	Trevor Gross, Christian S. Lima, rust-for-linux, linux-kernel

On Mon Jul 28, 2025 at 4:59 PM CEST, Boqun Feng wrote:
> On Mon, Jul 28, 2025 at 04:53:31PM +0200, Benno Lossin wrote:
>> On Mon Jul 28, 2025 at 2:47 PM CEST, Alexandre Courbot wrote:
>> > diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
>> > index d541c9960904d8c7f755351f22d06e4e8dbd546a..000fda2a78f8e6e8378bbe93cddd8a5008db20cc 100644
>> > --- a/rust/kernel/transmute.rs
>> > +++ b/rust/kernel/transmute.rs
>> > @@ -56,6 +56,19 @@ fn as_bytes(&self) -> &[u8] {
>> >          // SAFETY: `data` is non-null and valid for reads over `len * sizeof::<u8>()` bytes.
>> >          unsafe { core::slice::from_raw_parts(data, len) }
>> >      }
>> > +
>> > +    /// Returns `self` as a mutable slice of bytes.
>> > +    fn as_bytes_mut(&mut self) -> &mut [u8]
>> > +    where
>> > +        Self: FromBytes,
>> > +    {
>> > +        let data = core::ptr::from_mut(self).cast::<u8>();
>> 
>> Also add a `CAST` comment above this line:
>> 
>>     // CAST: `Self` implements both `AsBytes` and `FromBytes` thus making `Self` bi-directionally
>>     // transmutable to `[u8; size_of_val(self)]`.
>> 
>> On that note, `FromBytes` probably entails `Sized`, or does someone have
>> a use-case in mind? (because then we could replace the
>
> Because if `T` is `FromBytes` then we want `[T]` is `FromBytes` too?
> Also it would be weird if `[u8]` itself doesn't implement `FromBytes`.

Oh yeah that's true.

---
Cheers,
Benno

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-07-28 15:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-28 12:47 [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for `AsBytes` trait Alexandre Courbot
2025-07-28 12:47 ` [PATCH v4 1/2] rust: transmute: add `as_bytes` method " Alexandre Courbot
2025-07-28 14:30   ` Benno Lossin
2025-07-28 12:47 ` [PATCH v4 2/2] rust: transmute: add `as_bytes_mut` method to " Alexandre Courbot
2025-07-28 13:25   ` Danilo Krummrich
2025-07-28 14:53   ` Benno Lossin
2025-07-28 14:59     ` Boqun Feng
2025-07-28 15:52       ` Benno Lossin
2025-07-28 12:53 ` [PATCH v4 0/2] rust: transmute: add `as_bytes(_mut)` methods for " Alice Ryhl
2025-07-28 13:50 ` Gary Guo

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).