rust: transmute: add `as_bytes` method for `AsBytes` trait
Subsystems (2)
Alexandre Courbot
reviewerPATCH 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
@@ -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
@@ -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]>
Alice Ryhl
reviewerQuoted 5 lines · Alexandre Courbot
Every time that implements
AsBytesshould be able to provide its byte representation. Introduce theas_bytesmethod that returns the implementer as a stream of bytes.
Quoted 7 lines
Since types implementing
Sizedcan trivially be represented as a stream of bytes, introduce theAsBytesSizedproxy trait that can be implemented for anySizedtype and provides anAsBytesimplementation suitable for such types. Types that are notSizedneed to implementAsBytesdirectly and provide a method implementation.Signed-off-by Alexandre Courbot <[email protected]>
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
Alexandre Courbot
reviewerQuoted 16 lines · Alice Ryhl
Quoted 5 lines · Alexandre Courbot
Every time that implements
AsBytesshould be able to provide its byte representation. Introduce theas_bytesmethod that returns the implementer as a stream of bytes.Quoted 7 lines
Since types implementing
Sizedcan trivially be represented as a stream of bytes, introduce theAsBytesSizedproxy trait that can be implemented for anySizedtype and provides anAsBytesimplementation suitable for such types. Types that are notSizedneed to implementAsBytesdirectly and provide a method implementation.Signed-off-by Alexandre Courbot <[email protected]>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. ^_^;