How to convert OsStr to & [u8] / Vec <u8> on Windows?
I am trying to keep the original source OS file names for the OS, so I need to get the raw bytes OsStr.
It seems possible to invoke as_bytes()on * nix platforms, but this is not defined on MS Windows.
Is there a portable way to convert OsStrto bytes?
The point OsStris that its presentation itself is OS specific. The implementation is somewhat confusing for technical reasons ( @Shepmaster answer provides more details), but you can think of it this way:
- POSIX
OsStr&[u8], POSIX ; - Windows,
OsStr&[u16], Unicode Win32 16- .
API Windows 16- " " 1 , OsStr . OsStr , , , . OsStr Windows. OsStr::encode_wide(), u16, Win32. OsString::from_wide() OsString u16.
It's up to you how your level of perseverance will deal with this difference between platforms. What Rust OsStrprovides are essential tools for implementing round-trip, but the code will certainly differ between platforms. For example, serde decides the difference to efficiently handle how enum OsString { Unix(Vec<u8>), Windows(Vec<u16>) }.
1 Windows character strings are usually described as UTF-16 because that is how they are interpreted at a higher level, but this is not true for all OS strings. The Windows file name may contain pairs of values
u16that are not valid for UTF-16 and may be used. This is why it is not possible to represent Windows strings as bytes, for example. converting them to UTF-8.OsStr Windows Rust 1.16. OsStr . * nix Vec<u8>; Windows Wtf8Buf. Wtf8Buf Vec<u8>, . WTF-8 -, , :
Windows ( UTF-16 API-) Rust WTF-8 , WTF-8.
"" , "", . * nix UTF-8, , NUL. Windows , W A API, W .
, . , C, * nix Windows, -UTF-8, - , API.
Rust , OsStr OsString.
OsStr , UTF-8, String &str, . , LPCWSTR, Vec<u16>, Windows API. , Rust .