xref: /openbmc/linux/rust/kernel/static_assert.rs (revision 5e0266f0)
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 }
35