Implement [OsStr]::join
Second attempt at implementing [OsStr]::join.
This commit is contained in:
parent
0dd7e10282
commit
cb60e70dc4
@ -1222,6 +1222,23 @@ impl OsStr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[unstable(feature = "slice_concat_ext", issue = "27747")]
|
||||||
|
impl<S: Borrow<OsStr>> alloc::slice::Join<&OsStr> for [S] {
|
||||||
|
type Output = OsString;
|
||||||
|
|
||||||
|
fn join(slice: &Self, sep: &OsStr) -> OsString {
|
||||||
|
let Some((first, suffix)) = slice.split_first() else {
|
||||||
|
return OsString::new();
|
||||||
|
};
|
||||||
|
let first_owned = first.borrow().to_owned();
|
||||||
|
suffix.iter().fold(first_owned, |mut a, b| {
|
||||||
|
a.push(sep);
|
||||||
|
a.push(b.borrow());
|
||||||
|
a
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl Borrow<OsStr> for OsString {
|
impl Borrow<OsStr> for OsString {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -84,6 +84,20 @@ fn test_os_string_reserve_exact() {
|
|||||||
assert!(os_string.capacity() >= 33)
|
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]
|
#[test]
|
||||||
fn test_os_string_default() {
|
fn test_os_string_default() {
|
||||||
let os_string: OsString = Default::default();
|
let os_string: OsString = Default::default();
|
||||||
|
@ -241,6 +241,7 @@
|
|||||||
#![feature(intra_doc_pointers)]
|
#![feature(intra_doc_pointers)]
|
||||||
#![feature(lang_items)]
|
#![feature(lang_items)]
|
||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
|
#![feature(let_else)]
|
||||||
#![feature(linkage)]
|
#![feature(linkage)]
|
||||||
#![feature(min_specialization)]
|
#![feature(min_specialization)]
|
||||||
#![feature(must_not_suspend)]
|
#![feature(must_not_suspend)]
|
||||||
@ -301,6 +302,7 @@
|
|||||||
#![feature(toowned_clone_into)]
|
#![feature(toowned_clone_into)]
|
||||||
#![feature(try_reserve_kind)]
|
#![feature(try_reserve_kind)]
|
||||||
#![feature(vec_into_raw_parts)]
|
#![feature(vec_into_raw_parts)]
|
||||||
|
#![feature(slice_concat_trait)]
|
||||||
//
|
//
|
||||||
// Library features (unwind):
|
// Library features (unwind):
|
||||||
#![feature(panic_unwind)]
|
#![feature(panic_unwind)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user