In my codebase I usually assume that size_of::() >= 4 bytes (I sometimes add a const assert too). But sometimes, in tens of thousands of operations and assignments I may put by mistake a value larger than u32::MAX in an usize. This usually doesn't cause a problem because I am usually using a 64 bit system. But how can I spot such mistakes and only put values that fit in a 4 bytes inside my usize variables, so the code is portable to 32 bit systems too? A solution is to use u32 and cast only at the last moment to usize, but this introduces a ton of casts in my code that I want to avoid.
A possible solution could be to add two compilation features:
If you use one of them the compiler keeps using the same usize as before (this means size_of::() keeps being 8 for me) but it introduces overflow tests in debug builds to assure you don't put values larger than u32::MAX (or u16::MAX) inside your user-code usize values. I don't know if this a good solution, or if there's already another (better) solution.
(I've also seen this thread:
How to build both 32- and 64-bit app on Windows ).