Skip to contentSkip to conversation

rust: transmute: add `as_bytes` method for `AsBytes` trait

PATCH Superseded Alexandre Courbot 2 participants 3 messages Jul 25, 2025

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alexandre Courbot

author reviewer
#0 lore raw

PATCH 1/1 rust: transmute: add `as_bytes` method for `AsBytes` trait

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.

---
This is the sister patch of [1], requiring AsBytes implementors to provide an as_bytes method, and adding the convenience trait AsBytesSized that can be implemented on any sized type in order to benefit from a suitable AsBytes implementation.

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 require this method so far as for unsized types the bytes representation cannot be obviously inferred.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/kernel/dma.rs       |  4 ++--
 rust/kernel/transmute.rs | 48 ++++++++++++++++++++++++++++++++++++++++++------
 samples/rust/rust_dma.rs |  2 +-
 3 files changed, 45 insertions(+), 9 deletions(-)
rust/kernel/dma.rs +2 -2
index 25dfa0e6cc3ce50aa85463dae00fbdebcf8975d2..2ada95fdb41186957ee8dd39d43e473286f01d2c 100644
@@ -439,7 +439,7 @@ unsafe impl<T: AsBytes + FromBytes + Send> Send for CoherentAllocation<T> {}
 /// // SAFETY: All bit patterns are acceptable values for `MyStruct`.
 /// unsafe impl kernel::transmute::FromBytes for MyStruct{};
 /// // SAFETY: Instances of `MyStruct` have no uninitialized portions.
-/// unsafe impl kernel::transmute::AsBytes for MyStruct{};
+/// unsafe impl kernel::transmute::AsBytesSized for MyStruct{};
 ///
 /// # fn test(alloc: &kernel::dma::CoherentAllocation<MyStruct>) -> Result {
 /// let whole = kernel::dma_read!(alloc[2]);
@@ -483,7 +483,7 @@ macro_rules! dma_read {
 /// // SAFETY: All bit patterns are acceptable values for `MyStruct`.
 /// unsafe impl kernel::transmute::FromBytes for MyStruct{};
 /// // SAFETY: Instances of `MyStruct` have no uninitialized portions.
-/// unsafe impl kernel::transmute::AsBytes for MyStruct{};
+/// unsafe impl kernel::transmute::AsBytesSized for MyStruct{};
 ///
 /// # fn test(alloc: &kernel::dma::CoherentAllocation<MyStruct>) -> Result {
 /// kernel::dma_write!(alloc[2].member = 0xf);
rust/kernel/transmute.rs +42 -6

67 lines not on this page · raw message

samples/rust/rust_dma.rs +1 -1
index 9e05d5c0cdaeb3b36fcf204a91b52e382e73fbe6..ab4814c848f928240fc49d57ac6a2efe636fb39f 100644
@@ -30,7 +30,7 @@ fn new(h: u32, b: u32) -> Self {
     }
 }
 // SAFETY: All bit patterns are acceptable values for `MyStruct`.
-unsafe impl kernel::transmute::AsBytes for MyStruct {}
+unsafe impl kernel::transmute::AsBytesSized for MyStruct {}
 // SAFETY: Instances of `MyStruct` have no uninitialized portions.
 unsafe impl kernel::transmute::FromBytes for MyStruct {}
 

---
base-commit: 14ae91a81ec8fa0bc23170d4aa16dd2a20d54105
change-id: 20250725-as_bytes-6cbc11f2e8c3

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
reviewer
#1 lore raw
Quoted 5 lines · Alexandre Courbot

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.

Quoted 7 lines

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.

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

author reviewer
#2 lore raw
Quoted 16 lines · Alice Ryhl
Quoted 5 lines · Alexandre Courbot

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.

Quoted 7 lines

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.

Is the AsBytesSized trait necessary? Can't we just do this?

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