Skip to content

Commit 534b295

Browse files
committed
Add a new type parameter to new Range types
1 parent f29256d commit 534b295

2 files changed

Lines changed: 105 additions & 90 deletions

File tree

library/core/src/ops/range.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,11 @@ impl<T: Clone> Bound<&T> {
817817
#[stable(feature = "collections_range", since = "1.28.0")]
818818
#[rustc_diagnostic_item = "RangeBounds"]
819819
#[rustc_const_unstable(feature = "const_range", issue = "none")]
820-
pub const trait RangeBounds<T: ?Sized> {
820+
pub const trait RangeBounds<
821+
Start: ?Sized,
822+
#[unstable(feature = "new_range_end_bound", issue = "155456")] End: ?Sized = Start,
823+
>
824+
{
821825
/// Start index bound.
822826
///
823827
/// Returns the start value as a `Bound`.
@@ -832,7 +836,7 @@ pub const trait RangeBounds<T: ?Sized> {
832836
/// assert_eq!((3..10).start_bound(), Included(&3));
833837
/// ```
834838
#[stable(feature = "collections_range", since = "1.28.0")]
835-
fn start_bound(&self) -> Bound<&T>;
839+
fn start_bound(&self) -> Bound<&Start>;
836840

837841
/// End index bound.
838842
///
@@ -848,7 +852,7 @@ pub const trait RangeBounds<T: ?Sized> {
848852
/// assert_eq!((3..10).end_bound(), Excluded(&10));
849853
/// ```
850854
#[stable(feature = "collections_range", since = "1.28.0")]
851-
fn end_bound(&self) -> Bound<&T>;
855+
fn end_bound(&self) -> Bound<&End>;
852856

853857
/// Returns `true` if `item` is contained in the range.
854858
///
@@ -867,8 +871,9 @@ pub const trait RangeBounds<T: ?Sized> {
867871
#[stable(feature = "range_contains", since = "1.35.0")]
868872
fn contains<U>(&self, item: &U) -> bool
869873
where
870-
T: [const] PartialOrd<U>,
871-
U: ?Sized + [const] PartialOrd<T>,
874+
Start: [const] PartialOrd<U>,
875+
End: [const] PartialOrd<U>,
876+
U: ?Sized + [const] PartialOrd<Start> + [const] PartialOrd<End>,
872877
{
873878
(match self.start_bound() {
874879
Included(start) => start <= item,
@@ -935,7 +940,7 @@ pub const trait RangeBounds<T: ?Sized> {
935940
#[unstable(feature = "range_bounds_is_empty", issue = "137300")]
936941
fn is_empty(&self) -> bool
937942
where
938-
T: [const] PartialOrd,
943+
Start: [const] PartialOrd<End>,
939944
{
940945
!match (self.start_bound(), self.end_bound()) {
941946
(Unbounded, _) | (_, Unbounded) => true,
@@ -954,7 +959,11 @@ pub const trait RangeBounds<T: ?Sized> {
954959
/// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
955960
#[unstable(feature = "range_into_bounds", issue = "136903")]
956961
#[rustc_const_unstable(feature = "const_range", issue = "none")]
957-
pub const trait IntoBounds<T>: [const] RangeBounds<T> {
962+
pub const trait IntoBounds<
963+
Start,
964+
#[unstable(feature = "new_range_end_bound", issue = "155456")] End = Start,
965+
>: [const] RangeBounds<Start, End>
966+
{
958967
/// Convert this range into the start and end bounds.
959968
/// Returns `(start_bound, end_bound)`.
960969
///
@@ -968,7 +977,7 @@ pub const trait IntoBounds<T>: [const] RangeBounds<T> {
968977
/// assert_eq!((0..5).into_bounds(), (Included(0), Excluded(5)));
969978
/// assert_eq!((..=7).into_bounds(), (Unbounded, Included(7)));
970979
/// ```
971-
fn into_bounds(self) -> (Bound<T>, Bound<T>);
980+
fn into_bounds(self) -> (Bound<Start>, Bound<End>);
972981

973982
/// Compute the intersection of `self` and `other`.
974983
///
@@ -997,11 +1006,12 @@ pub const trait IntoBounds<T>: [const] RangeBounds<T> {
9971006
/// assert!(!(-12..387).intersect(0..256).is_empty());
9981007
/// assert!((1..5).intersect(6..).is_empty());
9991008
/// ```
1000-
fn intersect<R>(self, other: R) -> (Bound<T>, Bound<T>)
1009+
fn intersect<R>(self, other: R) -> (Bound<Start>, Bound<End>)
10011010
where
10021011
Self: Sized,
1003-
T: [const] Ord + [const] Destruct,
1004-
R: Sized + [const] IntoBounds<T>,
1012+
Start: [const] Ord + [const] Destruct,
1013+
End: [const] Ord + [const] Destruct,
1014+
R: Sized + [const] IntoBounds<Start, End>,
10051015
{
10061016
let (self_start, self_end) = IntoBounds::into_bounds(self);
10071017
let (other_start, other_end) = IntoBounds::into_bounds(other);
@@ -1056,15 +1066,15 @@ impl<T: ?Sized> const RangeBounds<T> for RangeFull {
10561066

10571067
#[unstable(feature = "range_into_bounds", issue = "136903")]
10581068
#[rustc_const_unstable(feature = "const_range", issue = "none")]
1059-
impl<T> const IntoBounds<T> for RangeFull {
1069+
impl<T> const IntoBounds<T, T> for RangeFull {
10601070
fn into_bounds(self) -> (Bound<T>, Bound<T>) {
10611071
(Unbounded, Unbounded)
10621072
}
10631073
}
10641074

10651075
#[stable(feature = "collections_range", since = "1.28.0")]
10661076
#[rustc_const_unstable(feature = "const_range", issue = "none")]
1067-
impl<T> const RangeBounds<T> for RangeFrom<T> {
1077+
impl<T> const RangeBounds<T, T> for RangeFrom<T> {
10681078
fn start_bound(&self) -> Bound<&T> {
10691079
Included(&self.start)
10701080
}
@@ -1075,15 +1085,15 @@ impl<T> const RangeBounds<T> for RangeFrom<T> {
10751085

10761086
#[unstable(feature = "range_into_bounds", issue = "136903")]
10771087
#[rustc_const_unstable(feature = "const_range", issue = "none")]
1078-
impl<T> const IntoBounds<T> for RangeFrom<T> {
1088+
impl<T> const IntoBounds<T, T> for RangeFrom<T> {
10791089
fn into_bounds(self) -> (Bound<T>, Bound<T>) {
10801090
(Included(self.start), Unbounded)
10811091
}
10821092
}
10831093

10841094
#[stable(feature = "collections_range", since = "1.28.0")]
10851095
#[rustc_const_unstable(feature = "const_range", issue = "none")]
1086-
impl<T> const RangeBounds<T> for RangeTo<T> {
1096+
impl<T> const RangeBounds<T, T> for RangeTo<T> {
10871097
fn start_bound(&self) -> Bound<&T> {
10881098
Unbounded
10891099
}
@@ -1094,15 +1104,15 @@ impl<T> const RangeBounds<T> for RangeTo<T> {
10941104

10951105
#[unstable(feature = "range_into_bounds", issue = "136903")]
10961106
#[rustc_const_unstable(feature = "const_range", issue = "none")]
1097-
impl<T> const IntoBounds<T> for RangeTo<T> {
1107+
impl<T> const IntoBounds<T, T> for RangeTo<T> {
10981108
fn into_bounds(self) -> (Bound<T>, Bound<T>) {
10991109
(Unbounded, Excluded(self.end))
11001110
}
11011111
}
11021112

11031113
#[stable(feature = "collections_range", since = "1.28.0")]
11041114
#[rustc_const_unstable(feature = "const_range", issue = "none")]
1105-
impl<T> const RangeBounds<T> for Range<T> {
1115+
impl<T> const RangeBounds<T, T> for Range<T> {
11061116
fn start_bound(&self) -> Bound<&T> {
11071117
Included(&self.start)
11081118
}

0 commit comments

Comments
 (0)