rust: debugfs: migrate debugfs traits requirements to zerocopy

Migrate `BinaryWriter` and `BinaryReaderMut`'s default implementation's
requirements on T from `kernel::transmute` traits to `zerocopy` traits.

The additional `zerocopy::Immutable` requirement on `BinaryWriter` 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@google.com>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/975
Link: https://github.com/Rust-for-Linux/linux/issues/1241
Signed-off-by: Josef Ippisch <josef.ippisch.dev@mailbox.org>
Link: https://patch.msgid.link/20260728-migrate-binarywriter-to-zerocopy-intobytes-v2-1-0a4ec1d3ead4@mailbox.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Josef Ippisch 2026-07-28 19:31:19 +02:00 committed by Greg Kroah-Hartman
parent 17fcdecb14
commit 6e33dee41d

View File

@ -18,10 +18,6 @@
Arc,
Mutex, //
},
transmute::{
AsBytes,
FromBytes, //
},
uaccess::{
UserSliceReader,
UserSliceWriter, //
@ -36,6 +32,8 @@
str::FromStr,
};
use zerocopy::Immutable;
/// 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 +74,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 +145,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 +155,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))
};
@ -230,14 +228,14 @@ fn read_from_slice_mut(
) -> Result<usize>;
}
// Base implementation for any `T: AsBytes + FromBytes`.
impl<T: AsBytes + FromBytes> BinaryReaderMut for T {
// Base implementation for any `T: FromBytes + IntoBytes`.
impl<T: FromBytes + IntoBytes> BinaryReaderMut for T {
fn read_from_slice_mut(
&mut self,
reader: &mut UserSliceReader,
offset: &mut file::Offset,
) -> Result<usize> {
reader.read_slice_file(self.as_bytes_mut(), offset)
reader.read_slice_file(self.as_mut_bytes(), offset)
}
}
@ -255,7 +253,7 @@ fn read_from_slice_mut(
// Delegate for `Vec<T, A>`: Support a `Vec<T, A>` with an outer lock.
impl<T, A> BinaryReaderMut for Vec<T, A>
where
T: AsBytes + FromBytes,
T: FromBytes + IntoBytes,
A: Allocator,
{
fn read_from_slice_mut(
@ -265,7 +263,7 @@ fn read_from_slice_mut(
) -> Result<usize> {
let slice = self.as_mut_slice();
// SAFETY: `T: AsBytes + FromBytes` allows us to treat `&mut [T]` as `&mut [u8]`.
// SAFETY: `T: FromBytes + IntoBytes` allow us to treat `&mut [T]` as `&mut [u8]`.
let buffer = unsafe {
core::slice::from_raw_parts_mut(
slice.as_mut_ptr().cast(),