From 6b7803647f1b775d87d069b2c2a240421f91842a Mon Sep 17 00:00:00 2001 From: Ticki <Ticki@users.noreply.github.com> Date: Tue, 22 Dec 2015 08:56:01 +0100 Subject: [PATCH 1/4] Add is_empty() for PathBuf, fix #30259 --- src/libstd/path.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index d0b9cc4c4602f..0036fa581ae2d 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1146,6 +1146,16 @@ impl PathBuf { pub fn into_os_string(self) -> OsString { self.inner } + + /// 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. + pub fn is_empty(&self) -> bool { + if let Some(b) = self.inner.to_bytes() { + b.is_empty() + } else { + true + } + } } #[stable(feature = "rust1", since = "1.0.0")] From 3c07d66a5b2b90fa5273fdd6f3fe5d839e3c0fcd Mon Sep 17 00:00:00 2001 From: Ticki <Ticki@users.noreply.github.com> Date: Tue, 22 Dec 2015 09:10:55 +0100 Subject: [PATCH 2/4] Move the is_empty method to Path instead Tests --- src/libstd/lib.rs | 1 + src/libstd/path.rs | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index e66cc49290adf..3c4398bb75ac8 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -226,6 +226,7 @@ #![feature(const_fn)] #![feature(core_float)] #![feature(core_intrinsics)] +#![feature(convert)] #![feature(decode_utf16)] #![feature(drop_in_place)] #![feature(dropck_parametricity)] diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 0036fa581ae2d..db726723f53a7 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1147,15 +1147,6 @@ impl PathBuf { self.inner } - /// 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. - pub fn is_empty(&self) -> bool { - if let Some(b) = self.inner.to_bytes() { - b.is_empty() - } else { - true - } - } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1869,6 +1860,27 @@ 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 + /// + /// ``` + /// use std::path::Path; + /// + /// let path = Path::new("/tmp/foo.rs"); + /// + /// assert!(!path.is_empty()); + /// ``` + #[unstable(feature = "path_extras", reason = "recently added", issue = "30259")] + pub fn is_empty(&self) -> bool { + if let Some(b) = self.inner.to_bytes() { + b.is_empty() + } else { + true + } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -3198,6 +3210,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( From 79e0a3a73b4ce8830bf7ce394250c49c16d36d79 Mon Sep 17 00:00:00 2001 From: Ticki <Ticki@users.noreply.github.com> Date: Tue, 22 Dec 2015 15:55:20 +0100 Subject: [PATCH 3/4] Add is_empty to OsStr, rename path_extras to os_extras, don\'t go through the unicode validity checking on Windows Add issue to attr --- src/libstd/ffi/os_str.rs | 5 +++++ src/libstd/path.rs | 8 ++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 90b108e677072..1f9b9a700cad2 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -283,6 +283,11 @@ impl OsStr { self.to_bytes().and_then(|b| CString::new(b).ok()) } + #[unstable(feature = "os_extras", 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 db726723f53a7..fc2423930822d 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1873,13 +1873,9 @@ impl Path { /// /// assert!(!path.is_empty()); /// ``` - #[unstable(feature = "path_extras", reason = "recently added", issue = "30259")] + #[unstable(feature = "os_extras", reason = "recently added", issue = "30259")] pub fn is_empty(&self) -> bool { - if let Some(b) = self.inner.to_bytes() { - b.is_empty() - } else { - true - } + self.inner.is_empty() } } From 5e992a3488c0290675fd1adb203ef2811ed1b59f Mon Sep 17 00:00:00 2001 From: Ticki <Ticki@users.noreply.github.com> Date: Tue, 22 Dec 2015 22:06:16 +0100 Subject: [PATCH 4/4] Documentation Added features --- src/libstd/ffi/os_str.rs | 1 + src/libstd/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 1f9b9a700cad2..00ee2134fadaa 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -283,6 +283,7 @@ impl OsStr { self.to_bytes().and_then(|b| CString::new(b).ok()) } + /// Checks if the string is empty. #[unstable(feature = "os_extras", reason = "recently added", issue = "30259")] pub fn is_empty(&self) -> bool { self.inner.inner.is_empty() diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 3c4398bb75ac8..53e177b1462c8 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -245,6 +245,7 @@ #![feature(on_unimplemented)] #![feature(oom)] #![feature(optin_builtin_traits)] +#![feature(os_extras)] #![feature(placement_in_syntax)] #![feature(rand)] #![feature(range_inclusive)]