Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inline some common methods on OsStr #67169

Merged
merged 1 commit into from
Dec 10, 2019
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
8 changes: 8 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,13 @@ impl OsStr {
///
/// let os_str = OsStr::new("foo");
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
s.as_ref()
}

#[inline]
fn from_inner(inner: &Slice) -> &OsStr {
unsafe { &*(inner as *const Slice as *const OsStr) }
}
Expand Down Expand Up @@ -658,6 +660,7 @@ impl OsStr {
///
/// Note: it is *crucial* that this API is private, to avoid
/// revealing the internal, platform-specific encodings.
#[inline]
fn bytes(&self) -> &[u8] {
unsafe { &*(&self.inner as *const _ as *const [u8]) }
}
Expand Down Expand Up @@ -797,20 +800,23 @@ impl Default for &OsStr {

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for OsStr {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
self.bytes().eq(other.bytes())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<str> for OsStr {
#[inline]
fn eq(&self, other: &str) -> bool {
*self == *OsStr::new(other)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq<OsStr> for str {
#[inline]
fn eq(&self, other: &OsStr) -> bool {
*other == *OsStr::new(self)
}
Expand Down Expand Up @@ -944,13 +950,15 @@ impl AsRef<OsStr> for OsString {

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for str {
#[inline]
fn as_ref(&self) -> &OsStr {
OsStr::from_inner(Slice::from_str(self))
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for String {
#[inline]
fn as_ref(&self) -> &OsStr {
(&**self).as_ref()
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl Buf {
}

impl Slice {
#[inline]
pub fn from_str(s: &str) -> &Slice {
unsafe { mem::transmute(Wtf8::from_str(s)) }
}
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys_common/os_str_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ impl Buf {
}

impl Slice {
#[inline]
fn from_u8_slice(s: &[u8]) -> &Slice {
unsafe { mem::transmute(s) }
}

#[inline]
pub fn from_str(s: &str) -> &Slice {
Slice::from_u8_slice(s.as_bytes())
}
Expand Down