Get filename from DirEntry without allocating


#1

The raw C API on both Windows and Linux allows the user to get the filename without allocating, but the Rust API only offers an OsString. There should be a way to get the filename without the extra allocation.


#2

You can use libc::readdir.


#3

That’s only available on *nix though. It’d be nice if the cross-platform API supported this.


#4

It’s hard to make it cross platform, since on Windows you’d need something like FindFirstFileW which is UCS2/UTF-16. So a cross-platform non-allocating API would have to return an enum of both string types, or create a platform-dependent string type.


#5

I thought that’s what OsStr was.


#6

AFAIK OsStr is just non-growable &OsString equivalent. It’s still WTF-8 underneath. This makes possible to have non-allocating to_str() and OsStr::from(str)


#7

Oh, I thought they were both platform-dependent, not WTF-8 (you know, because OS is in the name.) I guess that explains the need for the allocation.


#8

The idea is that Rust strings can be converted to OsStrings without allocation, but I do think (pseudo) UTF-16 is worth natively supporting. It’s not only a Windows thing - it’s also the only way to do free/cheap String exchange with C# and Java native libraries, and when WebAssembly gets better JavaScript interop the same will be true there.