Skip to content

is_empty for OsStr #30623

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

Closed
wants to merge 4 commits 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 @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
29 changes: 29 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ impl PathBuf {
pub fn into_os_string(self) -> OsString {
self.inner
}

}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -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
///
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add a #![feature(os_extras)] here in this code block

/// 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")]
Expand Down Expand Up @@ -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(
Expand Down