Skip to content

Commit 3ed6184

Browse files
committed
Implement Extend and FromIterator for OsString
Add the following trait impls: - `impl Extend<OsString> for OsString` - `impl<'a> Extend<&'a OsStr> for OsString` - `impl FromIterator<OsString> for OsString` - `impl<'a> FromIterator<&'a OsStr> for OsString` Because `OsString` is a platform string with no particular semantics, concatenating them together seems acceptable.
1 parent 5fa22fe commit 3ed6184

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

library/std/src/ffi/os_str.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::borrow::{Borrow, Cow};
55
use crate::cmp;
66
use crate::fmt;
77
use crate::hash::{Hash, Hasher};
8+
use crate::iter::{Extend, FromIterator};
89
use crate::ops;
910
use crate::rc::Rc;
1011
use crate::str::FromStr;
@@ -1182,3 +1183,47 @@ impl FromStr for OsString {
11821183
Ok(OsString::from(s))
11831184
}
11841185
}
1186+
1187+
#[stable(feature = "osstring_extend", since = "1.52.0")]
1188+
impl Extend<OsString> for OsString {
1189+
#[inline]
1190+
fn extend<T: IntoIterator<Item = OsString>>(&mut self, iter: T) {
1191+
for s in iter {
1192+
self.push(&s);
1193+
}
1194+
}
1195+
}
1196+
1197+
#[stable(feature = "osstring_extend", since = "1.52.0")]
1198+
impl<'a> Extend<&'a OsStr> for OsString {
1199+
#[inline]
1200+
fn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T) {
1201+
for s in iter {
1202+
self.push(s);
1203+
}
1204+
}
1205+
}
1206+
1207+
#[stable(feature = "osstring_extend", since = "1.52.0")]
1208+
impl FromIterator<OsString> for OsString {
1209+
#[inline]
1210+
fn from_iter<I: IntoIterator<Item = OsString>>(iter: I) -> Self {
1211+
let mut buf = Self::new();
1212+
for s in iter {
1213+
buf.push(&s);
1214+
}
1215+
buf
1216+
}
1217+
}
1218+
1219+
#[stable(feature = "osstring_extend", since = "1.52.0")]
1220+
impl<'a> FromIterator<&'a OsStr> for OsString {
1221+
#[inline]
1222+
fn from_iter<I: IntoIterator<Item = &'a OsStr>>(iter: I) -> Self {
1223+
let mut buf = Self::new();
1224+
for s in iter {
1225+
buf.push(s);
1226+
}
1227+
buf
1228+
}
1229+
}

0 commit comments

Comments
 (0)