diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 90b108e677072..00ee2134fadaa 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -283,6 +283,12 @@ 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() + } + /// Gets the underlying byte representation. /// /// Note: it is *crucial* that this API is private, to avoid diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index e66cc49290adf..53e177b1462c8 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)] @@ -244,6 +245,7 @@ #![feature(on_unimplemented)] #![feature(oom)] #![feature(optin_builtin_traits)] +#![feature(os_extras)] #![feature(placement_in_syntax)] #![feature(rand)] #![feature(range_inclusive)] diff --git a/src/libstd/path.rs b/src/libstd/path.rs index d0b9cc4c4602f..fc2423930822d 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1146,6 +1146,7 @@ impl PathBuf { pub fn into_os_string(self) -> OsString { self.inner } + } #[stable(feature = "rust1", since = "1.0.0")] @@ -1859,6 +1860,23 @@ 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 = "os_extras", reason = "recently added", issue = "30259")] + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -3188,6 +3206,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(