Skip to content

Commit e5cde7f

Browse files
Rollup merge of #147790 - npmccallum:slices, r=clarfonthey
constify comparison traits on sliced types Constify comparison traits for `str`, `CStr`, `ByteStr`, and `Location`. The slice comparison traits originally included here have since landed independently, so this update drops that already-merged commit and rebases the remaining constifications. r? clarfonthey Tracking issue #143800
2 parents cd992b4 + 181a305 commit e5cde7f

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

‎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+
const impl 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+
const impl 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+
const impl 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+
const impl 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
@@ -89,7 +89,8 @@ use crate::{fmt, ops, range, slice, str};
8989
/// ```
9090
///
9191
/// [str]: prim@str "str"
92-
#[derive(PartialEq, Eq, Hash)]
92+
#[derive(Hash)]
93+
#[derive_const(PartialEq, Eq)]
9394
#[stable(feature = "core_c_str", since = "1.64.0")]
9495
#[rustc_diagnostic_item = "cstr_type"]
9596
#[rustc_has_incoherent_inherent_impls]
@@ -691,15 +692,17 @@ impl PartialEq<&Self> for CStr {
691692
// because `c_char` is `i8` (not `u8`) on some platforms.
692693
// That is why this is implemented manually and not derived.
693694
#[stable(feature = "rust1", since = "1.0.0")]
694-
impl PartialOrd for CStr {
695+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
696+
const impl PartialOrd for CStr {
695697
#[inline]
696698
fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
697699
self.to_bytes().partial_cmp(other.to_bytes())
698700
}
699701
}
700702

701703
#[stable(feature = "rust1", since = "1.0.0")]
702-
impl Ord for CStr {
704+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
705+
const impl Ord for CStr {
703706
#[inline]
704707
fn cmp(&self, other: &CStr) -> Ordering {
705708
self.to_bytes().cmp(other.to_bytes())

‎library/core/src/panic/location.rs‎

Lines changed: 10 additions & 6 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+
const impl 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,23 @@ 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+
const impl 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+
const impl Ord for Location<'_> {
6366
fn cmp(&self, other: &Self) -> Ordering {
6467
self.file()
6568
.cmp(other.file())
66-
.then_with(|| self.line.cmp(&other.line))
67-
.then_with(|| self.col.cmp(&other.col))
69+
.then_with(const || self.line.cmp(&other.line))
70+
.then_with(const || self.col.cmp(&other.col))
6871
}
6972
}
7073

7174
#[stable(feature = "panic_hooks", since = "1.10.0")]
72-
impl PartialOrd for Location<'_> {
75+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
76+
const impl PartialOrd for Location<'_> {
7377
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
7478
Some(self.cmp(other))
7579
}

‎library/core/src/str/traits.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{ops, 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+
const impl Ord for str {
1920
#[inline]
2021
fn cmp(&self, other: &str) -> Ordering {
2122
self.as_bytes().cmp(other.as_bytes())
@@ -43,7 +44,8 @@ const impl Eq for str {}
4344
/// culturally-accepted standards requires locale-specific data that is outside the scope of
4445
/// the `str` type.
4546
#[stable(feature = "rust1", since = "1.0.0")]
46-
impl PartialOrd for str {
47+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
48+
const impl PartialOrd for str {
4749
#[inline]
4850
fn partial_cmp(&self, other: &str) -> Option<Ordering> {
4951
Some(self.cmp(other))

0 commit comments

Comments
 (0)