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.
This commit is contained in:
Ryan Lopopolo 2021-02-14 15:01:46 -08:00
parent 5fa22fe6f8
commit 3ed6184434
No known key found for this signature in database
GPG key ID: 46047D739B6AE0B1

View file

@ -5,6 +5,7 @@ use crate::borrow::{Borrow, Cow};
use crate::cmp;
use crate::fmt;
use crate::hash::{Hash, Hasher};
use crate::iter::{Extend, FromIterator};
use crate::ops;
use crate::rc::Rc;
use crate::str::FromStr;
@ -1182,3 +1183,47 @@ impl FromStr for OsString {
Ok(OsString::from(s))
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl Extend<OsString> for OsString {
#[inline]
fn extend<T: IntoIterator<Item = OsString>>(&mut self, iter: T) {
for s in iter {
self.push(&s);
}
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl<'a> Extend<&'a OsStr> for OsString {
#[inline]
fn extend<T: IntoIterator<Item = &'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]
fn from_iter<I: IntoIterator<Item = OsString>>(iter: I) -> Self {
let mut buf = Self::new();
for s in iter {
buf.push(&s);
}
buf
}
}
#[stable(feature = "osstring_extend", since = "1.52.0")]
impl<'a> FromIterator<&'a OsStr> for OsString {
#[inline]
fn from_iter<I: IntoIterator<Item = &'a OsStr>>(iter: I) -> Self {
let mut buf = Self::new();
for s in iter {
buf.push(s);
}
buf
}
}