Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_empty() for Path and OsStr #31231

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ impl OsStr {
self.to_bytes().and_then(|b| CString::new(b).ok())
}

/// Checks if the string is empty.
#[unstable(feature = "osstr_is_empty", 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
Expand Down
29 changes: 29 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,24 @@ 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
///
/// ```
/// # #![feature(os_extras)]
/// use std::path::Path;
///
/// let path = Path::new("/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 @@ -3239,6 +3257,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(
Expand Down