[PATCH 2/2] rust: debugfs: remove unsafe block from BinaryWriter impl for Vec
Josef Ippisch via B4 Relay <[email protected]>
| Newsgroups | dev.linux.lists.driver-core,org.kernel.feeds.b4-sent,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <20260719-migrate-binarywriter-to-zerocopy-intobytes-v1-2-27ed30512e75@mailbox.org> |
From: Josef Ippisch <[email protected]> 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 <[email protected]> --- 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