Skip to content

Commit 1a5bbef

Browse files
tickifrewsxcv
authored andcommitted
Add is_empty() for Path and OsStr
Related to #30259 Rebase of #30623
1 parent b2f4c5c commit 1a5bbef

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/libstd/ffi/os_str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ impl OsStr {
277277
self.to_bytes().and_then(|b| CString::new(b).ok())
278278
}
279279

280+
/// Checks if the string is empty.
281+
#[unstable(feature = "osstr_is_empty", reason = "recently added", issue = "30259")]
282+
pub fn is_empty(&self) -> bool {
283+
self.inner.inner.is_empty()
284+
}
285+
280286
/// Gets the underlying byte representation.
281287
///
282288
/// Note: it is *crucial* that this API is private, to avoid

src/libstd/path.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,24 @@ impl Path {
18971897
pub fn is_dir(&self) -> bool {
18981898
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
18991899
}
1900+
1901+
/// Checks if the path buffer is empty. On Windows, it will return false if the inner string is
1902+
/// invalid unicode. On Unix, this is a no-op.
1903+
///
1904+
/// # Examples
1905+
///
1906+
/// ```
1907+
/// # #![feature(os_extras)]
1908+
/// use std::path::Path;
1909+
///
1910+
/// let path = Path::new("/tmp/foo.rs");
1911+
///
1912+
/// assert!(!path.is_empty());
1913+
/// ```
1914+
#[unstable(feature = "path_is_empty", reason = "recently added", issue = "30259")]
1915+
pub fn is_empty(&self) -> bool {
1916+
self.inner.is_empty()
1917+
}
19001918
}
19011919

19021920
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3239,6 +3257,17 @@ mod tests {
32393257
}
32403258
}
32413259

3260+
#[test]
3261+
pub fn is_empty() {
3262+
let path = Path::new("/tmp/foo.rs");
3263+
let mut path_buf = PathBuf::new();
3264+
3265+
assert!(!path.is_empty());
3266+
assert!(path_buf.is_empty());
3267+
path_buf.push("catsarecute");
3268+
assert!(!path_buf.is_empty());
3269+
}
3270+
32423271
#[test]
32433272
pub fn test_set_extension() {
32443273
macro_rules! tfe(

0 commit comments

Comments
 (0)