Skip to content

Commit a14a4fc

Browse files
committedFeb 15, 2023
Constify RangeBounds, RangeX::contains and RangeX::is_empty.
1 parent 999ac5f commit a14a4fc

File tree

2 files changed

+63
-40
lines changed

2 files changed

+63
-40
lines changed
 

‎library/core/src/cmp.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1491,9 +1491,10 @@ mod impls {
14911491
}
14921492
}
14931493
#[stable(feature = "rust1", since = "1.0.0")]
1494-
impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A
1494+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
1495+
impl<A: ?Sized, B: ?Sized> const PartialOrd<&B> for &A
14951496
where
1496-
A: PartialOrd<B>,
1497+
A: ~const PartialOrd<B>,
14971498
{
14981499
#[inline]
14991500
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {

‎library/core/src/ops/range.rs

+60-38
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
9696
}
9797
}
9898

99-
impl<Idx: PartialOrd<Idx>> Range<Idx> {
99+
impl<Idx: ~const PartialOrd<Idx>> Range<Idx> {
100100
/// Returns `true` if `item` is contained in the range.
101101
///
102102
/// # Examples
@@ -116,10 +116,11 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
116116
/// assert!(!(f32::NAN..1.0).contains(&0.5));
117117
/// ```
118118
#[stable(feature = "range_contains", since = "1.35.0")]
119-
pub fn contains<U>(&self, item: &U) -> bool
119+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
120+
pub const fn contains<U>(&self, item: &U) -> bool
120121
where
121-
Idx: PartialOrd<U>,
122-
U: ?Sized + PartialOrd<Idx>,
122+
Idx: ~const PartialOrd<U>,
123+
U: ?Sized + ~const PartialOrd<Idx>,
123124
{
124125
<Self as RangeBounds<Idx>>::contains(self, item)
125126
}
@@ -142,7 +143,8 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
142143
/// assert!( (f32::NAN..5.0).is_empty());
143144
/// ```
144145
#[stable(feature = "range_is_empty", since = "1.47.0")]
145-
pub fn is_empty(&self) -> bool {
146+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
147+
pub const fn is_empty(&self) -> bool {
146148
!(self.start < self.end)
147149
}
148150
}
@@ -199,7 +201,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
199201
}
200202
}
201203

202-
impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
204+
impl<Idx: ~const PartialOrd<Idx>> RangeFrom<Idx> {
203205
/// Returns `true` if `item` is contained in the range.
204206
///
205207
/// # Examples
@@ -214,10 +216,11 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
214216
/// assert!(!(f32::NAN..).contains(&0.5));
215217
/// ```
216218
#[stable(feature = "range_contains", since = "1.35.0")]
217-
pub fn contains<U>(&self, item: &U) -> bool
219+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
220+
pub const fn contains<U>(&self, item: &U) -> bool
218221
where
219-
Idx: PartialOrd<U>,
220-
U: ?Sized + PartialOrd<Idx>,
222+
Idx: ~const PartialOrd<U>,
223+
U: ?Sized + ~const PartialOrd<Idx>,
221224
{
222225
<Self as RangeBounds<Idx>>::contains(self, item)
223226
}
@@ -280,7 +283,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
280283
}
281284
}
282285

283-
impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
286+
impl<Idx: ~const PartialOrd<Idx>> RangeTo<Idx> {
284287
/// Returns `true` if `item` is contained in the range.
285288
///
286289
/// # Examples
@@ -295,10 +298,11 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
295298
/// assert!(!(..f32::NAN).contains(&0.5));
296299
/// ```
297300
#[stable(feature = "range_contains", since = "1.35.0")]
298-
pub fn contains<U>(&self, item: &U) -> bool
301+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
302+
pub const fn contains<U>(&self, item: &U) -> bool
299303
where
300-
Idx: PartialOrd<U>,
301-
U: ?Sized + PartialOrd<Idx>,
304+
Idx: ~const PartialOrd<U>,
305+
U: ?Sized + ~const PartialOrd<Idx>,
302306
{
303307
<Self as RangeBounds<Idx>>::contains(self, item)
304308
}
@@ -437,7 +441,8 @@ impl<Idx> RangeInclusive<Idx> {
437441
/// ```
438442
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
439443
#[inline]
440-
pub fn into_inner(self) -> (Idx, Idx) {
444+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
445+
pub const fn into_inner(self) -> (Idx, Idx) {
441446
(self.start, self.end)
442447
}
443448
}
@@ -469,7 +474,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
469474
}
470475
}
471476

472-
impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
477+
impl<Idx: ~const PartialOrd<Idx>> RangeInclusive<Idx> {
473478
/// Returns `true` if `item` is contained in the range.
474479
///
475480
/// # Examples
@@ -500,10 +505,11 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
500505
/// assert!(!r.contains(&3) && !r.contains(&5));
501506
/// ```
502507
#[stable(feature = "range_contains", since = "1.35.0")]
503-
pub fn contains<U>(&self, item: &U) -> bool
508+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
509+
pub const fn contains<U>(&self, item: &U) -> bool
504510
where
505-
Idx: PartialOrd<U>,
506-
U: ?Sized + PartialOrd<Idx>,
511+
Idx: ~const PartialOrd<U>,
512+
U: ?Sized + ~const PartialOrd<Idx>,
507513
{
508514
<Self as RangeBounds<Idx>>::contains(self, item)
509515
}
@@ -535,8 +541,9 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
535541
/// assert!(r.is_empty());
536542
/// ```
537543
#[stable(feature = "range_is_empty", since = "1.47.0")]
544+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
538545
#[inline]
539-
pub fn is_empty(&self) -> bool {
546+
pub const fn is_empty(&self) -> bool {
540547
self.exhausted || !(self.start <= self.end)
541548
}
542549
}
@@ -598,7 +605,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
598605
}
599606
}
600607

601-
impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
608+
impl<Idx: ~const PartialOrd<Idx>> RangeToInclusive<Idx> {
602609
/// Returns `true` if `item` is contained in the range.
603610
///
604611
/// # Examples
@@ -613,10 +620,11 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
613620
/// assert!(!(..=f32::NAN).contains(&0.5));
614621
/// ```
615622
#[stable(feature = "range_contains", since = "1.35.0")]
616-
pub fn contains<U>(&self, item: &U) -> bool
623+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
624+
pub const fn contains<U>(&self, item: &U) -> bool
617625
where
618-
Idx: PartialOrd<U>,
619-
U: ?Sized + PartialOrd<Idx>,
626+
Idx: ~const PartialOrd<U>,
627+
U: ?Sized + ~const PartialOrd<Idx>,
620628
{
621629
<Self as RangeBounds<Idx>>::contains(self, item)
622630
}
@@ -757,6 +765,7 @@ impl<T: Clone> Bound<&T> {
757765
/// `RangeBounds` is implemented by Rust's built-in range types, produced
758766
/// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
759767
#[stable(feature = "collections_range", since = "1.28.0")]
768+
#[const_trait]
760769
pub trait RangeBounds<T: ?Sized> {
761770
/// Start index bound.
762771
///
@@ -809,8 +818,8 @@ pub trait RangeBounds<T: ?Sized> {
809818
#[stable(feature = "range_contains", since = "1.35.0")]
810819
fn contains<U>(&self, item: &U) -> bool
811820
where
812-
T: PartialOrd<U>,
813-
U: ?Sized + PartialOrd<T>,
821+
T: ~const PartialOrd<U>,
822+
U: ?Sized + ~const PartialOrd<T>,
814823
{
815824
(match self.start_bound() {
816825
Included(start) => start <= item,
@@ -827,7 +836,8 @@ pub trait RangeBounds<T: ?Sized> {
827836
use self::Bound::{Excluded, Included, Unbounded};
828837

829838
#[stable(feature = "collections_range", since = "1.28.0")]
830-
impl<T: ?Sized> RangeBounds<T> for RangeFull {
839+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
840+
impl<T: ?Sized> const RangeBounds<T> for RangeFull {
831841
fn start_bound(&self) -> Bound<&T> {
832842
Unbounded
833843
}
@@ -837,7 +847,8 @@ impl<T: ?Sized> RangeBounds<T> for RangeFull {
837847
}
838848

839849
#[stable(feature = "collections_range", since = "1.28.0")]
840-
impl<T> RangeBounds<T> for RangeFrom<T> {
850+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
851+
impl<T> const RangeBounds<T> for RangeFrom<T> {
841852
fn start_bound(&self) -> Bound<&T> {
842853
Included(&self.start)
843854
}
@@ -847,7 +858,8 @@ impl<T> RangeBounds<T> for RangeFrom<T> {
847858
}
848859

849860
#[stable(feature = "collections_range", since = "1.28.0")]
850-
impl<T> RangeBounds<T> for RangeTo<T> {
861+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
862+
impl<T> const RangeBounds<T> for RangeTo<T> {
851863
fn start_bound(&self) -> Bound<&T> {
852864
Unbounded
853865
}
@@ -857,7 +869,8 @@ impl<T> RangeBounds<T> for RangeTo<T> {
857869
}
858870

859871
#[stable(feature = "collections_range", since = "1.28.0")]
860-
impl<T> RangeBounds<T> for Range<T> {
872+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
873+
impl<T> const RangeBounds<T> for Range<T> {
861874
fn start_bound(&self) -> Bound<&T> {
862875
Included(&self.start)
863876
}
@@ -867,7 +880,8 @@ impl<T> RangeBounds<T> for Range<T> {
867880
}
868881

869882
#[stable(feature = "collections_range", since = "1.28.0")]
870-
impl<T> RangeBounds<T> for RangeInclusive<T> {
883+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
884+
impl<T> const RangeBounds<T> for RangeInclusive<T> {
871885
fn start_bound(&self) -> Bound<&T> {
872886
Included(&self.start)
873887
}
@@ -883,7 +897,8 @@ impl<T> RangeBounds<T> for RangeInclusive<T> {
883897
}
884898

885899
#[stable(feature = "collections_range", since = "1.28.0")]
886-
impl<T> RangeBounds<T> for RangeToInclusive<T> {
900+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
901+
impl<T> const RangeBounds<T> for RangeToInclusive<T> {
887902
fn start_bound(&self) -> Bound<&T> {
888903
Unbounded
889904
}
@@ -893,7 +908,8 @@ impl<T> RangeBounds<T> for RangeToInclusive<T> {
893908
}
894909

895910
#[stable(feature = "collections_range", since = "1.28.0")]
896-
impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
911+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
912+
impl<T> const RangeBounds<T> for (Bound<T>, Bound<T>) {
897913
fn start_bound(&self) -> Bound<&T> {
898914
match *self {
899915
(Included(ref start), _) => Included(start),
@@ -912,7 +928,8 @@ impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
912928
}
913929

914930
#[stable(feature = "collections_range", since = "1.28.0")]
915-
impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
931+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
932+
impl<'a, T: ?Sized + 'a> const RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
916933
fn start_bound(&self) -> Bound<&T> {
917934
self.0
918935
}
@@ -923,7 +940,8 @@ impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
923940
}
924941

925942
#[stable(feature = "collections_range", since = "1.28.0")]
926-
impl<T> RangeBounds<T> for RangeFrom<&T> {
943+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
944+
impl<T> const RangeBounds<T> for RangeFrom<&T> {
927945
fn start_bound(&self) -> Bound<&T> {
928946
Included(self.start)
929947
}
@@ -933,7 +951,8 @@ impl<T> RangeBounds<T> for RangeFrom<&T> {
933951
}
934952

935953
#[stable(feature = "collections_range", since = "1.28.0")]
936-
impl<T> RangeBounds<T> for RangeTo<&T> {
954+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
955+
impl<T> const RangeBounds<T> for RangeTo<&T> {
937956
fn start_bound(&self) -> Bound<&T> {
938957
Unbounded
939958
}
@@ -943,7 +962,8 @@ impl<T> RangeBounds<T> for RangeTo<&T> {
943962
}
944963

945964
#[stable(feature = "collections_range", since = "1.28.0")]
946-
impl<T> RangeBounds<T> for Range<&T> {
965+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
966+
impl<T> const RangeBounds<T> for Range<&T> {
947967
fn start_bound(&self) -> Bound<&T> {
948968
Included(self.start)
949969
}
@@ -953,7 +973,8 @@ impl<T> RangeBounds<T> for Range<&T> {
953973
}
954974

955975
#[stable(feature = "collections_range", since = "1.28.0")]
956-
impl<T> RangeBounds<T> for RangeInclusive<&T> {
976+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
977+
impl<T> const RangeBounds<T> for RangeInclusive<&T> {
957978
fn start_bound(&self) -> Bound<&T> {
958979
Included(self.start)
959980
}
@@ -963,7 +984,8 @@ impl<T> RangeBounds<T> for RangeInclusive<&T> {
963984
}
964985

965986
#[stable(feature = "collections_range", since = "1.28.0")]
966-
impl<T> RangeBounds<T> for RangeToInclusive<&T> {
987+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
988+
impl<T> const RangeBounds<T> for RangeToInclusive<&T> {
967989
fn start_bound(&self) -> Bound<&T> {
968990
Unbounded
969991
}

0 commit comments

Comments
 (0)