The solution below does not give access to the new items: you can
modify xpairs (exercise for the reader) to have that too if __index
is still available.
This is a very nice iterator. It's far more memory efficient than a shallow copy, and unlike my sorted traversal, it runs in linear time. Thank you for sharing it.
Sadly, in the cases where I actually need an insertion-safe iteration, my tables have nontrivial __newindex metamethods. And while there are various tricks I could use to make a modified version of xpairs work regardless -- the results would probably be a bit overly complex and error prone. All the same, there may well come a time when I need a linear time, insertion safe iterator -- and if and when that happens, I'll certainly come back to your approach.
-Sven