crosvm/assertions
David Tolnay 3c0aac44d7 assertions: Add compile-time assertion macro
A static assertion is particularly appropriate when unsafe code relies
on two types to have the same size, or on some type to have a particular
size. This is a pattern I observed in USB code, for example:

ff7068402e/devices/src/usb/xhci/xhci_abi_schema.rs (522)

EXAMPLE:

    extern crate assertions;
    use assertions::const_assert;

    fn main() {
        const_assert!(std::mem::size_of::<String>() == 24);
    }

EXAMPLE THAT FAILS TO COMPILE:

    extern crate assertions;
    use assertions::const_assert;

    fn main() {
        // fails to compile:
        const_assert!(std::mem::size_of::<String>() == 8);
    }

FAILURE LOOKS LIKE:

    error[E0271]: type mismatch resolving `<[(); 0] as assertions::Expr>::Value == assertions::True`
     --> src/main.rs:5:5
      |
    5 |     const_assert!(std::mem::size_of::<String>() == 8);
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `assertions::True`, found struct `assertions::False`

TEST=`cargo test` the new crate

Change-Id: I6abe36d5a6a4bd4acb1a02e3aa7c1ece5357f007
Reviewed-on: https://chromium-review.googlesource.com/1366819
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2018-12-09 00:48:17 -08:00
..
src assertions: Add compile-time assertion macro 2018-12-09 00:48:17 -08:00
Cargo.toml assertions: Add compile-time assertion macro 2018-12-09 00:48:17 -08:00