[PATCH 2/2] rust: debugfs: remove unsafe block from BinaryWriter impl for Vec
From: Josef Ippisch via B4 Relay
Date: Sun Jul 19 2026 - 04:29:41 EST
From: Josef Ippisch <josef.ippisch.dev@xxxxxxxxxxx>
The previous implementation used an `unsafe` block to manually cast Vec's
slice to a &[u8] using `core::slice::from_raw_parts`.
Instead, the implementation can be implemented in safe rust using
`IntoBytes::as_bytes()` and making use of deref coercion to implicitly cast
Vec to &[T] as `IntoBytes` automatically is implemented on [T] when
`IntoBytes` is implemented on T.
Signed-off-by: Josef Ippisch <josef.ippisch.dev@xxxxxxxxxxx>
---
rust/kernel/debugfs/traits.rs | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs
index ce030ff45a4c..4a24afbb8760 100644
--- a/rust/kernel/debugfs/traits.rs
+++ b/rust/kernel/debugfs/traits.rs
@@ -160,14 +160,7 @@ fn write_to_slice(
writer: &mut UserSliceWriter,
offset: &mut file::Offset,
) -> Result<usize> {
- let slice = self.as_slice();
-
- // 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))
- };
-
- writer.write_slice_file(buffer, offset)
+ writer.write_slice_file(self.as_bytes(), offset)
}
}
--
2.53.0