Idea: Unsafe Iterator optimization traits

For reference, rust has TrustedLen.
Mojo would arguably have an easier time than rust implementing that, since mojo has conform_to and reflect. This is one reason why rust iterators are described as zero cost; namely because of these internal optimizations(even though it falls apart at opt-level s or z and custom non-std iterators don’t benefit from those optimizations.).
So I think, mojo should also take this path, since the goal is maximum performance for higher level abstractions. If rust can do this, mojo should as well.

Hi @leb-kuchen - yep this is definitely something we have had the idea of for a long time.

However, before we do any sort of these “optimized” unsafe iterator traits, we need first officially settle how to really model the Iterator trait and how to make the __next__ function return either ref T or a T.
Once we figure this out, then we can do all the fancy optimizations and unsafe traits implementable for the Iterator trait family.