rust/library/std/src/os
Yuki Okushi 1fcd9abbb1
Rollup merge of #83581 - arennow:dir_entry_ext_unix_borrow_name, r=m-ou-se
Add std::os::unix::fs::DirEntryExt2::file_name_ref(&self) -> &OsStr

Greetings!

This is my first PR here, so please forgive me if I've missed an important step or otherwise done something wrong. I'm very open to suggestions/fixes/corrections.

This PR adds a function that allows `std::fs::DirEntry` to vend a borrow of its filename on Unix platforms, which is especially useful for sorting. (Windows has (as I understand it) encoding differences that require an allocation.) This new function sits alongside the cross-platform [`file_name(&self) -> OsString`](https://doc.rust-lang.org/std/fs/struct.DirEntry.html#method.file_name) function.

I pitched this idea in an [internals thread](https://internals.rust-lang.org/t/allow-std-direntry-to-vend-borrows-of-its-filename/14328/4), and no one objected vehemently, so here we are.

I understand features in general, I believe, but I'm not at all confident that my whole-cloth invention of a new feature string (as required by the compiler) was correct (or that the name is appropriate). Further, there doesn't appear to be a test for the sibling `ino` function, so I didn't add one for this similarly trivial function either. If it's desirable that I should do so, I'd be happy to [figure out how to] do that.

The following is a trivial sample of a use-case for this function, in which directory entries are sorted without any additional allocations:

```rust
use std::os::unix::fs::DirEntryExt;
use std::{fs, io};

fn main() -> io::Result<()> {
    let mut entries = fs::read_dir(".")?.collect::<Result<Vec<_>, io::Error>>()?;
    entries.sort_unstable_by(|a, b| a.file_name_ref().cmp(b.file_name_ref()));

    for p in entries {
        println!("{:?}", p);
    }

    Ok(())
}
```
2021-07-06 02:33:06 +09:00
..
android Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
dragonfly Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
emscripten Remove an invalid #[doc(inline)] 2021-05-11 00:03:44 +02:00
fortanix_sgx Rollup merge of #85054 - jethrogb:jb/sgx-inline-asm, r=Amanieu 2021-06-22 07:37:42 +09:00
freebsd Auto merge of #74576 - myfreeweb:freebsd-sanitizers, r=oli-obk 2020-08-15 11:38:24 +00:00
fuchsia Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
haiku Deprecate std::os::haiku::raw 2021-03-16 17:43:33 -04:00
hermit Move OsStringExt and OsStrExt to std::os 2021-06-20 11:55:01 +02:00
illumos Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
ios Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
linux Cleanup std::os 2021-05-03 16:56:20 +02:00
macos Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
netbsd Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
openbsd Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
raw Use #[doc = include_str!()] in std 2021-02-23 15:54:55 +01:00
redox Cleanup std::os 2021-05-03 16:56:20 +02:00
solaris Switch to intra-doc links in library/std/src/os/*/fs.rs 2020-08-11 15:20:01 +05:30
unix Rollup merge of #83581 - arennow:dir_entry_ext_unix_borrow_name, r=m-ou-se 2021-07-06 02:33:06 +09:00
vxworks Take sys/vxworks/{fd,fs,io} from sys/unix instead. 2020-10-16 06:19:00 +02:00
wasi Auto merge of #85746 - m-ou-se:io-error-other, r=joshtriplett 2021-07-02 09:01:42 +00:00
windows Move std::sys::windows::ext to std::os::windows 2021-05-03 16:56:20 +02:00
mod.rs Allow documenting on hermit 2021-05-03 16:56:22 +02:00