How to modify windows OsString in-place?

I want to modify windows OsString in-place, without any memory allocation.

I know there are two functions make_ascii_lowercase() and make_ascii_uppercase() could do this, but I need some more complex, user-defined modifications. (I can promise this modification is safe and will not change its length)

The inner: Buf in OsString is private, I can't get the mut ref of it; and encode_wide() returns EncodeWide<'a> without allocation, but it's just an Iterator and I cannot modify it.

Your best bet is the "encoded bytes" methods but note the caveats.

Specifically, OsString::into_encoded_bytes and from_encoded_bytes will allow you to mess with the internals and convert back without any (additional) allocation or validation.

Thank you! I didn't notice into_encoded_bytes is zero cost :smiling_face_with_tear: