rust: static_assert: add `static_assert!` macro
authorMiguel Ojeda <ojeda@kernel.org>
Thu, 10 Nov 2022 16:41:36 +0000 (17:41 +0100)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 4 Dec 2022 00:59:16 +0000 (01:59 +0100)
commitef9e37973c3a50497c943e70d577dad10a8e41f2
treec37456e5676a9169156ad4a03926655dfc27f064
parentbee1688940b9264a9960e6afdac36a4af35f1f4b
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
Link: https://en.cppreference.com/w/cpp/language/static_assert
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>
rust/kernel/lib.rs
rust/kernel/prelude.rs
rust/kernel/static_assert.rs [new file with mode: 0644]