rust-lang/rust · #165618
Implement RangeIntersect on core::range types
library/core/src/ops/range.rs0 + / 71 −
@@ -1,6 +1,5 @@ use crate::fmt; use crate::hash::Hash;-use crate::marker::Destruct; /// An unbounded range (`..`). /// /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.@@ -987,76 +986,6 @@ pub const trait IntoBounds<T>: [const] RangeBounds<T> { /// assert_eq!((..=7).into_bounds(), (Unbounded, Included(7))); /// ``` fn into_bounds(self) -> (Bound<T>, Bound<T>);-- /// Compute the intersection of `self` and `other`.- ///- /// # Examples- ///- /// ```- /// #![feature(range_into_bounds)]- /// use std::ops::Bound::*;- /// use std::ops::IntoBounds;- ///- /// assert_eq!((3..).intersect(..5), (Included(3), Excluded(5)));- /// assert_eq!((-12..387).intersect(0..256), (Included(0), Excluded(256)));- /// assert_eq!((1..5).intersect(..), (Included(1), Excluded(5)));- /// assert_eq!((1..=9).intersect(0..10), (Included(1), Included(9)));- /// assert_eq!((7..=13).intersect(8..13), (Included(8), Excluded(13)));- /// ```- ///- /// Combine with `is_empty` to determine if two ranges overlap.- ///- /// ```- /// #![feature(range_into_bounds)]- /// #![feature(range_bounds_is_empty)]- /// use std::ops::{RangeBounds, IntoBounds};- ///- /// assert!(!(3..).intersect(..5).is_empty());- /// assert!(!(-12..387).intersect(0..256).is_empty());- /// assert!((1..5).intersect(6..).is_empty());- /// ```- fn intersect<R>(self, other: R) -> (Bound<T>, Bound<T>)- where- Self: Sized,- T: [const] Ord + [const] Destruct,- R: Sized + [const] IntoBounds<T>,- {- let (self_start, self_end) = IntoBounds::into_bounds(self);- let (other_start, other_end) = IntoBounds::into_bounds(other);-- let start = match (self_start, other_start) {- (Included(a), Included(b)) => Included(Ord::max(a, b)),- (Excluded(a), Excluded(b)) => Excluded(Ord::max(a, b)),- (Unbounded, Unbounded) => Unbounded,-- (x, Unbounded) | (Unbounded, x) => x,-- (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {- if i > e {- Included(i)- } else {- Excluded(e)- }- }- };- let end = match (self_end, other_end) {- (Included(a), Included(b)) => Included(Ord::min(a, b)),- (Excluded(a), Excluded(b)) => Excluded(Ord::min(a, b)),- (Unbounded, Unbounded) => Unbounded,-- (x, Unbounded) | (Unbounded, x) => x,-- (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {- if i < e {- Included(i)- } else {- Excluded(e)- }- }- };-- (start, end)- } } use self::Bound::{Excluded, Included, Unbounded};library/core/src/range.rs450 + / 0 −
@@ -17,6 +17,7 @@ use crate::fmt; use crate::hash::Hash;+use crate::marker::Destruct; mod iter; @@ -759,3 +760,452 @@ where (OneSidedRangeBound::EndInclusive, self.last) } }++/// Computes the intersection of two ranges, returning the most specific range type possible.+///+/// This trait is not implemented for all combinations, only the ones with a good+/// `Self::Intersection` type available.+///+/// However, because `(Bound<T>, Bound<T>): RangeIntersect`, you can always use+/// `a.into_bounds().intersect(b.into_bounds())` for any two [`IntoBounds`] types if needed.+///+/// # Examples+///+/// ```+/// #![feature(range_intersect)]+/// use core::range::{Range, RangeFrom, RangeInclusive, RangeIntersect};+///+/// assert_eq!(+/// Range::from(-12..387).intersect(Range::from(0..256)),+/// Range::from(0..256),+/// );+/// assert_eq!(+/// RangeFrom::from(3..).intersect(..5),+/// Range::from(3..5),+/// );+/// assert_eq!(+/// RangeInclusive::from(1..=9).intersect(RangeFrom::from(5..)),+/// RangeInclusive::from(5..=9),+/// );+/// ```+///+/// Combine with `is_empty` to determine if two ranges overlap:+///+/// ```+/// #![feature(range_intersect)]+/// use core::range::{Range, RangeFrom, RangeIntersect};+///+/// assert!(!RangeFrom::from(3..).intersect(..5).is_empty());+/// assert!(!Range::from(-12..387).intersect(Range::from(0..256)).is_empty());+/// assert!(Range::from(1..5).intersect(RangeFrom::from(6..)).is_empty());+/// ```+#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+pub const trait RangeIntersect<Rhs = Self>: Sized {+ /// The resulting range type of the intersection.+ #[unstable(feature = "range_intersect", issue = "165428")]+ type Intersection;++ /// Computes the intersection of `self` and `rhs`.+ #[unstable(feature = "range_intersect", issue = "165428")]+ #[must_use]+ fn intersect(self, rhs: Rhs) -> Self::Intersection;+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect for (Bound<T>, Bound<T>)+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = Self;++ #[inline]+ fn intersect(self, rhs: Self) -> Self::Intersection {+ let (self_start, self_end) = self;+ let (other_start, other_end) = rhs;++ let start = match (self_start, other_start) {+ (Included(a), Included(b)) => Included(Ord::max(a, b)),+ (Excluded(a), Excluded(b)) => Excluded(Ord::max(a, b)),+ (Unbounded, Unbounded) => Unbounded,++ (x, Unbounded) | (Unbounded, x) => x,++ (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {+ if i > e {+ Included(i)+ } else {+ Excluded(e)+ }+ }+ };+ let end = match (self_end, other_end) {+ (Included(a), Included(b)) => Included(Ord::min(a, b)),+ (Excluded(a), Excluded(b)) => Excluded(Ord::min(a, b)),+ (Unbounded, Unbounded) => Unbounded,++ (x, Unbounded) | (Unbounded, x) => x,++ (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {+ if i < e {+ Included(i)+ } else {+ Excluded(e)+ }+ }+ };++ (start, end)+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect for Range<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: Self) -> Self::Intersection {+ Range { start: Ord::max(self.start, rhs.start), end: Ord::min(self.end, rhs.end) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFrom<T>> for Range<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {+ Range { start: Ord::max(self.start, rhs.start), end: self.end }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeTo<T>> for Range<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {+ Range { start: self.start, end: Ord::min(self.end, rhs.end) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFull> for Range<T> {+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, _rhs: RangeFull) -> Self::Intersection {+ self+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<Range<T>> for RangeFrom<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: Range<T>) -> Self::Intersection {+ Range { start: Ord::max(self.start, rhs.start), end: rhs.end }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect for RangeFrom<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeFrom<T>;++ #[inline]+ fn intersect(self, rhs: Self) -> Self::Intersection {+ RangeFrom { start: Ord::max(self.start, rhs.start) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeTo<T>> for RangeFrom<T> {+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {+ Range { start: self.start, end: rhs.end }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFull> for RangeFrom<T> {+ type Intersection = RangeFrom<T>;++ #[inline]+ fn intersect(self, _rhs: RangeFull) -> Self::Intersection {+ self+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeInclusive<T>> for RangeFrom<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {+ RangeInclusive { start: Ord::max(self.start, rhs.start), last: rhs.last }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeFrom<T> {+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {+ RangeInclusive { start: self.start, last: rhs.last }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<Range<T>> for RangeTo<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: Range<T>) -> Self::Intersection {+ Range { start: rhs.start, end: Ord::min(self.end, rhs.end) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFrom<T>> for RangeTo<T> {+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {+ Range { start: rhs.start, end: self.end }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect for RangeTo<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeTo<T>;++ #[inline]+ fn intersect(self, rhs: Self) -> Self::Intersection {+ RangeTo { end: Ord::min(self.end, rhs.end) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFull> for RangeTo<T> {+ type Intersection = RangeTo<T>;++ #[inline]+ fn intersect(self, _rhs: RangeFull) -> Self::Intersection {+ self+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<Range<T>> for RangeFull {+ type Intersection = Range<T>;++ #[inline]+ fn intersect(self, rhs: Range<T>) -> Self::Intersection {+ rhs+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFrom<T>> for RangeFull {+ type Intersection = RangeFrom<T>;++ #[inline]+ fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {+ rhs+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeTo<T>> for RangeFull {+ type Intersection = RangeTo<T>;++ #[inline]+ fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {+ rhs+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl RangeIntersect for RangeFull {+ type Intersection = RangeFull;++ #[inline]+ fn intersect(self, _rhs: RangeFull) -> Self::Intersection {+ RangeFull+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeInclusive<T>> for RangeFull {+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {+ rhs+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeFull {+ type Intersection = RangeToInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {+ rhs+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFrom<T>> for RangeInclusive<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {+ RangeInclusive { start: Ord::max(self.start, rhs.start), last: self.last }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFull> for RangeInclusive<T> {+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, _rhs: RangeFull) -> Self::Intersection {+ self+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect for RangeInclusive<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: Self) -> Self::Intersection {+ RangeInclusive {+ start: Ord::max(self.start, rhs.start),+ last: Ord::min(self.last, rhs.last),+ }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeInclusive<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {+ RangeInclusive { start: self.start, last: Ord::min(self.last, rhs.last) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFrom<T>> for RangeToInclusive<T> {+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {+ RangeInclusive { start: rhs.start, last: self.last }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeFull> for RangeToInclusive<T> {+ type Intersection = RangeToInclusive<T>;++ #[inline]+ fn intersect(self, _rhs: RangeFull) -> Self::Intersection {+ self+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect<RangeInclusive<T>> for RangeToInclusive<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeInclusive<T>;++ #[inline]+ fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {+ RangeInclusive { start: rhs.start, last: Ord::min(self.last, rhs.last) }+ }+}++#[unstable(feature = "range_intersect", issue = "165428")]+#[rustc_const_unstable(feature = "const_range", issue = "none")]+const impl<T> RangeIntersect for RangeToInclusive<T>+where+ T: [const] Ord + [const] Destruct,+{+ type Intersection = RangeToInclusive<T>;++ #[inline]+ fn intersect(self, rhs: Self) -> Self::Intersection {+ RangeToInclusive { last: Ord::min(self.last, rhs.last) }+ }+}library/coretests/tests/lib.rs2 + / 0 −
@@ -106,6 +106,8 @@ #![feature(pointer_is_aligned_to)] #![feature(portable_simd)] #![feature(ptr_metadata)]+#![feature(range_intersect)]+#![feature(range_into_bounds)] #![feature(raw_os_error_ty)] #![feature(rustc_attrs)] #![feature(signed_bigint_helpers)]library/coretests/tests/ops.rs83 + / 0 −
@@ -318,3 +318,86 @@ fn test_fmt() { assert_eq!(format!("{:?}", ..=1), "..=1"); assert_eq!(format!("{:?}", ..), ".."); }++#[test]+fn test_range_intersect() {+ use core::ops::Bound::{Excluded, Included, Unbounded};+ use core::ops::IntoBounds;+ use core::range::{+ Range as NewRange, RangeFrom as NewRangeFrom, RangeInclusive as NewRangeInclusive,+ RangeIntersect, RangeToInclusive as NewRangeToInclusive,+ };++ assert_eq!(NewRange::from(-12..387).intersect(NewRange::from(0..256)), NewRange::from(0..256));+ assert_eq!(NewRange::from(1..5).intersect(NewRangeFrom::from(3..)), NewRange::from(3..5));+ assert_eq!(NewRange::from(1..5).intersect(..3), NewRange::from(1..3));+ assert_eq!(NewRange::from(1..5).intersect(..), NewRange::from(1..5));++ assert_eq!(NewRangeFrom::from(3..).intersect(NewRange::from(1..5)), NewRange::from(3..5));+ assert_eq!(NewRangeFrom::from(3..).intersect(NewRangeFrom::from(5..)), NewRangeFrom::from(5..));+ assert_eq!(NewRangeFrom::from(3..).intersect(..5), NewRange::from(3..5));+ assert_eq!(NewRangeFrom::from(3..).intersect(..), NewRangeFrom::from(3..));+ assert_eq!(+ NewRangeFrom::from(3..).intersect(NewRangeInclusive::from(1..=5)),+ NewRangeInclusive::from(3..=5),+ );+ assert_eq!(+ NewRangeFrom::from(3..).intersect(NewRangeToInclusive::from(..=5)),+ NewRangeInclusive::from(3..=5),+ );++ assert_eq!((..5).intersect(NewRange::from(1..8)), NewRange::from(1..5));+ assert_eq!((..5).intersect(NewRangeFrom::from(3..)), NewRange::from(3..5));+ assert_eq!((..5).intersect(..3), ..3);+ assert_eq!((..5).intersect(..), ..5);++ assert_eq!((..).intersect(NewRange::from(1..5)), NewRange::from(1..5));+ assert_eq!((..).intersect(NewRangeFrom::from(3..)), NewRangeFrom::from(3..));+ assert_eq!((..).intersect(..5), ..5);+ assert_eq!((..).intersect(..), ..);+ assert_eq!((..).intersect(NewRangeInclusive::from(1..=5)), NewRangeInclusive::from(1..=5));+ assert_eq!((..).intersect(NewRangeToInclusive::from(..=5)), NewRangeToInclusive::from(..=5));++ assert_eq!(+ NewRangeInclusive::from(1..=9).intersect(NewRangeFrom::from(3..)),+ NewRangeInclusive::from(3..=9),+ );+ assert_eq!(NewRangeInclusive::from(1..=9).intersect(..), NewRangeInclusive::from(1..=9));+ assert_eq!(+ NewRangeInclusive::from(1..=9).intersect(NewRangeInclusive::from(3..=5)),+ NewRangeInclusive::from(3..=5),+ );+ assert_eq!(+ NewRangeInclusive::from(1..=9).intersect(NewRangeToInclusive::from(..=5)),+ NewRangeInclusive::from(1..=5),+ );++ assert_eq!(+ NewRangeToInclusive::from(..=9).intersect(NewRangeFrom::from(3..)),+ NewRangeInclusive::from(3..=9),+ );+ assert_eq!(NewRangeToInclusive::from(..=9).intersect(..), NewRangeToInclusive::from(..=9));+ assert_eq!(+ NewRangeToInclusive::from(..=9).intersect(NewRangeInclusive::from(3..=12)),+ NewRangeInclusive::from(3..=9),+ );+ assert_eq!(+ NewRangeToInclusive::from(..=9).intersect(NewRangeToInclusive::from(..=5)),+ NewRangeToInclusive::from(..=5),+ );++ // Fallback via into_bounds() for combinations without a concrete range type+ assert_eq!((1..=9).into_bounds().intersect((0..10).into_bounds()), (Included(1), Included(9)));+ assert_eq!(+ (7..=13).into_bounds().intersect((8..13).into_bounds()),+ (Included(8), Excluded(13)),+ );+ assert_eq!(+ (Excluded(1), Included(9)).intersect((Included(2), Excluded(9))),+ (Included(2), Excluded(9)),+ );+ assert_eq!(+ (Unbounded::<i32>, Unbounded).intersect((Included(1), Excluded(5))),+ (Included(1), Excluded(5)),+ );+}