Making `OsStr` less opaque on Windows

It's something I'd like to see too.

The problem is that if you work with OsStr, it's because you specifically care about preserving the broken surrogates, so all the functionality has to support them losslessly. Otherwise you can call to_string_lossy to make all the problems disappear.

If you expose impl Iterator<Item=Option<u8>>, it's not materially different from to_string_lossy, you just get None instead of the replacement character.

Iterating over Result<char, u16> (I assume you've meant char here, not u8) is not especially useful, because with such iterator you can't do anything useful other than build a string, but given constraints, the only encoding you can use is WTF-8 or UCS-2, so you're back to square one.


I think char_indices() would be useful. It could iterate over (usize, Option<char>) (or (usize, enum {char, u16})). In such case you wouldn't iterate to keep the chars, but only to compare them and make a note of the indices for later slicing.

And of course it needs to support get(range), split_at(), splitn(), etc.

2 Likes