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 – Jul 26, 2025 Rust

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alexandre Courbot

author reviewer
#0 lore raw

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

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.

---
This is the sister patch of [1], providing an as_bytes method for AsBytes.

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 method so far.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/
---
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/[email protected]

---
 rust/kernel/transmute.rs | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
rust/kernel/transmute.rs +10 -1
index 1c7d43771a37b90150de86699f114a2ffb84db91..d4036986a3d7fb97e5da3e121e9590ad23b784e9 100644
@@ -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 = (self as *const Self).cast::<u8>();
+        let len = size_of_val(self);
+
+        // SAFETY: `data` is non-null and valid for `len * sizeof::<u8>()` bytes.
+        unsafe { core::slice::from_raw_parts(data, len) }
+    }
+}
 
 macro_rules! impl_asbytes {
     ($($({$($generics:tt)*})? $t:ty, )*) => {

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

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
reviewer Reviewed-by on v2 1/1
#1 lore raw

rust/kernel/transmute.rs macro_rules! impl_frombytes { · line 53

Quoted 13 lines · Alexandre Courbot
rust/kernel/transmute.rs +4 -1
index 1c7d43771a37b90150de86699f114a2ffb84db91..d4036986a3d7fb97e5da3e121e9590ad23b784e9 100644
@@ -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 = (self as *const Self).cast::<u8>();

I'd probably use from_ref() instead. With that,

Quoted 6 lines
rust/kernel/transmute.rs +6 -0
+        let len = size_of_val(self);
+
+        // SAFETY: `data` is non-null and valid for `len * sizeof::<u8>()` bytes.
+        unsafe { core::slice::from_raw_parts(data, len) }
+    }
+}
author reviewer
#2 lore raw
Quoted 16 lines · Danilo Krummrich

rust/kernel/transmute.rs macro_rules! impl_frombytes { · line 53

Quoted 13 lines · Alexandre Courbot
rust/kernel/transmute.rs +4 -1
index 1c7d43771a37b90150de86699f114a2ffb84db91..d4036986a3d7fb97e5da3e121e9590ad23b784e9 100644
@@ -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 = (self as *const Self).cast::<u8>();

I'd probably use from_ref() instead. With that,

Oh yes I always forget about this one...

Quoted 2 lines

Thanks!