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

Implement [OsStr]::join #96744

Merged
merged 1 commit into from
May 6, 2022
Merged
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
17 changes: 17 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,23 @@ impl OsStr {
}
}

#[unstable(feature = "slice_concat_ext", issue = "27747")]
Copy link
Member

Choose a reason for hiding this comment

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

The tracking issue #27747 was closed a while ago.

Copy link
Member Author

Choose a reason for hiding this comment

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

I used the same tracking issue the other impl blocks for that trait have. The reason to close the tracking issue was because the feature is to be perma-unstable: #27747 (comment) . I'm not sure about changes in policy, but in the older times tracking issues for perma-unstable things would remain open indefinitely.

impl<S: Borrow<OsStr>> alloc::slice::Join<&OsStr> for [S] {
type Output = OsString;

fn join(slice: &Self, sep: &OsStr) -> OsString {
let Some(first) = slice.first() else {
return OsString::new();
Copy link

Choose a reason for hiding this comment

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

Why not use slice::split_first?

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't know the function existed, thanks! Feel free to file a PR.

Ideally, one would make an optimized implementation like there is for str slices, but OsStr is platform dependent in its representation as well as the invariants, so it's not as easy as just exporting that function from alloc, probably as perma unstable and #[doc(hidden)] and then using it, at least not on unixes. It's a larger project and I wasn't sure if the PR was going to accepted at all, so I went with the simplest implementation I could think of, establishing a baseline that can later be improved. Maybe a good next step would be to use the join_generic_copy function on Unix and fall back on the current implementation on other operating systems, but without benchmarking I don't know how much that is an improvement.

};
let first = first.borrow().to_owned();
slice[1..].iter().fold(first, |mut a, b| {
a.push(sep);
a.push(b.borrow());
a
})
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Borrow<OsStr> for OsString {
#[inline]
Expand Down
14 changes: 14 additions & 0 deletions library/std/src/ffi/os_str/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ fn test_os_string_reserve_exact() {
assert!(os_string.capacity() >= 33)
}

#[test]
fn test_os_string_join() {
let strings = [OsStr::new("hello"), OsStr::new("dear"), OsStr::new("world")];
assert_eq!("hello", strings[..1].join(OsStr::new(" ")));
assert_eq!("hello dear world", strings.join(OsStr::new(" ")));
assert_eq!("hellodearworld", strings.join(OsStr::new("")));
assert_eq!("hello.\n dear.\n world", strings.join(OsStr::new(".\n ")));

assert_eq!("dear world", strings[1..].join(&OsString::from(" ")));

let strings_abc = [OsString::from("a"), OsString::from("b"), OsString::from("c")];
assert_eq!("a b c", strings_abc.join(OsStr::new(" ")));
}

#[test]
fn test_os_string_default() {
let os_string: OsString = Default::default();
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
#![feature(intra_doc_pointers)]
#![feature(lang_items)]
#![feature(let_chains)]
#![feature(let_else)]
#![feature(linkage)]
#![feature(min_specialization)]
#![feature(must_not_suspend)]
Expand Down Expand Up @@ -300,6 +301,7 @@
#![feature(toowned_clone_into)]
#![feature(try_reserve_kind)]
#![feature(vec_into_raw_parts)]
#![feature(slice_concat_trait)]
//
// Library features (unwind):
#![feature(panic_unwind)]
Expand Down