[PATCH 1/2] rust: debugfs: migrate BinaryWriter requirements to zerocopy
From: Josef Ippisch via B4 Relay
Date: Sun Jul 19 2026 - 04:29:38 EST
From: Josef Ippisch <josef.ippisch.dev@xxxxxxxxxxx>
Migrate `BinaryWriter`'s default implementation's requirements on T from
`kernel::transmute::AsBytes` to `zerocopy::Immutable` +
`zerocopy::IntoBytes`.
The additional `zerocopy::Immutable` requirement does not further
restrict the types in practice but is rather a more explicit requirement
(that the type does not have interior mutability) and is required by
zerocopy for the `as_bytes()` function.
Suggested-by: Joshua Liebow-Feeser <joshlf@xxxxxxxxxx>
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/975
Signed-off-by: Josef Ippisch <josef.ippisch.dev@xxxxxxxxxxx>
---
rust/kernel/debugfs/traits.rs | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs
index 8c39524b6a99..ce030ff45a4c 100644
--- a/rust/kernel/debugfs/traits.rs
+++ b/rust/kernel/debugfs/traits.rs
@@ -36,6 +36,11 @@
str::FromStr,
};
+use zerocopy::{
+ Immutable,
+ IntoBytes, //
+};
+
/// A trait for types that can be written into a string.
///
/// This works very similarly to `Debug`, and is automatically implemented if `Debug` is
@@ -76,8 +81,8 @@ fn write_to_slice(
) -> Result<usize>;
}
-// Base implementation for any `T: AsBytes`.
-impl<T: AsBytes> BinaryWriter for T {
+// Base implementation for any `T: Immutable + IntoBytes`.
+impl<T: Immutable + IntoBytes> BinaryWriter for T {
fn write_to_slice(
&self,
writer: &mut UserSliceWriter,
@@ -147,7 +152,7 @@ fn write_to_slice(
// Delegate for `Vec<T, A>`.
impl<T, A> BinaryWriter for Vec<T, A>
where
- T: AsBytes,
+ T: Immutable + IntoBytes,
A: Allocator,
{
fn write_to_slice(
@@ -157,7 +162,7 @@ fn write_to_slice(
) -> Result<usize> {
let slice = self.as_slice();
- // SAFETY: `T: AsBytes` allows us to treat `&[T]` as `&[u8]`.
+ // SAFETY: `T: Immutable + IntoBytes` allows us to treat `&[T]` as `&[u8]`.
let buffer = unsafe {
core::slice::from_raw_parts(slice.as_ptr().cast(), core::mem::size_of_val(slice))
};
--
2.53.0