Skip to content

Commit

Permalink
Stabilize Range[Inclusive]::is_empty
Browse files Browse the repository at this point in the history
I would like to propose these two simple methods for stabilization:
- Knowing that a range is exhaused isn't otherwise trivial
- Clippy would like to suggest them, but had to do extra work to disable that path <rust-lang/rust-clippy#3807> because they're unstable
- These work on `PartialOrd`, consistently with now-stable `contains`, and are thus more general than iterator-based approaches that need `Step`
- They've been unchanged for some time, and have picked up uses in the compiler
- Stabilizing them doesn't block any future iterator-based is_empty plans, as the inherent ones are preferred in name resolution
  • Loading branch information
scottmcm committed Aug 24, 2020
1 parent e6d85ea commit c20ad72
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 17 deletions.
14 changes: 2 additions & 12 deletions library/core/src/ops/range.rs
Expand Up @@ -125,8 +125,6 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// # Examples
///
/// ```
/// #![feature(range_is_empty)]
///
/// assert!(!(3..5).is_empty());
/// assert!( (3..3).is_empty());
/// assert!( (3..2).is_empty());
Expand All @@ -135,13 +133,11 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// The range is empty if either side is incomparable:
///
/// ```
/// #![feature(range_is_empty)]
///
/// assert!(!(3.0..5.0).is_empty());
/// assert!( (3.0..f32::NAN).is_empty());
/// assert!( (f32::NAN..5.0).is_empty());
/// ```
#[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
#[stable(feature = "range_is_empty", since = "1.47.0")]
pub fn is_empty(&self) -> bool {
!(self.start < self.end)
}
Expand Down Expand Up @@ -481,8 +477,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// # Examples
///
/// ```
/// #![feature(range_is_empty)]
///
/// assert!(!(3..=5).is_empty());
/// assert!(!(3..=3).is_empty());
/// assert!( (3..=2).is_empty());
Expand All @@ -491,8 +485,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// The range is empty if either side is incomparable:
///
/// ```
/// #![feature(range_is_empty)]
///
/// assert!(!(3.0..=5.0).is_empty());
/// assert!( (3.0..=f32::NAN).is_empty());
/// assert!( (f32::NAN..=5.0).is_empty());
Expand All @@ -501,14 +493,12 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// This method returns `true` after iteration has finished:
///
/// ```
/// #![feature(range_is_empty)]
///
/// let mut r = 3..=5;
/// for _ in r.by_ref() {}
/// // Precise field values are unspecified here
/// assert!(r.is_empty());
/// ```
#[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
#[stable(feature = "range_is_empty", since = "1.47.0")]
#[inline]
pub fn is_empty(&self) -> bool {
self.exhausted || !(self.start <= self.end)
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/lib.rs
Expand Up @@ -17,7 +17,6 @@
#![feature(try_find)]
#![feature(is_sorted)]
#![feature(pattern)]
#![feature(range_is_empty)]
#![feature(raw)]
#![feature(sort_internals)]
#![feature(slice_partition_at_index)]
Expand Down
1 change: 0 additions & 1 deletion src/librustc_infer/lib.rs
Expand Up @@ -22,7 +22,6 @@
#![feature(extend_one)]
#![feature(never_type)]
#![feature(or_patterns)]
#![feature(range_is_empty)]
#![feature(in_band_lifetimes)]
#![feature(crate_visibility_modifier)]
#![recursion_limit = "512"] // For rustdoc
Expand Down
1 change: 0 additions & 1 deletion src/librustc_middle/lib.rs
Expand Up @@ -40,7 +40,6 @@
#![feature(nll)]
#![feature(option_expect_none)]
#![feature(or_patterns)]
#![feature(range_is_empty)]
#![feature(min_specialization)]
#![feature(trusted_len)]
#![feature(stmt_expr_attributes)]
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/lib.rs
Expand Up @@ -22,7 +22,6 @@ Rust MIR: a lowered representation of Rust.
#![feature(try_blocks)]
#![feature(associated_type_bounds)]
#![feature(associated_type_defaults)]
#![feature(range_is_empty)]
#![feature(stmt_expr_attributes)]
#![feature(trait_alias)]
#![feature(option_expect_none)]
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/range_inclusive.rs
@@ -1,6 +1,5 @@
// run-pass
// Test inclusive range syntax.
#![feature(range_is_empty)]
#![allow(unused_braces)]
#![allow(unused_comparisons)]

Expand Down

0 comments on commit c20ad72

Please sign in to comment.