After having to spend time using java and getting annoyed by either having to use unreadable constructors with many fields or using builders without type safety I was wondering if I could implement a type safe builder in Rust.
After a short while I came up with this, which I consider to be a relatively easy pattern.
The most prominent downside I see with this approach is, that it produces a quadratic amount of code in respect to the number of fields the struct has.
I think the pattern should be easy enough for llvm to optimize out (especially if I add
#[inline(always)]to all functions) but I am worried that llvm might just decide that compilation takes to long and stop optimizing - or just taking a lot of time compiling (which is hurtful to anyones workflow).
So my question is: Is there a trick I could use to still have the type safety and rather simple "type interface" (
FooBuilder<Unset, Set<_>>) that would asymptotically reduce the amount of code generated?
Edit: My gut feeling is that this is not possible. Reason being that for type safety I will need a "type flag" for each field, resulting in at least
n monomorphized builder structs passed to llvm for optimization.