Constify RangeBounds, RangeX::contains and RangeX::is_empty.

This commit is contained in:
onestacked 2023-02-15 15:49:17 +01:00
parent 999ac5f777
commit a14a4fc3d0
2 changed files with 63 additions and 40 deletions

View File

@ -1491,9 +1491,10 @@ mod impls {
} }
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized, B: ?Sized> PartialOrd<&B> for &A #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl<A: ?Sized, B: ?Sized> const PartialOrd<&B> for &A
where where
A: PartialOrd<B>, A: ~const PartialOrd<B>,
{ {
#[inline] #[inline]
fn partial_cmp(&self, other: &&B) -> Option<Ordering> { fn partial_cmp(&self, other: &&B) -> Option<Ordering> {

View File

@ -96,7 +96,7 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
} }
} }
impl<Idx: PartialOrd<Idx>> Range<Idx> { impl<Idx: ~const PartialOrd<Idx>> Range<Idx> {
/// Returns `true` if `item` is contained in the range. /// Returns `true` if `item` is contained in the range.
/// ///
/// # Examples /// # Examples
@ -116,10 +116,11 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// assert!(!(f32::NAN..1.0).contains(&0.5)); /// assert!(!(f32::NAN..1.0).contains(&0.5));
/// ``` /// ```
#[stable(feature = "range_contains", since = "1.35.0")] #[stable(feature = "range_contains", since = "1.35.0")]
pub fn contains<U>(&self, item: &U) -> bool #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn contains<U>(&self, item: &U) -> bool
where where
Idx: PartialOrd<U>, Idx: ~const PartialOrd<U>,
U: ?Sized + PartialOrd<Idx>, U: ?Sized + ~const PartialOrd<Idx>,
{ {
<Self as RangeBounds<Idx>>::contains(self, item) <Self as RangeBounds<Idx>>::contains(self, item)
} }
@ -142,7 +143,8 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// assert!( (f32::NAN..5.0).is_empty()); /// assert!( (f32::NAN..5.0).is_empty());
/// ``` /// ```
#[stable(feature = "range_is_empty", since = "1.47.0")] #[stable(feature = "range_is_empty", since = "1.47.0")]
pub fn is_empty(&self) -> bool { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn is_empty(&self) -> bool {
!(self.start < self.end) !(self.start < self.end)
} }
} }
@ -199,7 +201,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
} }
} }
impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> { impl<Idx: ~const PartialOrd<Idx>> RangeFrom<Idx> {
/// Returns `true` if `item` is contained in the range. /// Returns `true` if `item` is contained in the range.
/// ///
/// # Examples /// # Examples
@ -214,10 +216,11 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
/// assert!(!(f32::NAN..).contains(&0.5)); /// assert!(!(f32::NAN..).contains(&0.5));
/// ``` /// ```
#[stable(feature = "range_contains", since = "1.35.0")] #[stable(feature = "range_contains", since = "1.35.0")]
pub fn contains<U>(&self, item: &U) -> bool #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn contains<U>(&self, item: &U) -> bool
where where
Idx: PartialOrd<U>, Idx: ~const PartialOrd<U>,
U: ?Sized + PartialOrd<Idx>, U: ?Sized + ~const PartialOrd<Idx>,
{ {
<Self as RangeBounds<Idx>>::contains(self, item) <Self as RangeBounds<Idx>>::contains(self, item)
} }
@ -280,7 +283,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
} }
} }
impl<Idx: PartialOrd<Idx>> RangeTo<Idx> { impl<Idx: ~const PartialOrd<Idx>> RangeTo<Idx> {
/// Returns `true` if `item` is contained in the range. /// Returns `true` if `item` is contained in the range.
/// ///
/// # Examples /// # Examples
@ -295,10 +298,11 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
/// assert!(!(..f32::NAN).contains(&0.5)); /// assert!(!(..f32::NAN).contains(&0.5));
/// ``` /// ```
#[stable(feature = "range_contains", since = "1.35.0")] #[stable(feature = "range_contains", since = "1.35.0")]
pub fn contains<U>(&self, item: &U) -> bool #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn contains<U>(&self, item: &U) -> bool
where where
Idx: PartialOrd<U>, Idx: ~const PartialOrd<U>,
U: ?Sized + PartialOrd<Idx>, U: ?Sized + ~const PartialOrd<Idx>,
{ {
<Self as RangeBounds<Idx>>::contains(self, item) <Self as RangeBounds<Idx>>::contains(self, item)
} }
@ -437,7 +441,8 @@ impl<Idx> RangeInclusive<Idx> {
/// ``` /// ```
#[stable(feature = "inclusive_range_methods", since = "1.27.0")] #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline] #[inline]
pub fn into_inner(self) -> (Idx, Idx) { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn into_inner(self) -> (Idx, Idx) {
(self.start, self.end) (self.start, self.end)
} }
} }
@ -469,7 +474,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
} }
} }
impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> { impl<Idx: ~const PartialOrd<Idx>> RangeInclusive<Idx> {
/// Returns `true` if `item` is contained in the range. /// Returns `true` if `item` is contained in the range.
/// ///
/// # Examples /// # Examples
@ -500,10 +505,11 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// assert!(!r.contains(&3) && !r.contains(&5)); /// assert!(!r.contains(&3) && !r.contains(&5));
/// ``` /// ```
#[stable(feature = "range_contains", since = "1.35.0")] #[stable(feature = "range_contains", since = "1.35.0")]
pub fn contains<U>(&self, item: &U) -> bool #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn contains<U>(&self, item: &U) -> bool
where where
Idx: PartialOrd<U>, Idx: ~const PartialOrd<U>,
U: ?Sized + PartialOrd<Idx>, U: ?Sized + ~const PartialOrd<Idx>,
{ {
<Self as RangeBounds<Idx>>::contains(self, item) <Self as RangeBounds<Idx>>::contains(self, item)
} }
@ -535,8 +541,9 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// assert!(r.is_empty()); /// assert!(r.is_empty());
/// ``` /// ```
#[stable(feature = "range_is_empty", since = "1.47.0")] #[stable(feature = "range_is_empty", since = "1.47.0")]
#[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub const fn is_empty(&self) -> bool {
self.exhausted || !(self.start <= self.end) self.exhausted || !(self.start <= self.end)
} }
} }
@ -598,7 +605,7 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
} }
} }
impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> { impl<Idx: ~const PartialOrd<Idx>> RangeToInclusive<Idx> {
/// Returns `true` if `item` is contained in the range. /// Returns `true` if `item` is contained in the range.
/// ///
/// # Examples /// # Examples
@ -613,10 +620,11 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
/// assert!(!(..=f32::NAN).contains(&0.5)); /// assert!(!(..=f32::NAN).contains(&0.5));
/// ``` /// ```
#[stable(feature = "range_contains", since = "1.35.0")] #[stable(feature = "range_contains", since = "1.35.0")]
pub fn contains<U>(&self, item: &U) -> bool #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
pub const fn contains<U>(&self, item: &U) -> bool
where where
Idx: PartialOrd<U>, Idx: ~const PartialOrd<U>,
U: ?Sized + PartialOrd<Idx>, U: ?Sized + ~const PartialOrd<Idx>,
{ {
<Self as RangeBounds<Idx>>::contains(self, item) <Self as RangeBounds<Idx>>::contains(self, item)
} }
@ -757,6 +765,7 @@ impl<T: Clone> Bound<&T> {
/// `RangeBounds` is implemented by Rust's built-in range types, produced /// `RangeBounds` is implemented by Rust's built-in range types, produced
/// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`. /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
#[const_trait]
pub trait RangeBounds<T: ?Sized> { pub trait RangeBounds<T: ?Sized> {
/// Start index bound. /// Start index bound.
/// ///
@ -809,8 +818,8 @@ pub trait RangeBounds<T: ?Sized> {
#[stable(feature = "range_contains", since = "1.35.0")] #[stable(feature = "range_contains", since = "1.35.0")]
fn contains<U>(&self, item: &U) -> bool fn contains<U>(&self, item: &U) -> bool
where where
T: PartialOrd<U>, T: ~const PartialOrd<U>,
U: ?Sized + PartialOrd<T>, U: ?Sized + ~const PartialOrd<T>,
{ {
(match self.start_bound() { (match self.start_bound() {
Included(start) => start <= item, Included(start) => start <= item,
@ -827,7 +836,8 @@ pub trait RangeBounds<T: ?Sized> {
use self::Bound::{Excluded, Included, Unbounded}; use self::Bound::{Excluded, Included, Unbounded};
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T: ?Sized> RangeBounds<T> for RangeFull { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T: ?Sized> const RangeBounds<T> for RangeFull {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Unbounded Unbounded
} }
@ -837,7 +847,8 @@ impl<T: ?Sized> RangeBounds<T> for RangeFull {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeFrom<T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeFrom<T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Included(&self.start) Included(&self.start)
} }
@ -847,7 +858,8 @@ impl<T> RangeBounds<T> for RangeFrom<T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeTo<T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeTo<T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Unbounded Unbounded
} }
@ -857,7 +869,8 @@ impl<T> RangeBounds<T> for RangeTo<T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for Range<T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for Range<T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Included(&self.start) Included(&self.start)
} }
@ -867,7 +880,8 @@ impl<T> RangeBounds<T> for Range<T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeInclusive<T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeInclusive<T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Included(&self.start) Included(&self.start)
} }
@ -883,7 +897,8 @@ impl<T> RangeBounds<T> for RangeInclusive<T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeToInclusive<T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeToInclusive<T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Unbounded Unbounded
} }
@ -893,7 +908,8 @@ impl<T> RangeBounds<T> for RangeToInclusive<T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for (Bound<T>, Bound<T>) {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
match *self { match *self {
(Included(ref start), _) => Included(start), (Included(ref start), _) => Included(start),
@ -912,7 +928,8 @@ impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<'a, T: ?Sized + 'a> const RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
self.0 self.0
} }
@ -923,7 +940,8 @@ impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeFrom<&T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeFrom<&T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Included(self.start) Included(self.start)
} }
@ -933,7 +951,8 @@ impl<T> RangeBounds<T> for RangeFrom<&T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeTo<&T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeTo<&T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Unbounded Unbounded
} }
@ -943,7 +962,8 @@ impl<T> RangeBounds<T> for RangeTo<&T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for Range<&T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for Range<&T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Included(self.start) Included(self.start)
} }
@ -953,7 +973,8 @@ impl<T> RangeBounds<T> for Range<&T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeInclusive<&T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeInclusive<&T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Included(self.start) Included(self.start)
} }
@ -963,7 +984,8 @@ impl<T> RangeBounds<T> for RangeInclusive<&T> {
} }
#[stable(feature = "collections_range", since = "1.28.0")] #[stable(feature = "collections_range", since = "1.28.0")]
impl<T> RangeBounds<T> for RangeToInclusive<&T> { #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
impl<T> const RangeBounds<T> for RangeToInclusive<&T> {
fn start_bound(&self) -> Bound<&T> { fn start_bound(&self) -> Bound<&T> {
Unbounded Unbounded
} }