mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-03 17:35:28 +03:00
Add impls for iterators of Cow<OsStr>
This commit is contained in:
@@ -1204,6 +1204,16 @@ fn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T) {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "osstring_extend", since = "1.52.0")]
|
||||
impl<'a> Extend<Cow<'a, OsStr>> for OsString {
|
||||
#[inline]
|
||||
fn extend<T: IntoIterator<Item = Cow<'a, OsStr>>>(&mut self, iter: T) {
|
||||
for s in iter {
|
||||
self.push(&s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "osstring_extend", since = "1.52.0")]
|
||||
impl FromIterator<OsString> for OsString {
|
||||
#[inline]
|
||||
@@ -1234,3 +1244,27 @@ fn from_iter<I: IntoIterator<Item = &'a OsStr>>(iter: I) -> Self {
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "osstring_extend", since = "1.52.0")]
|
||||
impl<'a> FromIterator<Cow<'a, OsStr>> for OsString {
|
||||
#[inline]
|
||||
fn from_iter<I: IntoIterator<Item = Cow<'a, OsStr>>>(iter: I) -> Self {
|
||||
let mut iterator = iter.into_iter();
|
||||
|
||||
// Because we're iterating over `OsString`s, we can avoid at least
|
||||
// one allocation by getting the first owned string from the iterator
|
||||
// and appending to it all the subsequent strings.
|
||||
match iterator.next() {
|
||||
None => OsString::new(),
|
||||
Some(Cow::Owned(mut buf)) => {
|
||||
buf.extend(iterator);
|
||||
buf
|
||||
}
|
||||
Some(Cow::Borrowed(buf)) => {
|
||||
let mut buf = OsString::from(buf);
|
||||
buf.extend(iterator);
|
||||
buf
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user