Skip to content

Commit

Permalink
Add std::path::Path::is_empty.
Browse files Browse the repository at this point in the history
Original issue requesting this feature:
rust-lang#30259

Originally implemented in rust-lang#30623
but that pull request when stale.

It was rebased in rust-lang#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.
  • Loading branch information
frewsxcv committed Feb 25, 2016
1 parent 0ef8d42 commit 82163fe
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,24 @@ impl Path {
pub fn is_dir(&self) -> bool {
fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
}

/// Returns true if the path is empty.
///
/// # Examples
///
/// ```
/// use std::path::Path;
///
/// let mut path = Path::new("");
/// assert!(path.is_empty());
///
/// path.push("/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")]
Expand Down Expand Up @@ -3332,6 +3350,17 @@ mod tests {
}
}

#[test]
pub fn is_empty() {
let path = Path::new("/tmp/foo.rs");
assert!(!path.is_empty());

let mut path_buf = PathBuf::new();
assert!(path_buf.is_empty());
path_buf.push("foo");
assert!(!path_buf.is_empty());
}

#[test]
pub fn test_set_extension() {
macro_rules! tfe(
Expand Down

0 comments on commit 82163fe

Please sign in to comment.