mirror of
https://github.com/openharmony/third_party_rust_static-assertions-rs.git
synced 2026-07-21 04:05:27 -04:00
e48415c19c3e9e196b68377d4d18d833658cdbf8
A function can be used in more places than a module, providing much more flexibility.
static_assertions

Rust compile-time assertions to ensure that invariants are met.
Usage
Assert Equal Size
Use assert_eq_size! to ensure two types are the same size:
assert_eq_size!([u8; 4], u32);
assert_eq_size!([u8; 8], u64, (u32, u32), (u32, u16, u16), ...);
// Fails to compile
assert_eq_size!(u16, u64);
Use assert_eq_size_val! to ensure two values are the same size:
let x: u32 = 42;
let y: u32 = 10;
assert_eq_size_val!(x, y, [0u8; 4]);
// Fails to compile
assert_eq_size_val!(x, 0u8);
Note: Both macros support multiple arguments and are not restricted by the recursion limit.
Limitation: Due to implementation details, these macros can only be called
from within the context of a function. This may change when mem::size_of
becomes a const fn.
Assert Constant Expression
A constant expression can be ensured to evaluate to true at compile-time.
const_assert!(1 + 1 == 2);
// Supports constants
const FIVE: usize = 5;
// Supports comma-separated conditions
const_assert!(4 > 3, 3 + 2 == FIVE);
// Fails to compile
const_assert!(2 != 2);
Limitation: Due to implementation details, const_assert! can only be
called from within the context of a function.
License
This project is released under either:
at your choosing.
Description
Languages
Rust
100%