Skip to content

Commit 82163fe

Browse files
committed
Add std::path::Path::is_empty.
Original issue requesting this feature: #30259 Originally implemented in #30623 but that pull request when stale. It was rebased in #31231, but the `Path` changes got lost as the focus shifted towards `OsString` and `OsStr`. An RFC (rust-lang/rfcs#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.
1 parent 0ef8d42 commit 82163fe

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libstd/path.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,24 @@ impl Path {
19211921
pub fn is_dir(&self) -> bool {
19221922
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
19231923
}
1924+
1925+
/// Returns true if the path is empty.
1926+
///
1927+
/// # Examples
1928+
///
1929+
/// ```
1930+
/// use std::path::Path;
1931+
///
1932+
/// let mut path = Path::new("");
1933+
/// assert!(path.is_empty());
1934+
///
1935+
/// path.push("/tmp/foo.rs");
1936+
/// assert!(!path.is_empty());
1937+
/// ```
1938+
#[unstable(feature = "path_is_empty", reason = "recently added", issue = "30259")]
1939+
pub fn is_empty(&self) -> bool {
1940+
self.inner.is_empty()
1941+
}
19241942
}
19251943

19261944
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3332,6 +3350,17 @@ mod tests {
33323350
}
33333351
}
33343352

3353+
#[test]
3354+
pub fn is_empty() {
3355+
let path = Path::new("/tmp/foo.rs");
3356+
assert!(!path.is_empty());
3357+
3358+
let mut path_buf = PathBuf::new();
3359+
assert!(path_buf.is_empty());
3360+
path_buf.push("foo");
3361+
assert!(!path_buf.is_empty());
3362+
}
3363+
33353364
#[test]
33363365
pub fn test_set_extension() {
33373366
macro_rules! tfe(

0 commit comments

Comments
 (0)