From 82163fe1a900458427daa54c24eb1712379dbb88 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 24 Feb 2016 22:03:55 -0500 Subject: [PATCH] Add `std::path::Path::is_empty`. Original issue requesting this feature: https://github.com/rust-lang/rust/issues/30259 Originally implemented in https://github.com/rust-lang/rust/pull/30623 but that pull request when stale. It was rebased in https://github.com/rust-lang/rust/pull/31231, but the `Path` changes got lost as the focus shifted towards `OsString` and `OsStr`. An RFC (https://github.com/rust-lang/rfcs/issues/1497) was briefly opened, since I didn't know if this functionality needed an RFC, but @alexcrichton clarified in the RFC issue I linked that this is not the case. --- src/libstd/path.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 35118bde96bb9..df1d25f73a27e 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1921,6 +1921,24 @@ impl Path { pub fn is_dir(&self) -> bool { fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) } + + /// Returns true if the path is empty. + /// + /// # Examples + /// + /// ``` + /// use std::path::Path; + /// + /// let mut path = Path::new(""); + /// assert!(path.is_empty()); + /// + /// path.push("/tmp/foo.rs"); + /// assert!(!path.is_empty()); + /// ``` + #[unstable(feature = "path_is_empty", reason = "recently added", issue = "30259")] + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -3332,6 +3350,17 @@ mod tests { } } + #[test] + pub fn is_empty() { + let path = Path::new("/tmp/foo.rs"); + assert!(!path.is_empty()); + + let mut path_buf = PathBuf::new(); + assert!(path_buf.is_empty()); + path_buf.push("foo"); + assert!(!path_buf.is_empty()); + } + #[test] pub fn test_set_extension() { macro_rules! tfe(