Skip to content

Commit 85dea54

Browse files
committed
constify comparison traits on multiple sliced types
1 parent d52e5dc commit 85dea54

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

library/core/src/bstr/traits.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,35 @@ use crate::slice::SliceIndex;
66
use crate::{hash, ops, range};
77

88
#[unstable(feature = "bstr", issue = "134915")]
9-
impl Ord for ByteStr {
9+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
10+
impl const Ord for ByteStr {
1011
#[inline]
1112
fn cmp(&self, other: &ByteStr) -> Ordering {
1213
Ord::cmp(&self.0, &other.0)
1314
}
1415
}
1516

1617
#[unstable(feature = "bstr", issue = "134915")]
17-
impl PartialOrd for ByteStr {
18+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
19+
impl const PartialOrd for ByteStr {
1820
#[inline]
1921
fn partial_cmp(&self, other: &ByteStr) -> Option<Ordering> {
2022
PartialOrd::partial_cmp(&self.0, &other.0)
2123
}
2224
}
2325

2426
#[unstable(feature = "bstr", issue = "134915")]
25-
impl PartialEq<ByteStr> for ByteStr {
27+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
28+
impl const PartialEq<ByteStr> for ByteStr {
2629
#[inline]
2730
fn eq(&self, other: &ByteStr) -> bool {
2831
&self.0 == &other.0
2932
}
3033
}
3134

3235
#[unstable(feature = "bstr", issue = "134915")]
33-
impl Eq for ByteStr {}
36+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
37+
impl const Eq for ByteStr {}
3438

3539
#[unstable(feature = "bstr", issue = "134915")]
3640
impl hash::Hash for ByteStr {

library/core/src/ffi/c_str.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ use crate::{fmt, ops, slice, str};
8888
/// ```
8989
///
9090
/// [str]: prim@str "str"
91-
#[derive(PartialEq, Eq, Hash)]
91+
#[derive(Hash)]
92+
#[derive_const(PartialEq, Eq)]
9293
#[stable(feature = "core_c_str", since = "1.64.0")]
9394
#[rustc_diagnostic_item = "cstr_type"]
9495
#[rustc_has_incoherent_inherent_impls]
@@ -669,15 +670,17 @@ impl PartialEq<&Self> for CStr {
669670
// because `c_char` is `i8` (not `u8`) on some platforms.
670671
// That is why this is implemented manually and not derived.
671672
#[stable(feature = "rust1", since = "1.0.0")]
672-
impl PartialOrd for CStr {
673+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
674+
impl const PartialOrd for CStr {
673675
#[inline]
674676
fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
675677
self.to_bytes().partial_cmp(&other.to_bytes())
676678
}
677679
}
678680

679681
#[stable(feature = "rust1", since = "1.0.0")]
680-
impl Ord for CStr {
682+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
683+
impl const Ord for CStr {
681684
#[inline]
682685
fn cmp(&self, other: &CStr) -> Ordering {
683686
self.to_bytes().cmp(&other.to_bytes())

library/core/src/panic/location.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub struct Location<'a> {
4747
}
4848

4949
#[stable(feature = "panic_hooks", since = "1.10.0")]
50-
impl PartialEq for Location<'_> {
50+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
51+
impl const PartialEq for Location<'_> {
5152
fn eq(&self, other: &Self) -> bool {
5253
// Compare col / line first as they're cheaper to compare and more likely to differ,
5354
// while not impacting the result.
@@ -56,20 +57,26 @@ impl PartialEq for Location<'_> {
5657
}
5758

5859
#[stable(feature = "panic_hooks", since = "1.10.0")]
59-
impl Eq for Location<'_> {}
60+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
61+
impl const Eq for Location<'_> {}
6062

6163
#[stable(feature = "panic_hooks", since = "1.10.0")]
62-
impl Ord for Location<'_> {
64+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
65+
impl const Ord for Location<'_> {
6366
fn cmp(&self, other: &Self) -> Ordering {
64-
self.file()
65-
.cmp(other.file())
66-
.then_with(|| self.line.cmp(&other.line))
67-
.then_with(|| self.col.cmp(&other.col))
67+
match self.file().cmp(other.file()) {
68+
Ordering::Equal => match self.line.cmp(&other.line) {
69+
Ordering::Equal => self.col.cmp(&other.col),
70+
ordering => ordering,
71+
},
72+
ordering => ordering,
73+
}
6874
}
6975
}
7076

7177
#[stable(feature = "panic_hooks", since = "1.10.0")]
72-
impl PartialOrd for Location<'_> {
78+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
79+
impl const PartialOrd for Location<'_> {
7380
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
7481
Some(self.cmp(other))
7582
}

library/core/src/str/traits.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{ops, ptr, range};
1515
/// culturally-accepted standards requires locale-specific data that is outside the scope of
1616
/// the `str` type.
1717
#[stable(feature = "rust1", since = "1.0.0")]
18-
impl Ord for str {
18+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
19+
impl const Ord for str {
1920
#[inline]
2021
fn cmp(&self, other: &str) -> Ordering {
2122
self.as_bytes().cmp(other.as_bytes())
@@ -32,7 +33,8 @@ impl const PartialEq for str {
3233
}
3334

3435
#[stable(feature = "rust1", since = "1.0.0")]
35-
impl Eq for str {}
36+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
37+
impl const Eq for str {}
3638

3739
/// Implements comparison operations on strings.
3840
///
@@ -42,7 +44,8 @@ impl Eq for str {}
4244
/// culturally-accepted standards requires locale-specific data that is outside the scope of
4345
/// the `str` type.
4446
#[stable(feature = "rust1", since = "1.0.0")]
45-
impl PartialOrd for str {
47+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
48+
impl const PartialOrd for str {
4649
#[inline]
4750
fn partial_cmp(&self, other: &str) -> Option<Ordering> {
4851
Some(self.cmp(other))

library/core/src/wtf8.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ use crate::{ops, slice, str};
3333
/// Compares with the `char` type,
3434
/// which represents a Unicode scalar value:
3535
/// a code point that is not a surrogate (U+D800 to U+DFFF).
36-
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
36+
#[derive(Clone, Copy)]
37+
#[derive_const(Eq, PartialEq, Ord, PartialOrd)]
3738
#[doc(hidden)]
3839
pub struct CodePoint(CodePointInner);
3940

@@ -123,7 +124,7 @@ impl CodePoint {
123124
///
124125
/// Similar to `&str`, but can additionally contain surrogate code points
125126
/// if they’re not in a surrogate pair.
126-
#[derive(Eq, Ord, PartialEq, PartialOrd)]
127+
#[derive_const(Eq, Ord, PartialEq, PartialOrd)]
127128
#[repr(transparent)]
128129
#[rustc_has_incoherent_inherent_impls]
129130
#[doc(hidden)]

0 commit comments

Comments
 (0)