From 1a5bbefec0c3bd550577389df8223032ec134be8 Mon Sep 17 00:00:00 2001 From: Ticki Date: Tue, 22 Dec 2015 08:56:01 +0100 Subject: [PATCH] Add is_empty() for Path and OsStr Related to https://github.com/rust-lang/rust/pull/30259 Rebase of https://github.com/rust-lang/rust/pull/30623 --- src/libstd/ffi/os_str.rs | 6 ++++++ src/libstd/path.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index eb5ddecbd054d..48b716e32bbfa 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -277,6 +277,12 @@ impl OsStr { self.to_bytes().and_then(|b| CString::new(b).ok()) } + /// Checks if the string is empty. + #[unstable(feature = "osstr_is_empty", reason = "recently added", issue = "30259")] + pub fn is_empty(&self) -> bool { + self.inner.inner.is_empty() + } + /// Gets the underlying byte representation. /// /// Note: it is *crucial* that this API is private, to avoid diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 207c4d02e4817..e196a580bc231 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1897,6 +1897,24 @@ impl Path { pub fn is_dir(&self) -> bool { fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) } + + /// Checks if the path buffer is empty. On Windows, it will return false if the inner string is + /// invalid unicode. On Unix, this is a no-op. + /// + /// # Examples + /// + /// ``` + /// # #![feature(os_extras)] + /// use std::path::Path; + /// + /// let path = Path::new("/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")] @@ -3239,6 +3257,17 @@ mod tests { } } + #[test] + pub fn is_empty() { + let path = Path::new("/tmp/foo.rs"); + let mut path_buf = PathBuf::new(); + + assert!(!path.is_empty()); + assert!(path_buf.is_empty()); + path_buf.push("catsarecute"); + assert!(!path_buf.is_empty()); + } + #[test] pub fn test_set_extension() { macro_rules! tfe(