Get filename from DirEntry without allocating

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.

1 Like

You can use libc::readdir.

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

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.

I thought that’s what OsStr was.

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)

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.

1 Like

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.

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.