Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: assert_{lt,gt,le,ge}_size #18

Open
alecmocatta opened this issue Sep 18, 2019 · 2 comments
Open

Feature suggestion: assert_{lt,gt,le,ge}_size #18

alecmocatta opened this issue Sep 18, 2019 · 2 comments

Comments

@alecmocatta
Copy link

This would be handy for example to ensure struct sizes don't grow beyond some limit.

It can be implemented like this:

macro_rules! assert_le_size {
    ($a:ty, $b:ty) => {
        const _: MaybeUninit<[u8; size_of::<$b>() - size_of::<$a>()]> = MaybeUninit::uninit();
    }
}
@konsumlamm
Copy link

You can already do that with const_assert (since size_of is a const fn), for example:

use std::mem::size_of;

const_assert!(size_of::<u8>() <= size_of::<u16>());

Your proposed implementation wouldn't provide a better/specialized error message, so I don't see why a separate macro for this would be useful.

@jyn514
Copy link

jyn514 commented Feb 4, 2022

Here's a macro with a better error message for asserting types are equal:

macro_rules! static_assert_size {
    ($ty:ty, $size:expr) => {
        const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
    };
}

Before:

error[E0080]: evaluation of constant value failed
  --> lib.rs:62:1
   |
62 | static_assertions::const_assert_eq!(std::mem::size_of::<Settings>(), 0);
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
   |
   = note: this error originates in the macro `const_assert` (in Nightly builds, run with -Z macro-backtrace for more info)

After:

error[E0308]: mismatched types
  --> lib.rs:57:32
   |
57 |         const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 0 elements, found one with 160 elements
...
61 | static_assert_size!(Settings, 0);
   | -------------------------------- in this macro invocation
   |
   = note: this error originates in the macro `static_assert_size` (in Nightly builds, run with -Z macro-backtrace for more info)

(this macro is from rustc_data_structures: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/macro.static_assert_size.html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants