Skip to content

Commit 989b806

Browse files
committed
Auto merge of #96881 - est31:join_osstr, r=dtolnay
Implement [OsStr]::join Implements join for `OsStr` and `OsString` slices: ```Rust let strings = [OsStr::new("hello"), OsStr::new("dear"), OsStr::new("world")]; assert_eq!("hello dear world", strings.join(OsStr::new(" "))); ```` This saves one from converting to strings and back, or from implementing it manually. This PR has been re-filed after #96744 was first accidentally merged and then reverted. The change is instantly stable and thus: r? rust-lang/libs-api `@rustbot` label +T-libs-api -T-libs cc `@thomcc` `@m-ou-se` `@faptc`
2 parents 47365c0 + cb60e70 commit 989b806

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

library/std/src/ffi/os_str.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,23 @@ impl OsStr {
12221222
}
12231223
}
12241224

1225+
#[unstable(feature = "slice_concat_ext", issue = "27747")]
1226+
impl<S: Borrow<OsStr>> alloc::slice::Join<&OsStr> for [S] {
1227+
type Output = OsString;
1228+
1229+
fn join(slice: &Self, sep: &OsStr) -> OsString {
1230+
let Some((first, suffix)) = slice.split_first() else {
1231+
return OsString::new();
1232+
};
1233+
let first_owned = first.borrow().to_owned();
1234+
suffix.iter().fold(first_owned, |mut a, b| {
1235+
a.push(sep);
1236+
a.push(b.borrow());
1237+
a
1238+
})
1239+
}
1240+
}
1241+
12251242
#[stable(feature = "rust1", since = "1.0.0")]
12261243
impl Borrow<OsStr> for OsString {
12271244
#[inline]

library/std/src/ffi/os_str/tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ fn test_os_string_reserve_exact() {
8484
assert!(os_string.capacity() >= 33)
8585
}
8686

87+
#[test]
88+
fn test_os_string_join() {
89+
let strings = [OsStr::new("hello"), OsStr::new("dear"), OsStr::new("world")];
90+
assert_eq!("hello", strings[..1].join(OsStr::new(" ")));
91+
assert_eq!("hello dear world", strings.join(OsStr::new(" ")));
92+
assert_eq!("hellodearworld", strings.join(OsStr::new("")));
93+
assert_eq!("hello.\n dear.\n world", strings.join(OsStr::new(".\n ")));
94+
95+
assert_eq!("dear world", strings[1..].join(&OsString::from(" ")));
96+
97+
let strings_abc = [OsString::from("a"), OsString::from("b"), OsString::from("c")];
98+
assert_eq!("a b c", strings_abc.join(OsStr::new(" ")));
99+
}
100+
87101
#[test]
88102
fn test_os_string_default() {
89103
let os_string: OsString = Default::default();

library/std/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
#![feature(intra_doc_pointers)]
242242
#![feature(lang_items)]
243243
#![feature(let_chains)]
244+
#![feature(let_else)]
244245
#![feature(linkage)]
245246
#![feature(min_specialization)]
246247
#![feature(must_not_suspend)]
@@ -301,6 +302,7 @@
301302
#![feature(toowned_clone_into)]
302303
#![feature(try_reserve_kind)]
303304
#![feature(vec_into_raw_parts)]
305+
#![feature(slice_concat_trait)]
304306
//
305307
// Library features (unwind):
306308
#![feature(panic_unwind)]

0 commit comments

Comments
 (0)