Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: static_assert: add `static_assert!` macro

Add the `static_assert!` macro, which is a compile-time assert, similar
to the C11 `_Static_assert` and C++11 `static_assert` declarations [1,2].
Do so in a new module, called `static_assert`.

For instance:

static_assert!(42 > 24);
static_assert!(core::mem::size_of::<u8>() == 1);

const X: &[u8] = b"bar";
static_assert!(X[1] == b'a');

const fn f(x: i32) -> i32 {
x + 2
}
static_assert!(f(40) == 42);

Link: https://en.cppreference.com/w/c/language/_Static_assert [1]
Link: https://en.cppreference.com/w/cpp/language/static_assert [2]
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+37
+1
rust/kernel/lib.rs
··· 26 26 pub mod error; 27 27 pub mod prelude; 28 28 pub mod print; 29 + mod static_assert; 29 30 #[doc(hidden)] 30 31 pub mod std_vendor; 31 32 pub mod str;
+2
rust/kernel/prelude.rs
··· 19 19 20 20 pub use super::{dbg, pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn}; 21 21 22 + pub use super::static_assert; 23 + 22 24 pub use super::error::{code::*, Error, Result}; 23 25 24 26 pub use super::{str::CStr, ThisModule};
+34
rust/kernel/static_assert.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + //! Static assert. 4 + 5 + /// Static assert (i.e. compile-time assert). 6 + /// 7 + /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`]. 8 + /// 9 + /// The feature may be added to Rust in the future: see [RFC 2790]. 10 + /// 11 + /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert 12 + /// [`static_assert`]: https://en.cppreference.com/w/cpp/language/static_assert 13 + /// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790 14 + /// 15 + /// # Examples 16 + /// 17 + /// ``` 18 + /// static_assert!(42 > 24); 19 + /// static_assert!(core::mem::size_of::<u8>() == 1); 20 + /// 21 + /// const X: &[u8] = b"bar"; 22 + /// static_assert!(X[1] == b'a'); 23 + /// 24 + /// const fn f(x: i32) -> i32 { 25 + /// x + 2 26 + /// } 27 + /// static_assert!(f(40) == 42); 28 + /// ``` 29 + #[macro_export] 30 + macro_rules! static_assert { 31 + ($condition:expr) => { 32 + const _: () = core::assert!($condition); 33 + }; 34 + }