Skip to content

Commit

Permalink
inline some common methods on OsStr
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Dec 9, 2019
1 parent 14195e1 commit bf1f1c2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libstd/ffi/os_str.rs
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
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
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

0 comments on commit bf1f1c2

Please sign in to comment.