From: Josef Ippisch via B4 Relay <devnull+josef.ippisch.dev.mailbox.org@kernel.org>
To: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Danilo Krummrich" <dakr@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: driver-core@lists.linux.dev, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org,
Joshua Liebow-Feeser <joshlf@google.com>,
Miguel Ojeda <ojeda@kernel.org>,
Josef Ippisch <josef.ippisch.dev@mailbox.org>
Subject: [PATCH v2 2/2] rust: debugfs: remove unsafe blocks from traits impl for Vec
Date: Tue, 28 Jul 2026 19:31:20 +0200 [thread overview]
Message-ID: <20260728-migrate-binarywriter-to-zerocopy-intobytes-v2-2-0a4ec1d3ead4@mailbox.org> (raw)
In-Reply-To: <20260728-migrate-binarywriter-to-zerocopy-intobytes-v2-0-0a4ec1d3ead4@mailbox.org>
From: Josef Ippisch <josef.ippisch.dev@mailbox.org>
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 zerocopy's trait
functions `as_bytes()` and `as_mut_bytes()`, respectively, and making use
of deref coercion to implicitly cast Vec to &[T] as `FromBytes` and
`IntoBytes` automatically are implemented on [T] when they are implemented
on T.
Signed-off-by: Josef Ippisch <josef.ippisch.dev@mailbox.org>
---
rust/kernel/debugfs/traits.rs | 21 ++-------------------
1 file changed, 2 insertions(+), 19 deletions(-)
diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs
index b794f7dd7e14..b295f8420abd 100644
--- a/rust/kernel/debugfs/traits.rs
+++ b/rust/kernel/debugfs/traits.rs
@@ -153,14 +153,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)
}
}
@@ -261,17 +254,7 @@ fn read_from_slice_mut(
reader: &mut UserSliceReader,
offset: &mut file::Offset,
) -> Result<usize> {
- let slice = self.as_mut_slice();
-
- // 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(),
- core::mem::size_of_val(slice),
- )
- };
-
- reader.read_slice_file(buffer, offset)
+ reader.read_slice_file(self.as_mut_bytes(), offset)
}
}
--
2.53.0
next prev parent reply other threads:[~2026-07-28 17:31 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 17:31 [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Josef Ippisch via B4 Relay
2026-07-28 17:31 ` [PATCH v2 1/2] " Josef Ippisch via B4 Relay
2026-07-28 17:31 ` Josef Ippisch via B4 Relay [this message]
2026-07-31 9:09 ` [PATCH v2 0/2] " Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260728-migrate-binarywriter-to-zerocopy-intobytes-v2-2-0a4ec1d3ead4@mailbox.org \
--to=devnull+josef.ippisch.dev.mailbox.org@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=driver-core@lists.linux.dev \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=josef.ippisch.dev@mailbox.org \
--cc=joshlf@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®