ostd/util/macros.rs
1// SPDX-License-Identifier: MPL-2.0
2
3/// Asserts that a boolean expression is `true` at compile-time.
4///
5/// Rust provides [`const` blocks], which can be used flexibly within methods, but cannot be used
6/// directly at the top level. This macro serves as a helper to perform compile-time assertions
7/// outside of methods.
8///
9/// [`const` blocks]: https://doc.rust-lang.org/reference/expressions/block-expr.html#const-blocks
10//
11// TODO: Introduce `const_assert_eq!()` once `assert_eq!()` can be used in the `const` context.
12#[macro_export]
13macro_rules! const_assert {
14 ($cond:expr $(,)?) => { const _: () = assert!($cond); };
15 ($cond:expr, $($arg:tt)+) => { const _: () = assert!($cond, $($arg)*); };
16}
17
18/// Creates a pointer whose type matches the expression, but whose value is always NULL.
19///
20/// This is a helper macro, typically used in another macro to help with type inference.
21///
22/// The expression is guaranteed never to be executed, so it can contain arbitrarily unsafe code
23/// without causing any soundness problems.
24#[macro_export]
25macro_rules! ptr_null_of {
26 ($expr:expr $(,)?) => {
27 if true {
28 core::ptr::null()
29 } else {
30 unreachable!();
31
32 // SAFETY: This is dead code and will never be executed.
33 //
34 // One may wonder: is it possible for the dead code to
35 // trigger UBs by simply being compiled, rather than being executed?
36 // More specifically, what if the caller attempts to
37 // trick the macro into defining unsafe language items,
38 // like static variables, functions, implementation blocks, or attributes,
39 // those that are not executed.
40 // Luckily for us, in such cases, the Rust compiler would complain that
41 // "items do not inherit unsafety from separate enclosing items".
42 #[expect(unreachable_code)]
43 unsafe {
44 $expr
45 }
46 }
47 };
48}