Skip to content

Commit 18a59c0

Browse files
committed
Delegate Eq/PartialEq/Ord/PartialOrd/Hash of OsStr to their internal impl.
This is required to make OsStr on Windows behaves as expected.
1 parent 58e8735 commit 18a59c0

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

src/libstd/ffi/os_str.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -675,14 +675,6 @@ impl OsStr {
675675
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
676676
OsString { inner: Buf::from_box(boxed) }
677677
}
678-
679-
/// Gets the underlying byte representation.
680-
///
681-
/// Note: it is *crucial* that this API is private, to avoid
682-
/// revealing the internal, platform-specific encodings.
683-
fn bytes(&self) -> &[u8] {
684-
unsafe { &*(&self.inner as *const _ as *const [u8]) }
685-
}
686678
}
687679

688680
#[stable(feature = "box_from_os_str", since = "1.17.0")]
@@ -819,7 +811,7 @@ impl Default for &OsStr {
819811
#[stable(feature = "rust1", since = "1.0.0")]
820812
impl PartialEq for OsStr {
821813
fn eq(&self, other: &OsStr) -> bool {
822-
self.bytes().eq(other.bytes())
814+
self.inner == other.inner
823815
}
824816
}
825817

@@ -844,16 +836,16 @@ impl Eq for OsStr {}
844836
impl PartialOrd for OsStr {
845837
#[inline]
846838
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
847-
self.bytes().partial_cmp(other.bytes())
839+
self.inner.partial_cmp(&other.inner)
848840
}
849841
#[inline]
850-
fn lt(&self, other: &OsStr) -> bool { self.bytes().lt(other.bytes()) }
842+
fn lt(&self, other: &OsStr) -> bool { self.inner < other.inner }
851843
#[inline]
852-
fn le(&self, other: &OsStr) -> bool { self.bytes().le(other.bytes()) }
844+
fn le(&self, other: &OsStr) -> bool { self.inner <= other.inner }
853845
#[inline]
854-
fn gt(&self, other: &OsStr) -> bool { self.bytes().gt(other.bytes()) }
846+
fn gt(&self, other: &OsStr) -> bool { self.inner > other.inner }
855847
#[inline]
856-
fn ge(&self, other: &OsStr) -> bool { self.bytes().ge(other.bytes()) }
848+
fn ge(&self, other: &OsStr) -> bool { self.inner >= other.inner }
857849
}
858850

859851
#[stable(feature = "rust1", since = "1.0.0")]
@@ -870,7 +862,7 @@ impl PartialOrd<str> for OsStr {
870862
#[stable(feature = "rust1", since = "1.0.0")]
871863
impl Ord for OsStr {
872864
#[inline]
873-
fn cmp(&self, other: &OsStr) -> cmp::Ordering { self.bytes().cmp(other.bytes()) }
865+
fn cmp(&self, other: &OsStr) -> cmp::Ordering { self.inner.cmp(&other.inner) }
874866
}
875867

876868
macro_rules! impl_cmp {
@@ -915,7 +907,7 @@ impl_cmp!(Cow<'a, OsStr>, OsString);
915907
impl Hash for OsStr {
916908
#[inline]
917909
fn hash<H: Hasher>(&self, state: &mut H) {
918-
self.bytes().hash(state)
910+
self.inner.hash(state)
919911
}
920912
}
921913

src/libstd/sys/windows/os_str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl fmt::Display for Buf {
4545
}
4646
}
4747

48+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
4849
pub struct Slice {
4950
pub inner: Wtf8
5051
}

src/libstd/sys_common/os_str_bytes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) struct Buf {
1818
pub inner: Vec<u8>
1919
}
2020

21+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
2122
pub(crate) struct Slice {
2223
pub inner: [u8]
2324
}

0 commit comments

Comments
 (0)