diff options
| author | Aaron Rennow <arennow@outlook.com> | 2021-03-27 14:25:52 -0400 |
|---|---|---|
| committer | Aaron Rennow <arennow@outlook.com> | 2021-05-21 21:53:03 -0400 |
| commit | bc45e474a0e871dc2d28abdc75d68d84c5249a8c (patch) | |
| tree | 168a3d1ecc9017236c9ecee2e69713e139aae203 /library/std | |
| parent | 5dc8789e300930751a78996da0fa906be5a344a2 (diff) | |
| download | rust-bc45e474a0e871dc2d28abdc75d68d84c5249a8c.tar.gz rust-bc45e474a0e871dc2d28abdc75d68d84c5249a8c.zip | |
Add std::os::unix::fs::DirEntryExt2::file_name_ref(&self) -> &OsStr
DirEntryExt2 is a new trait with the same purpose as DirEntryExt, but sealed
Diffstat (limited to 'library/std')
| -rw-r--r-- | library/std/src/os/unix/fs.rs | 38 | ||||
| -rw-r--r-- | library/std/src/sys/unix/fs.rs | 4 |
2 files changed, 42 insertions, 0 deletions
diff --git a/library/std/src/os/unix/fs.rs b/library/std/src/os/unix/fs.rs index 9cf51be2836..3939d3c4ca6 100644 --- a/library/std/src/os/unix/fs.rs +++ b/library/std/src/os/unix/fs.rs @@ -9,6 +9,8 @@ use crate::path::Path; use crate::sys; use crate::sys_common::{AsInner, AsInnerMut, FromInner}; // Used for `File::read` on intra-doc links +use crate::ffi::OsStr; +use crate::sealed::Sealed; #[allow(unused_imports)] use io::{Read, Write}; @@ -839,6 +841,42 @@ impl DirEntryExt for fs::DirEntry { } } +/// Sealed Unix-specific extension methods for [`fs::DirEntry`]. +#[unstable(feature = "dir_entry_ext2", issue = "85573")] +pub trait DirEntryExt2: Sealed { + /// Returns a reference to the underlying `OsStr` of this entry's filename. + /// + /// # Examples + /// + /// ``` + /// use std::os::unix::fs::DirEntryExt2; + /// 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(()) + /// } + /// ``` + fn file_name_ref(&self) -> &OsStr; +} + +/// Allows extension traits within `std`. +#[unstable(feature = "sealed", issue = "none")] +impl Sealed for fs::DirEntry {} + +#[unstable(feature = "dir_entry_ext2", issue = "85573")] +impl DirEntryExt2 for fs::DirEntry { + fn file_name_ref(&self) -> &OsStr { + self.as_inner().file_name_os_str() + } +} + /// Creates a new symbolic link on the filesystem. /// /// The `link` path will be a symbolic link pointing to the `original` path. diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index ef14865fbcd..eae156bff41 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -647,6 +647,10 @@ impl DirEntry { fn name_bytes(&self) -> &[u8] { &*self.name } + + pub fn file_name_os_str(&self) -> &OsStr { + OsStr::from_bytes(self.name_bytes()) + } } impl OpenOptions { |
