Is there a way to, at compile time, assert that an enum / struct has size <= T ?
XY problem: Trying to build high performance game server. Want to have upper bound of how much traffic I'm using. Need to upper bound the size of enums / structs I'm dealing with.
It is okay if, for an Arc/Rc , it only counts the size of the ptr.
I wrote the const generics first, then to be straightforward, the const generics is removed.
So I don't think there are traps here. The final code depends on how generic / simplified you want it to be.
Update: I forgot to say formatting is not allowed in const eval, like:
const fn check_size<T>() {
let size = std::mem::size_of::<T>();
if size > 16 {
panic!("the size of type is {size}, and shouldn't be greater than 16")
// error[E0015]: cannot call non-const formatting macro in constant functions
--> src/lib.rs:30:38
|
30 | panic!("the size of type is {size}, and shouldn't be greater than 16")
| ^^^^