#assert #static #testing

no-std static_assertions

Compile-time assertions to ensure that invariants are met

8 releases

0.2.5 Dec 12, 2017
0.2.4 Dec 11, 2017
0.2.3 Aug 24, 2017
0.1.1 Aug 13, 2017

#59 in Rust patterns

Download history 1072/week @ 2018-08-22 1172/week @ 2018-08-29 1362/week @ 2018-09-05 1217/week @ 2018-09-12 2491/week @ 2018-09-19 1507/week @ 2018-09-26 1234/week @ 2018-10-03 990/week @ 2018-10-10 1253/week @ 2018-10-17 1424/week @ 2018-10-24 1564/week @ 2018-10-31 1612/week @ 2018-11-07 1682/week @ 2018-11-14

937 downloads per month
Used in 13 crates (8 directly)

MIT/Apache

17KB
98 lines

static_assertions Crates.io Downloads Build Status

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

Documentation

Installation

This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

[dependencies]
static_assertions = "0.2.5"

and this to your crate root:

#[macro_use]
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!(byte; u8, u8);

fn func() {
    // If label-less, must be in a function to work
    assert_eq_size!([u8; 4], u32);

    // Supports unlimited arguments
    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.

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;

fn func() {
    const_assert!(1 + 1 == 2);

    // Supports unlimited comma-separated conditions
    const_assert!(4 > 3, 3 + 2 == FIVE);

    // Fails to compile
    const_assert!(2 != 2);
}

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.

No runtime deps