Skip to content

Commit a68202d

Browse files
Make RangeBounds impls const
1 parent fd9fc7c commit a68202d

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
#![feature(const_ptr_offset_from)]
128128
#![feature(const_ptr_read)]
129129
#![feature(const_ptr_write)]
130+
#![feature(const_range_bounds)]
130131
#![feature(const_raw_ptr_comparison)]
131132
#![feature(const_size_of_val)]
132133
#![feature(const_slice_from_raw_parts)]

library/core/src/ops/range.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -807,18 +807,19 @@ pub trait RangeBounds<T: ?Sized> {
807807
/// assert!(!(0.0..f32::NAN).contains(&0.5));
808808
/// assert!(!(f32::NAN..1.0).contains(&0.5));
809809
#[stable(feature = "range_contains", since = "1.35.0")]
810+
#[default_method_body_is_const]
810811
fn contains<U>(&self, item: &U) -> bool
811812
where
812-
T: PartialOrd<U>,
813-
U: ?Sized + PartialOrd<T>,
813+
T: ~const PartialOrd<U>,
814+
U: ?Sized + ~const PartialOrd<T>,
814815
{
815816
(match self.start_bound() {
816-
Included(start) => start <= item,
817-
Excluded(start) => start < item,
817+
Included(start) => start.le(item),
818+
Excluded(start) => start.lt(item),
818819
Unbounded => true,
819820
}) && (match self.end_bound() {
820-
Included(end) => item <= end,
821-
Excluded(end) => item < end,
821+
Included(end) => item.le(end),
822+
Excluded(end) => item.lt(end),
822823
Unbounded => true,
823824
})
824825
}
@@ -827,7 +828,8 @@ pub trait RangeBounds<T: ?Sized> {
827828
use self::Bound::{Excluded, Included, Unbounded};
828829

829830
#[stable(feature = "collections_range", since = "1.28.0")]
830-
impl<T: ?Sized> RangeBounds<T> for RangeFull {
831+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
832+
impl<T: ?Sized> const RangeBounds<T> for RangeFull {
831833
fn start_bound(&self) -> Bound<&T> {
832834
Unbounded
833835
}
@@ -837,7 +839,8 @@ impl<T: ?Sized> RangeBounds<T> for RangeFull {
837839
}
838840

839841
#[stable(feature = "collections_range", since = "1.28.0")]
840-
impl<T> RangeBounds<T> for RangeFrom<T> {
842+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
843+
impl<T> const RangeBounds<T> for RangeFrom<T> {
841844
fn start_bound(&self) -> Bound<&T> {
842845
Included(&self.start)
843846
}
@@ -847,7 +850,8 @@ impl<T> RangeBounds<T> for RangeFrom<T> {
847850
}
848851

849852
#[stable(feature = "collections_range", since = "1.28.0")]
850-
impl<T> RangeBounds<T> for RangeTo<T> {
853+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
854+
impl<T> const RangeBounds<T> for RangeTo<T> {
851855
fn start_bound(&self) -> Bound<&T> {
852856
Unbounded
853857
}
@@ -857,7 +861,8 @@ impl<T> RangeBounds<T> for RangeTo<T> {
857861
}
858862

859863
#[stable(feature = "collections_range", since = "1.28.0")]
860-
impl<T> RangeBounds<T> for Range<T> {
864+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
865+
impl<T> const RangeBounds<T> for Range<T> {
861866
fn start_bound(&self) -> Bound<&T> {
862867
Included(&self.start)
863868
}
@@ -867,7 +872,8 @@ impl<T> RangeBounds<T> for Range<T> {
867872
}
868873

869874
#[stable(feature = "collections_range", since = "1.28.0")]
870-
impl<T> RangeBounds<T> for RangeInclusive<T> {
875+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
876+
impl<T> const RangeBounds<T> for RangeInclusive<T> {
871877
fn start_bound(&self) -> Bound<&T> {
872878
Included(&self.start)
873879
}
@@ -883,7 +889,8 @@ impl<T> RangeBounds<T> for RangeInclusive<T> {
883889
}
884890

885891
#[stable(feature = "collections_range", since = "1.28.0")]
886-
impl<T> RangeBounds<T> for RangeToInclusive<T> {
892+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
893+
impl<T> const RangeBounds<T> for RangeToInclusive<T> {
887894
fn start_bound(&self) -> Bound<&T> {
888895
Unbounded
889896
}
@@ -893,7 +900,8 @@ impl<T> RangeBounds<T> for RangeToInclusive<T> {
893900
}
894901

895902
#[stable(feature = "collections_range", since = "1.28.0")]
896-
impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
903+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
904+
impl<T> const RangeBounds<T> for (Bound<T>, Bound<T>) {
897905
fn start_bound(&self) -> Bound<&T> {
898906
match *self {
899907
(Included(ref start), _) => Included(start),
@@ -912,7 +920,8 @@ impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
912920
}
913921

914922
#[stable(feature = "collections_range", since = "1.28.0")]
915-
impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
923+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
924+
impl<'a, T: ?Sized + 'a> const RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
916925
fn start_bound(&self) -> Bound<&T> {
917926
self.0
918927
}
@@ -923,7 +932,8 @@ impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
923932
}
924933

925934
#[stable(feature = "collections_range", since = "1.28.0")]
926-
impl<T> RangeBounds<T> for RangeFrom<&T> {
935+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
936+
impl<T> const RangeBounds<T> for RangeFrom<&T> {
927937
fn start_bound(&self) -> Bound<&T> {
928938
Included(self.start)
929939
}
@@ -933,7 +943,8 @@ impl<T> RangeBounds<T> for RangeFrom<&T> {
933943
}
934944

935945
#[stable(feature = "collections_range", since = "1.28.0")]
936-
impl<T> RangeBounds<T> for RangeTo<&T> {
946+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
947+
impl<T> const RangeBounds<T> for RangeTo<&T> {
937948
fn start_bound(&self) -> Bound<&T> {
938949
Unbounded
939950
}
@@ -943,7 +954,8 @@ impl<T> RangeBounds<T> for RangeTo<&T> {
943954
}
944955

945956
#[stable(feature = "collections_range", since = "1.28.0")]
946-
impl<T> RangeBounds<T> for Range<&T> {
957+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
958+
impl<T> const RangeBounds<T> for Range<&T> {
947959
fn start_bound(&self) -> Bound<&T> {
948960
Included(self.start)
949961
}
@@ -953,7 +965,8 @@ impl<T> RangeBounds<T> for Range<&T> {
953965
}
954966

955967
#[stable(feature = "collections_range", since = "1.28.0")]
956-
impl<T> RangeBounds<T> for RangeInclusive<&T> {
968+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
969+
impl<T> const RangeBounds<T> for RangeInclusive<&T> {
957970
fn start_bound(&self) -> Bound<&T> {
958971
Included(self.start)
959972
}
@@ -963,7 +976,8 @@ impl<T> RangeBounds<T> for RangeInclusive<&T> {
963976
}
964977

965978
#[stable(feature = "collections_range", since = "1.28.0")]
966-
impl<T> RangeBounds<T> for RangeToInclusive<&T> {
979+
#[rustc_const_unstable(feature = "const_range_bounds", issue = "none")]
980+
impl<T> const RangeBounds<T> for RangeToInclusive<&T> {
967981
fn start_bound(&self) -> Bound<&T> {
968982
Unbounded
969983
}

0 commit comments

Comments
 (0)