From a6e1abecfceaf4a5ac26ae8a1f266217745f442e Mon Sep 17 00:00:00 2001 From: Wouter Geraedts Date: Sun, 13 Oct 2019 19:03:33 +0200 Subject: [PATCH] Implemented Path::file_name --- src/path/path.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/path/path.rs b/src/path/path.rs index c1b902b..83af632 100644 --- a/src/path/path.rs +++ b/src/path/path.rs @@ -183,7 +183,7 @@ impl Path { /// * Otherwise, the portion of the file name after the final `.` /// /// [`self.file_name`]: struct.Path.html#method.file_name - /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html + /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// /// # Examples /// @@ -198,6 +198,32 @@ impl Path { self.inner.extension() } + /// Returns the final component of the `Path`, if there is one. + /// + /// If the path is a normal file, this is the file name. If it's the path of a directory, this + /// is the directory name. + /// + /// Returns [`None`] if the path terminates in `..`. + /// + /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None + /// + /// # Examples + /// + /// ``` + /// use async_std::path::Path; + /// use std::ffi::OsStr; + /// + /// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name()); + /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name()); + /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name()); + /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name()); + /// assert_eq!(None, Path::new("foo.txt/..").file_name()); + /// assert_eq!(None, Path::new("/").file_name()); + /// ``` + pub fn file_name(&self) -> Option<&OsStr> { + self.inner.file_name() + } + /// Converts a [`Box`][`Box`] into a [`PathBuf`] without copying or /// allocating. ///