I’ve been using the newtype pattern extensively across my codebase for various domain IDs to ensure strict type safety. While the safety guarantees are fantastic, the amount of boilerplate required has become quite painful.
For every single ID type, I find myself repeatedly implementing or deriving traits like Deref, From, Into, Display, Serialize, Deserialize, etc., just to make them usable without constant unwrapping (.0) or casting friction. Crates help, but it still feels like a lot of repetitive ceremony for something that conceptually is just a primitive type with a different semantic tag.
I wanted to ask: Is there any discussion, RFC, or future plan in the Rust ecosystem to introduce some form of native "strong type alias" or lightweight syntax sugar (similar to distinct types in other languages) to create distinct types from primitives without writing a full tuple struct and manually bridging traits?
How does the community usually cope with this boilerplate at scale? I'd love to hear if there are standard patterns or if this is an acknowledged ergonomic pain point being looked at.
I think some amount of casting friction is fundamental to them being useful. If they all naturally interconvert all over the place without explicit conversions, you lose essentially all of the value.
This is also where a ton of the complexity comes in delegation. If I have a strong newtype over u32, is that comparable to u32? Can I add a u32 to it? Etc. The answers aren't necessarily obvious.
That said, the answer is usually a good macro to generate stuff, coupled with some generic wrappers to make using them easy.
In rustc, for example, a ton of types are defined using the newtype_index! macro. Those then interop with the IndexVec type, so you don't convert a FieldIdx back to a usize to index a Vec<Foo> -- that would make it just as easy to use the wrong kind -- but you instead use it to index into a IndexVec<FieldIdx, Foo> (or IndexSlice) where indexing takes a FieldIdx.
In principle, newtypes would still have value with free coercion from and to their wrapped type, but not other newtypes — you could coerce FooIdx to u32 and u32 to BarIdx, but not FooIdx to BarIdx. But this would still introduce new kinds of mistakes, like writing code that unwraps the newtpye and proceeds with “untyped” u32s by accident rather than by intent.
I'm a newbie to Rust, but would Deref be appropriate for most newtypes? From the docs:
Types that implement Deref or DerefMut are often called “smart pointers” and the mechanism of deref coercion has been specifically designed to facilitate the pointer-like behavior that name suggests.
One possibility is to use declarative macro. This will save some boilerplate efforts if the majority of id-type interfaces are more or less the same.
Personally I ended up that simple types aliases to integral types are fair enough. You are loosing type safety, but the code becomes much simpler. I found that I rarely mistake types aliases in practice, because their names already giving you informative hint. The code does not bloat. If the new type does not do anything except just being a wrapper around the built-in integral type, it feels to me a little bit of over-engineering both to the end user ergonomics and perhaps for the compiler too.
New types make sense if they do something genuinely new. For example, a handle to resource might be seen as a new type around an integer value, but such type usually has dedicated own methods specifically designed to encapsulate inner implementation details. New types might also be used (and that's was their initial intended purpose) to implement foreign trait on foreign type, but I think it's a different story to IDs.
If the new type is just a duck-type wrapper, I personally see no much value in it. After all, we usually don't design dedicated index type for each indexable entity in the code, we simply use usize.
Note that this is generally not the case. I've done work so that even if you don't run the LLVM optimizer, rustc still elides things like the construct-and-extract patterns for simple newtype wrappers like MyId(123):
The key difference between an alias and a genuine new type is that the compiler recognises the new type as a standalone, new element. With an alias, the compiler may instead consider the underlying type. A new type provides a stronger binding and is generally more declarative. Another factor to bear in mind with Rust is what is known as ‘zero-cost abstraction’. Having more types does not adversely affect runtime performance.
An alias makes sense when types eventually become too long and attempting to write declarative code leads to more confusion. Something like:
I'm working with Rust on a daily basis for many years. All arguments that you have mentioned are valid concerns. I just shared my personal point of view on this topic as someone who tried out various approaches.