linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: add `assert_sync` function
@ 2025-06-07 13:02 Christian Schrefl
  2025-06-07 15:42 ` Benno Lossin
  2025-06-07 17:29 ` Miguel Ojeda
  0 siblings, 2 replies; 11+ messages in thread
From: Christian Schrefl @ 2025-06-07 13:02 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: linux-kernel, rust-for-linux, Christian Schrefl

Adds a new file `compile_assert.rs` for asserts during compile time and
add the `assert_sync` function to this file.

This will be used in `miscdevice` to avoid regression in case a `: Send`
bound falsely gets dropped in the future.

Suggested-by: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/rust-for-linux/20250530-b4-rust_miscdevice_registrationdata-v4-0-d313aafd7e59@gmail.com/T/#mdf3328834ce1d136daf836c9e089b5a8627a6d53
Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com>
---
For now I've only added the function.

Some things that might make sense to do as well:
- Move `static_assert` into `compile_assert.rs`.
- Add `assert_sync` to prelude.
- Add `assert_send` as well.
- Use these asserts in various places around the kernel. (I'm not sure
where it would make sense)
---
 rust/kernel/compile_assert.rs | 24 ++++++++++++++++++++++++
 rust/kernel/lib.rs            |  1 +
 2 files changed, 25 insertions(+)

diff --git a/rust/kernel/compile_assert.rs b/rust/kernel/compile_assert.rs
new file mode 100644
index 0000000000000000000000000000000000000000..2a99de1ba919dc3952d7a1585869567a44106b44
--- /dev/null
+++ b/rust/kernel/compile_assert.rs
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Compile-time asserts.
+
+/// Asserts that the given type is [`Sync`]. This check is done at compile time and does nothing
+/// at runtime.
+///
+/// Note that this is only intended to avoid regressions and for sanity checks.
+///
+/// # Examples
+/// ```
+/// # use kernel::compile_assert::assert_sync;
+/// # use kernel::types::NotThreadSafe;
+///
+///
+/// // Do the assertion in a const block to make sure it won't be executed at runtime.
+/// const _:() = {
+///     assert_sync::<i32>(); // Succeeds because `i32` is Sync
+///     // assert_sync::<NotThreadSafe>(); // Fails because `NotThreadSafe` is not `Sync`.
+/// };
+///
+/// ```
+#[inline(always)]
+pub const fn assert_sync<T: ?Sized + Sync>() {}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 7e227b52b4d8ff23d39d5cc9ad3ab57132c448d0..e1630e5079b2436eda6f8b71225bd5371af337b4 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -56,6 +56,7 @@
 pub mod block;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod compile_assert;
 pub mod cred;
 pub mod device;
 pub mod device_id;

---
base-commit: 7a17bbc1d952057898cb0739e60665908fbb8c72
change-id: 20250607-assert_sync-62fddc5a0adc

Best regards,
-- 
Christian Schrefl <chrisi.schrefl@gmail.com>


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-06-08  7:41 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-07 13:02 [PATCH] rust: add `assert_sync` function Christian Schrefl
2025-06-07 15:42 ` Benno Lossin
2025-06-07 15:54   ` Christian Schrefl
2025-06-07 17:30     ` Miguel Ojeda
2025-06-07 18:11     ` Benno Lossin
2025-06-07 19:20       ` Christian Schrefl
2025-06-07 22:31         ` Benno Lossin
2025-06-07 23:38           ` Christian Schrefl
2025-06-08  7:41             ` Benno Lossin
2025-06-07 17:29 ` Miguel Ojeda
2025-06-07 20:19   ` Christian Schrefl

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).