Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,30 @@ impl OsString {
#[inline]
#[rustc_confusables("append", "put")]
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
trait SpecPushTo {
fn spec_push_to(&self, buf: &mut OsString);
}

impl<T: AsRef<OsStr>> SpecPushTo for T {
#[inline]
default fn spec_push_to(&self, buf: &mut OsString) {
buf.inner.push_slice(&self.as_ref().inner);
}
}

// Use a more efficient implementation when the string is UTF-8.
macro spec_str($T:ty) {
impl SpecPushTo for $T {
#[inline]
fn spec_push_to(&self, buf: &mut OsString) {
buf.inner.push_str(self);
}
}
}
spec_str!(str);
spec_str!(String);

s.spec_push_to(self)
}

/// Creates a new `OsString` with at least the given capacity.
Expand Down Expand Up @@ -587,7 +610,30 @@ impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
/// Copies any value implementing <code>[AsRef]&lt;[OsStr]&gt;</code>
/// into a newly allocated [`OsString`].
fn from(s: &T) -> OsString {
s.as_ref().to_os_string()
trait SpecToOsString {
fn spec_to_os_string(&self) -> OsString;
}

impl<T: AsRef<OsStr>> SpecToOsString for T {
#[inline]
default fn spec_to_os_string(&self) -> OsString {
self.as_ref().to_os_string()
}
}

// Preserve the known-UTF-8 property for strings.
macro spec_str($T:ty) {
impl SpecToOsString for $T {
#[inline]
fn spec_to_os_string(&self) -> OsString {
OsString::from(String::from(self))
}
}
}
spec_str!(str);
spec_str!(String);

s.spec_to_os_string()
}
}

Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/os_str/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ impl Buf {
self.inner.extend_from_slice(&s.inner)
}

#[inline]
pub fn push_str(&mut self, s: &str) {
self.inner.extend_from_slice(s.as_bytes());
}

#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/os_str/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ impl Buf {
self.inner.push_wtf8(&s.inner)
}

#[inline]
pub fn push_str(&mut self, s: &str) {
self.inner.push_str(s);
}

#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
Expand Down
Loading