Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/core/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<ops::RangeFrom<usize>> {
}

#[unstable(feature = "sliceindex_wrappers", issue = "146179")]
unsafe impl<T> SliceIndex<[T]> for Clamp<range::RangeTo<usize>> {
unsafe impl<T> SliceIndex<[T]> for Clamp<ops::RangeTo<usize>> {
type Output = [T];

fn get(self, slice: &[T]) -> Option<&Self::Output> {
Expand Down Expand Up @@ -408,7 +408,7 @@ unsafe impl<T> SliceIndex<[T]> for Clamp<ops::RangeToInclusive<usize>> {
}

#[unstable(feature = "sliceindex_wrappers", issue = "146179")]
unsafe impl<T> SliceIndex<[T]> for Clamp<range::RangeFull> {
unsafe impl<T> SliceIndex<[T]> for Clamp<ops::RangeFull> {
type Output = [T];

fn get(self, slice: &[T]) -> Option<&Self::Output> {
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub mod pat;
pub mod pin;
#[unstable(feature = "random", issue = "130703")]
pub mod random;
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
pub mod range;
pub mod result;
pub mod sync;
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/random.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Random value generation.

use crate::range::RangeFull;
use crate::ops::RangeFull;

/// A source of randomness.
#[unstable(feature = "random", issue = "130703")]
Expand Down
58 changes: 33 additions & 25 deletions library/core/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,26 @@ mod iter;
#[unstable(feature = "new_range_api", issue = "125687")]
pub mod legacy;

use Bound::{Excluded, Included, Unbounded};
#[doc(inline)]
pub use iter::{RangeFromIter, RangeInclusiveIter, RangeIter};

#[doc(inline)]
pub use crate::iter::Step;
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
pub use iter::RangeInclusiveIter;
#[doc(inline)]
pub use crate::ops::{Bound, IntoBounds, OneSidedRange, RangeBounds, RangeFull, RangeTo};
#[unstable(feature = "new_range_api", issue = "125687")]
pub use iter::{RangeFromIter, RangeIter};

// FIXME(#125687): re-exports temporarily removed
// Because re-exports of stable items (Bound, RangeBounds, RangeFull, RangeTo)
// can't be made unstable.
//
// #[doc(inline)]
// #[unstable(feature = "new_range_api", issue = "125687")]
// pub use crate::iter::Step;
// #[doc(inline)]
// #[unstable(feature = "new_range_api", issue = "125687")]
// pub use crate::ops::{Bound, IntoBounds, OneSidedRange, RangeBounds, RangeFull, RangeTo};
use crate::iter::Step;
use crate::ops::Bound::{self, Excluded, Included, Unbounded};
use crate::ops::{IntoBounds, RangeBounds};

/// A (half-open) range bounded inclusively below and exclusively above
/// (`start..end` in a future edition).
Expand Down Expand Up @@ -226,25 +238,24 @@ impl<T> const From<legacy::Range<T>> for Range<T> {
/// The `start..=last` syntax is a `RangeInclusive`:
///
/// ```
/// #![feature(new_range_api)]
/// use core::range::RangeInclusive;
///
/// assert_eq!(RangeInclusive::from(3..=5), RangeInclusive { start: 3, last: 5 });
/// assert_eq!(3 + 4 + 5, RangeInclusive::from(3..=5).into_iter().sum());
/// ```
#[lang = "RangeInclusiveCopy"]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
pub struct RangeInclusive<Idx> {
/// The lower bound of the range (inclusive).
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
pub start: Idx,
/// The upper bound of the range (inclusive).
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
pub last: Idx,
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.start.fmt(fmt)?;
Expand All @@ -260,7 +271,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// # Examples
///
/// ```
/// #![feature(new_range_api)]
/// use core::range::RangeInclusive;
///
/// assert!(!RangeInclusive::from(3..=5).contains(&2));
Expand All @@ -278,7 +288,7 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// assert!(!RangeInclusive::from(f32::NAN..=1.0).contains(&1.0));
/// ```
#[inline]
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_range", issue = "none")]
pub const fn contains<U>(&self, item: &U) -> bool
where
Expand All @@ -293,7 +303,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// # Examples
///
/// ```
/// #![feature(new_range_api)]
/// use core::range::RangeInclusive;
///
/// assert!(!RangeInclusive::from(3..=5).is_empty());
Expand All @@ -304,14 +313,13 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// The range is empty if either side is incomparable:
///
/// ```
/// #![feature(new_range_api)]
/// use core::range::RangeInclusive;
///
/// assert!(!RangeInclusive::from(3.0..=5.0).is_empty());
/// assert!( RangeInclusive::from(3.0..=f32::NAN).is_empty());
/// assert!( RangeInclusive::from(f32::NAN..=5.0).is_empty());
/// ```
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[rustc_const_unstable(feature = "const_range", issue = "none")]
pub const fn is_empty(&self) -> bool
Expand All @@ -330,15 +338,14 @@ impl<Idx: Step> RangeInclusive<Idx> {
/// # Examples
///
/// ```
/// #![feature(new_range_api)]
/// use core::range::RangeInclusive;
///
/// let mut i = RangeInclusive::from(3..=8).iter().map(|n| n*n);
/// assert_eq!(i.next(), Some(9));
/// assert_eq!(i.next(), Some(16));
/// assert_eq!(i.next(), Some(25));
/// ```
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub fn iter(&self) -> RangeInclusiveIter<Idx> {
self.clone().into_iter()
Expand All @@ -354,7 +361,7 @@ impl RangeInclusive<usize> {
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_range", issue = "none")]
impl<T> const RangeBounds<T> for RangeInclusive<T> {
fn start_bound(&self) -> Bound<&T> {
Expand All @@ -371,7 +378,7 @@ impl<T> const RangeBounds<T> for RangeInclusive<T> {
/// If you need to use this implementation where `T` is unsized,
/// consider using the `RangeBounds` impl for a 2-tuple of [`Bound<&T>`][Bound],
/// i.e. replace `start..=end` with `(Bound::Included(start), Bound::Included(end))`.
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_range", issue = "none")]
impl<T> const RangeBounds<T> for RangeInclusive<&T> {
fn start_bound(&self) -> Bound<&T> {
Expand All @@ -382,24 +389,24 @@ impl<T> const RangeBounds<T> for RangeInclusive<&T> {
}
}

// #[unstable(feature = "range_into_bounds", issue = "136903")]
#[unstable(feature = "new_range_api", issue = "125687")]
// #[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[unstable(feature = "range_into_bounds", issue = "136903")]
#[rustc_const_unstable(feature = "const_range", issue = "none")]
impl<T> const IntoBounds<T> for RangeInclusive<T> {
fn into_bounds(self) -> (Bound<T>, Bound<T>) {
(Included(self.start), Included(self.last))
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T> const From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
#[inline]
fn from(value: RangeInclusive<T>) -> Self {
Self::new(value.start, value.last)
}
}
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T> const From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
#[inline]
Expand Down Expand Up @@ -650,12 +657,13 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<legacy::RangeToInclusive<T>> for RangeToInclusive<T> {
fn from(value: legacy::RangeToInclusive<T>) -> Self {
Self { last: value.end }
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<RangeToInclusive<T>> for legacy::RangeToInclusive<T> {
fn from(value: RangeToInclusive<T>) -> Self {
Self { end: value.last }
Expand Down
15 changes: 9 additions & 6 deletions library/core/src/range/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{intrinsics, mem};
pub struct RangeIter<A>(legacy::Range<A>);

impl<A> RangeIter<A> {
#[unstable(feature = "new_range_api", issue = "125687")]
/// Returns the remainder of the range being iterated over.
pub fn remainder(self) -> Range<A> {
Range { start: self.0.start, end: self.0.end }
Expand Down Expand Up @@ -152,14 +153,15 @@ impl<A: Step> IntoIterator for Range<A> {
}

/// By-value [`RangeInclusive`] iterator.
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[derive(Debug, Clone)]
pub struct RangeInclusiveIter<A>(legacy::RangeInclusive<A>);

impl<A: Step> RangeInclusiveIter<A> {
/// Returns the remainder of the range being iterated over.
///
/// If the iterator is exhausted or empty, returns `None`.
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
pub fn remainder(self) -> Option<RangeInclusive<A>> {
if self.0.is_empty() {
return None;
Expand All @@ -169,7 +171,7 @@ impl<A: Step> RangeInclusiveIter<A> {
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl<A: Step> Iterator for RangeInclusiveIter<A> {
type Item = A;

Expand Down Expand Up @@ -225,7 +227,7 @@ impl<A: Step> Iterator for RangeInclusiveIter<A> {
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl<A: Step> DoubleEndedIterator for RangeInclusiveIter<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
Expand All @@ -246,10 +248,10 @@ impl<A: Step> DoubleEndedIterator for RangeInclusiveIter<A> {
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<A: TrustedStep> TrustedLen for RangeInclusiveIter<A> {}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl<A: Step> FusedIterator for RangeInclusiveIter<A> {}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl<A: Step> IntoIterator for RangeInclusive<A> {
type Item = A;
type IntoIter = RangeInclusiveIter<A>;
Expand All @@ -276,7 +278,7 @@ macro_rules! range_exact_iter_impl {

macro_rules! range_incl_exact_iter_impl {
($($t:ty)*) => ($(
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl ExactSizeIterator for RangeInclusiveIter<$t> { }
)*)
}
Expand Down Expand Up @@ -305,6 +307,7 @@ impl<A: Step> RangeFromIter<A> {
/// Returns the remainder of the range being iterated over.
#[inline]
#[rustc_inherit_overflow_checks]
#[unstable(feature = "new_range_api", issue = "125687")]
pub fn remainder(self) -> RangeFrom<A> {
if intrinsics::overflow_checks() {
if !self.first {
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/slice/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod private_slice_index {

#[unstable(feature = "new_range_api", issue = "125687")]
impl Sealed for range::Range<usize> {}
#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
impl Sealed for range::RangeInclusive<usize> {}
#[unstable(feature = "new_range_api", issue = "125687")]
impl Sealed for range::RangeToInclusive<usize> {}
Expand Down Expand Up @@ -724,7 +724,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
unsafe impl<T> const SliceIndex<[T]> for range::RangeInclusive<usize> {
type Output = [T];
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/str/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ unsafe impl const SliceIndex<str> for ops::RangeInclusive<usize> {
}
}

#[unstable(feature = "new_range_api", issue = "125687")]
#[stable(feature = "new_range_inclusive_api", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
unsafe impl const SliceIndex<str> for range::RangeInclusive<usize> {
type Output = str;
Expand Down
9 changes: 5 additions & 4 deletions tests/ui/new-range/disabled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

fn main() {
// Unchanged
let a: core::range::RangeFull = ..;
let b: core::range::RangeTo<u8> = ..2;
let a: core::ops::RangeFull = ..;
let b: core::ops::RangeTo<u8> = ..2;

let _: core::ops::RangeFull = a;
let _: core::ops::RangeTo<u8> = b;
// FIXME(#125687): re-exports temporarily removed
// let _: core::range::RangeFull = a;
// let _: core::range::RangeTo<u8> = b;

// Changed
let a: core::range::legacy::RangeFrom<u8> = 1..;
Expand Down
9 changes: 5 additions & 4 deletions tests/ui/new-range/enabled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

fn main() {
// Unchanged
let a: core::range::RangeFull = ..;
let b: core::range::RangeTo<u8> = ..2;
let a: core::ops::RangeFull = ..;
let b: core::ops::RangeTo<u8> = ..2;

let _: core::ops::RangeFull = a;
let _: core::ops::RangeTo<u8> = b;
// FIXME(#125687): re-exports temporarily removed
// let _: core::range::RangeFull = a;
// let _: core::range::RangeTo<u8> = b;

// Changed
let a: core::range::RangeFrom<u8> = 1..;
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/range/new_range_stability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Stable

use std::range::{RangeInclusive, RangeInclusiveIter};

fn range_inclusive(mut r: RangeInclusive<usize>) {
r.start;
r.last;
r.contains(&5);
r.is_empty();
r.iter();

let mut i = r.into_iter();
i.next();
i.remainder();
}

// Unstable module

use std::range::legacy; //~ ERROR unstable

// Unstable types

use std::range::RangeFrom; //~ ERROR unstable
use std::range::Range; //~ ERROR unstable
use std::range::RangeFromIter; //~ ERROR unstable
use std::range::RangeIter; //~ ERROR unstable

fn main() {}
Loading
Loading