From: Miguel Ojeda <ojeda@kernel.org>
To: Miguel Ojeda <ojeda@kernel.org>,
Alex Gaynor <alex.gaynor@gmail.com>,
Wedson Almeida Filho <wedsonaf@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
patches@lists.linux.dev
Subject: [PATCH 03/19] rust: types: avoid repetition in `{As,From}Bytes` impls
Date: Wed, 4 Sep 2024 22:43:31 +0200 [thread overview]
Message-ID: <20240904204347.168520-4-ojeda@kernel.org> (raw)
In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org>
In order to provide `// SAFETY` comments for every `unsafe impl`, we would
need to repeat them, which is not very useful and would be harder to read.
We could perhaps allow the lint (ideally within a small module), but we
can take the chance to avoid the repetition of the `impl`s themselves
too by using a small local macro, like in other places where we have
had to do this sort of thing.
Thus add the straightforward `impl_{from,as}bytes!` macros and use them
to implement `FromBytes`.
This, in turn, will allow us in the next patch to place a `// SAFETY`
comment that defers to the actual invocation of the macro.
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
rust/kernel/types.rs | 68 +++++++++++++++++++++++---------------------
1 file changed, 35 insertions(+), 33 deletions(-)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 9e7ca066355c..70e173f15d87 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -481,21 +481,22 @@ pub enum Either<L, R> {
/// All bit-patterns must be valid for this type. This type must not have interior mutability.
pub unsafe trait FromBytes {}
-// SAFETY: All bit patterns are acceptable values of the types below.
-unsafe impl FromBytes for u8 {}
-unsafe impl FromBytes for u16 {}
-unsafe impl FromBytes for u32 {}
-unsafe impl FromBytes for u64 {}
-unsafe impl FromBytes for usize {}
-unsafe impl FromBytes for i8 {}
-unsafe impl FromBytes for i16 {}
-unsafe impl FromBytes for i32 {}
-unsafe impl FromBytes for i64 {}
-unsafe impl FromBytes for isize {}
-// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
-// patterns are also acceptable for arrays of that type.
-unsafe impl<T: FromBytes> FromBytes for [T] {}
-unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
+macro_rules! impl_frombytes {
+ ($($({$($generics:tt)*})? $t:ty, )*) => {
+ $(unsafe impl$($($generics)*)? FromBytes for $t {})*
+ };
+}
+
+impl_frombytes! {
+ // SAFETY: All bit patterns are acceptable values of the types below.
+ u8, u16, u32, u64, usize,
+ i8, i16, i32, i64, isize,
+
+ // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
+ // patterns are also acceptable for arrays of that type.
+ {<T: FromBytes>} [T],
+ {<T: FromBytes, const N: usize>} [T; N],
+}
/// Types that can be viewed as an immutable slice of initialized bytes.
///
@@ -514,21 +515,22 @@ unsafe impl<T: FromBytes> FromBytes for [T] {}
/// mutability.
pub unsafe trait AsBytes {}
-// SAFETY: Instances of the following types have no uninitialized portions.
-unsafe impl AsBytes for u8 {}
-unsafe impl AsBytes for u16 {}
-unsafe impl AsBytes for u32 {}
-unsafe impl AsBytes for u64 {}
-unsafe impl AsBytes for usize {}
-unsafe impl AsBytes for i8 {}
-unsafe impl AsBytes for i16 {}
-unsafe impl AsBytes for i32 {}
-unsafe impl AsBytes for i64 {}
-unsafe impl AsBytes for isize {}
-unsafe impl AsBytes for bool {}
-unsafe impl AsBytes for char {}
-unsafe impl AsBytes for str {}
-// SAFETY: If individual values in an array have no uninitialized portions, then the array itself
-// does not have any uninitialized portions either.
-unsafe impl<T: AsBytes> AsBytes for [T] {}
-unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}
+macro_rules! impl_asbytes {
+ ($($({$($generics:tt)*})? $t:ty, )*) => {
+ $(unsafe impl$($($generics)*)? AsBytes for $t {})*
+ };
+}
+
+impl_asbytes! {
+ // SAFETY: Instances of the following types have no uninitialized portions.
+ u8, u16, u32, u64, usize,
+ i8, i16, i32, i64, isize,
+ bool,
+ char,
+ str,
+
+ // SAFETY: If individual values in an array have no uninitialized portions, then the array
+ // itself does not have any uninitialized portions either.
+ {<T: AsBytes>} [T],
+ {<T: AsBytes, const N: usize>} [T; N],
+}
--
2.46.0
next prev parent reply other threads:[~2024-09-04 20:44 UTC|newest]
Thread overview: 72+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-04 20:43 [PATCH 00/19] rust: lint improvements Miguel Ojeda
2024-09-04 20:43 ` [PATCH 01/19] rust: workqueue: remove unneeded ``#[allow(clippy::new_ret_no_self)]` Miguel Ojeda
2024-09-05 7:57 ` Alice Ryhl
2024-09-29 4:17 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 02/19] rust: sort global Rust flags Miguel Ojeda
2024-09-05 7:57 ` Alice Ryhl
2024-09-29 4:18 ` Trevor Gross
2024-09-04 20:43 ` Miguel Ojeda [this message]
2024-09-05 7:58 ` [PATCH 03/19] rust: types: avoid repetition in `{As,From}Bytes` impls Alice Ryhl
2024-09-29 4:20 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 04/19] rust: enable `clippy::undocumented_unsafe_blocks` lint Miguel Ojeda
2024-09-05 8:04 ` Alice Ryhl
2024-09-29 4:33 ` Trevor Gross
2024-10-03 18:45 ` Miguel Ojeda
2024-09-04 20:43 ` [PATCH 05/19] rust: enable `clippy::unnecessary_safety_comment` lint Miguel Ojeda
2024-09-05 8:07 ` Alice Ryhl
2024-09-08 7:38 ` Vincenzo Palazzo
2024-09-29 4:35 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 06/19] rust: enable `clippy::unnecessary_safety_doc` lint Miguel Ojeda
2024-09-05 8:07 ` Alice Ryhl
2024-09-29 4:35 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 07/19] rust: enable `clippy::ignored_unit_patterns` lint Miguel Ojeda
2024-09-05 8:08 ` Alice Ryhl
2024-09-29 4:37 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 08/19] rust: enable `rustdoc::unescaped_backticks` lint Miguel Ojeda
2024-09-05 8:09 ` Alice Ryhl
2024-09-29 4:40 ` Trevor Gross
2024-10-01 17:06 ` Miguel Ojeda
2024-09-04 20:43 ` [PATCH 09/19] rust: init: remove unneeded `#[allow(clippy::disallowed_names)]` Miguel Ojeda
2024-09-05 8:10 ` Alice Ryhl
2024-09-29 4:41 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 10/19] rust: sync: remove unneeded `#[allow(clippy::non_send_fields_in_send_ty)]` Miguel Ojeda
2024-09-05 8:10 ` Alice Ryhl
2024-09-29 4:41 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 11/19] rust: introduce `.clippy.toml` Miguel Ojeda
2024-09-05 8:12 ` Alice Ryhl
2024-09-29 4:48 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 12/19] rust: replace `clippy::dbg_macro` with `disallowed_macros` Miguel Ojeda
2024-09-05 5:20 ` Geert Stappers
2024-09-14 23:10 ` Gary Guo
2024-10-03 18:43 ` Miguel Ojeda
2024-09-04 20:43 ` [PATCH 13/19] rust: rbtree: fix `SAFETY` comments that should be `# Safety` sections Miguel Ojeda
2024-09-05 8:12 ` Alice Ryhl
2024-09-29 4:51 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 14/19] rust: provide proper code documentation titles Miguel Ojeda
2024-09-05 8:13 ` Alice Ryhl
2024-09-29 4:56 ` Trevor Gross
2024-10-03 18:44 ` Miguel Ojeda
2024-09-04 20:43 ` [PATCH 15/19] rust: enable Clippy's `check-private-items` Miguel Ojeda
2024-09-05 8:14 ` Alice Ryhl
2024-09-29 4:57 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 16/19] Documentation: rust: add coding guidelines on lints Miguel Ojeda
2024-09-05 8:15 ` Alice Ryhl
2024-09-05 9:45 ` Miguel Ojeda
2024-09-07 22:22 ` comex
2024-10-01 17:07 ` Miguel Ojeda
2024-10-03 20:00 ` Miguel Ojeda
2024-09-29 5:03 ` Trevor Gross
2024-10-03 18:47 ` Miguel Ojeda
2024-09-04 20:43 ` [PATCH 17/19] rust: start using the `#[expect(...)]` attribute Miguel Ojeda
2024-09-05 8:17 ` Alice Ryhl
2024-09-29 5:05 ` Trevor Gross
2024-09-04 20:43 ` [PATCH 18/19] Documentation: rust: discuss `#[expect(...)]` in the guidelines Miguel Ojeda
2024-09-29 5:10 ` Trevor Gross
2024-10-01 17:11 ` Miguel Ojeda
2024-09-04 20:43 ` [PATCH 19/19] rust: std_vendor: simplify `{ .. macro! .. }` with inner attributes Miguel Ojeda
2024-09-05 8:19 ` Alice Ryhl
2024-09-05 9:27 ` Miguel Ojeda
2024-09-05 9:41 ` Alice Ryhl
2024-09-08 8:50 ` Vincenzo Palazzo
2024-09-14 23:19 ` [PATCH 00/19] rust: lint improvements Gary Guo
2024-10-03 21:51 ` Miguel Ojeda
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=20240904204347.168520-4-ojeda@kernel.org \
--to=ojeda@kernel.org \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=patches@lists.linux.dev \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=wedsonaf@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).