static-assertions icon indicating copy to clipboard operation
static-assertions copied to clipboard

Add assert_sizeof_eq!

Open tesuji opened this issue 4 years ago • 2 comments

to equate size of a type to a literal usize at compile time.

I try to improve the error message with std::concat! but it doesn't help much: Playground link for this newly added macro: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3f807d04413a9706b8afc8472895f664

tesuji avatar Feb 18 '21 06:02 tesuji

I cannot reproduce locally the error on CI with nightly toolchain:

error[E0019]: constant contains unimplemented expression type
Error:  --> tests/eq_size.rs:8:1
  |
8 | assert_sizeof_eq!(u32, 4);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

tesuji avatar Feb 18 '21 06:02 tesuji

Is there any reason for adding a new macro for this, instead of just using for example

use std::mem::size_of;

const_assert_eq!(size_of::<u32>(), 4);

?

konsumlamm avatar Aug 19 '21 11:08 konsumlamm