(The answer is always "plan, measure, decide".)
The question is: I am using a bitmap library that is hardcoded to u32. I have many different types of u32 Ids that all need to be stored in them. I also have other things that are u32 that aren't Ids.
I'll have one Bitmap for Ids for TypeOne and another Bitmap for Ids for TypeTwo. No Bitmap will ever contain Ids for different types.
I could:
use u32 and hope I don't get terribly confused as to which is which....that never works well
use a plethora of TypeOneId(u32), TypeTwoId(u32) and make a Bitmap wrapper which is generic over the "Into + From".
I vastly prefer the second (readability > safety > ignorantly perceived performance costs), but this will be used frequently in a hot loop (imagine database indexes for example). I know the Rust implementation of newtypes have 0 overhead at runtime but what about the cost for Into and From? Is the compiler smart enough to optimise those away as well?
I realise this is like an ignorant child asking Picasso about whether they should use red or green without any more context, but .. help
For functions this simple, I would expect them to be inlined and optimized away. If you’re really worried about it, you can mark your implementations #[inline(always)].
That's what I'd expect - if the Into and From implementations are MyNewType(the_u32) and my_new_type.0 respectively then I'd hope it would be the like the spoon in the matrix...
That's always been reliably optimized away in release mode, but now it's so zero-overhead that LLVM doesn't even have to optimize away constructing them