static_assertions
Rust compile-time assertions to ensure that invariants are met.
Installation
This crate is available on crates.io and can be used by adding the
following to your project's Cargo.toml
:
[]
= "0.2.1"
and this to your crate root:
extern crate static_assertions;
Usage
Assert Equal Size
Use assert_eq_size!
to ensure two types are the same size:
// Requires a label if in module scope
assert_eq_size!;
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!;
// Fails to compile
assert_eq_size_val!;
Note: Both macros support multiple arguments and are not restricted by the recursion limit.
Assert Constant Expression
A constant expression can be ensured to evaluate to true
at compile-time.
The const_assert
and const_assert_eq
macros have the same scope and label
limitations as assert_eq_size
.
// Supports constants
const FIVE: usize = 5;
Limitations
See issue #1 to read up on current limitations of this crate and how to currently overcome them.
License
This project is released under either:
at your choosing.