Nikolai Vazquez e48415c19c Use fn instead of mod for global assert_eq_size
A function can be used in more places than a module, providing much more
flexibility.
2017-08-13 02:34:18 -04:00
2017-08-12 14:02:37 -04:00
2017-08-12 23:54:37 -04:00

static_assertions Crates.io Build Status

Rust compile-time assertions to ensure that invariants are met.

Documentation

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.

S
Description
提供用于编译时静态断言的宏。 | A Rust library that provides support for compile-time assertions.
Readme 866 KiB
Languages
Rust 100%