Compile time assert

I’ve read this: https://github.com/rust-lang/rfcs/pull/1229

and wonder am I understand it correctly, if at now I write:

assert_eq!(4, mem::size_of::<usize>());

then after implementing 1229 I got at least warning during compile time.

So at now I can using assert macros and check constraints at runtime, and after somebody implement 1229 my assert_eq! magically become compile time asserts?

You can abuse transmute to do this at compile time if you want:

fn _size_check() {
    unsafe { mem::transmute::<usize, [u8; 4]>(0); }
}
2 Likes

Yes, I know about this trick, there is also static_assertions crate that provides nice wrapper around this or similar trick.

But it would be great to have it inside std or compiler.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.