# Get filename from DirEntry without allocating

**URL:** <https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244>\
**Category:** Uncategorized\
**Created:** [November 14, 2017, 11:06pm UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244 "2017-11-14T23:06:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [November 14, 2017, 11:06pm UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/1 "2017-11-14T23:06:37Z")

</div>

The raw C API on both [Windows](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365740(v=vs.85).aspx) and [Linux](https://linux.die.net/man/3/readdir) allows the user to get the filename without allocating, but the [Rust API](https://doc.rust-lang.org/std/fs/struct.DirEntry.html#method.file_name) only offers an `OsString`. There should be a way to get the filename without the extra allocation.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 14, 2017, 11:21pm UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/2 "2017-11-14T23:21:19Z")

</div>

You can use [libc::readdir](https://doc.rust-lang.org/libc/x86_64-unknown-linux-gnu/libc/fn.readdir.html).

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [November 14, 2017, 11:33pm UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/3 "2017-11-14T23:33:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 14, 2017, 11:54pm UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/4 "2017-11-14T23:54:23Z")

</div>

It’s hard to make it cross platform, since on Windows you’d need something like [`FindFirstFileW`](https://retep998.github.io/doc/kernel32/fn.FindFirstFileW.html) 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.

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [November 15, 2017, 12:11am UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/5 "2017-11-15T00:11:49Z")

</div>

I thought that’s what OsStr was.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 15, 2017, 12:26am UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/6 "2017-11-15T00:26:05Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [November 15, 2017, 12:43am UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/7 "2017-11-15T00:43:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![parkovski](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/parkovski/32/2730_2.png) [@parkovski](https://internals.rust-lang.org/u/parkovski)\
**Post date:** [November 15, 2017, 6:26am UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/8 "2017-11-15T06:26:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/get-filename-from-direntry-without-allocating/6244/9 "2019-03-25T08:29:15Z")

</div>

Closed on March 25, 2019, 8:29am UTC

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