Dummy ComponentStorage casts are technically UB
We commonly cast a type-erased ComponentStorage pointer to a Storage(u1) when we don't know its actual type, so that we perform component-agnostic operations on it.
This is technically undefined behavior, as there is no guarantee that the dummy type and the actual type have the same layout.
There are a few ways to fix this:
- Marking the ComponentStorage struct as
externorpackedis impossible because it containsautolayout structs.- We could make our own ArrayList and Allocator types so they could be extern, but that sucks for obvious reasons...
- We could create a non-generic InnerComponentStorage type that holds all the fields, then have ComponentStorage simply be a type-safe wrapper around it.
- Alternatively, we could make everything more generic, so that all component types are known at comptime. I personally prefer this one, as it would also fix #68.
- Finally, we could just do nothing, as it doesn't seem to have caused any issues thus far...
I have never been a big fan of the old u1 hack. That being said, with careful use (and the few “struct closures” to capture the real type) it does seem to do the trick without issues. If you want to take a crack at a proper fix I’m all for it.
Would you mind if Registry were to be refactored to require that component types be known at comptime? IMO that is the cleanest solution.
I can't think of any reason why component types wouldn't be known at comptime so that should be totally fine.