Skip to content

Commit 05ea200

Browse files
committed
Add impls for iterators of Cow<OsStr>
1 parent 2fcb8b5 commit 05ea200

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

library/std/src/ffi/os_str.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,16 @@ impl<'a> Extend<&'a OsStr> for OsString {
12041204
}
12051205
}
12061206

1207+
#[stable(feature = "osstring_extend", since = "1.52.0")]
1208+
impl<'a> Extend<Cow<'a, OsStr>> for OsString {
1209+
#[inline]
1210+
fn extend<T: IntoIterator<Item = Cow<'a, OsStr>>>(&mut self, iter: T) {
1211+
for s in iter {
1212+
self.push(&s);
1213+
}
1214+
}
1215+
}
1216+
12071217
#[stable(feature = "osstring_extend", since = "1.52.0")]
12081218
impl FromIterator<OsString> for OsString {
12091219
#[inline]
@@ -1234,3 +1244,27 @@ impl<'a> FromIterator<&'a OsStr> for OsString {
12341244
buf
12351245
}
12361246
}
1247+
1248+
#[stable(feature = "osstring_extend", since = "1.52.0")]
1249+
impl<'a> FromIterator<Cow<'a, OsStr>> for OsString {
1250+
#[inline]
1251+
fn from_iter<I: IntoIterator<Item = Cow<'a, OsStr>>>(iter: I) -> Self {
1252+
let mut iterator = iter.into_iter();
1253+
1254+
// Because we're iterating over `OsString`s, we can avoid at least
1255+
// one allocation by getting the first owned string from the iterator
1256+
// and appending to it all the subsequent strings.
1257+
match iterator.next() {
1258+
None => OsString::new(),
1259+
Some(Cow::Owned(mut buf)) => {
1260+
buf.extend(iterator);
1261+
buf
1262+
}
1263+
Some(Cow::Borrowed(buf)) => {
1264+
let mut buf = OsString::from(buf);
1265+
buf.extend(iterator);
1266+
buf
1267+
}
1268+
}
1269+
}
1270+
}

0 commit comments

Comments
 (0)