From f166bd9857dac3c66e812ba6bc33e59494c3fef2 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 23 Apr 2017 21:14:32 -0700 Subject: Make RangeInclusive just a two-field struct Not being an enum improves ergonomics, especially since NonEmpty could be Empty. It can still be iterable without an extra "done" bit by making the range have !(start <= end), which is even possible without changing the Step trait. Implements RFC 1980 --- src/libcore/slice/mod.rs | 67 +++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 40 deletions(-) (limited to 'src/libcore/slice') diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index e15eb8f2444..69b9e2a6288 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -981,95 +981,82 @@ impl SliceIndex<[T]> for ops::RangeInclusive { #[inline] fn get(self, slice: &[T]) -> Option<&[T]> { - match self { - ops::RangeInclusive::Empty { .. } => Some(&[]), - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => None, - ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).get(slice), - } + if self.end == usize::max_value() { None } + else { (self.start..self.end + 1).get(slice) } } #[inline] fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> { - match self { - ops::RangeInclusive::Empty { .. } => Some(&mut []), - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => None, - ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).get_mut(slice), - } + if self.end == usize::max_value() { None } + else { (self.start..self.end + 1).get_mut(slice) } } #[inline] unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { - match self { - ops::RangeInclusive::Empty { .. } => &[], - ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).get_unchecked(slice), - } + (self.start..self.end + 1).get_unchecked(slice) } #[inline] unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] { - match self { - ops::RangeInclusive::Empty { .. } => &mut [], - ops::RangeInclusive::NonEmpty { start, end } => { - (start..end + 1).get_unchecked_mut(slice) - } - } + (self.start..self.end + 1).get_unchecked_mut(slice) } #[inline] fn index(self, slice: &[T]) -> &[T] { - match self { - ops::RangeInclusive::Empty { .. } => &[], - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => { - panic!("attempted to index slice up to maximum usize"); - }, - ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).index(slice), - } + assert!(self.end != usize::max_value(), + "attempted to index slice up to maximum usize"); + (self.start..self.end + 1).index(slice) } #[inline] fn index_mut(self, slice: &mut [T]) -> &mut [T] { - match self { - ops::RangeInclusive::Empty { .. } => &mut [], - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => { - panic!("attempted to index slice up to maximum usize"); - }, - ops::RangeInclusive::NonEmpty { start, end } => (start..end + 1).index_mut(slice), - } + assert!(self.end != usize::max_value(), + "attempted to index slice up to maximum usize"); + (self.start..self.end + 1).index_mut(slice) } } +#[cfg(stage0)] // The bootstrap compiler has a different `...` desugar +fn inclusive(start: usize, end: usize) -> ops::RangeInclusive { + ops::RangeInclusive { start, end } +} +#[cfg(not(stage0))] +fn inclusive(start: usize, end: usize) -> ops::RangeInclusive { + start...end +} + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] impl SliceIndex<[T]> for ops::RangeToInclusive { type Output = [T]; #[inline] fn get(self, slice: &[T]) -> Option<&[T]> { - (0...self.end).get(slice) + inclusive(0, self.end).get(slice) } #[inline] fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> { - (0...self.end).get_mut(slice) + inclusive(0, self.end).get_mut(slice) } #[inline] unsafe fn get_unchecked(self, slice: &[T]) -> &[T] { - (0...self.end).get_unchecked(slice) + inclusive(0, self.end).get_unchecked(slice) } #[inline] unsafe fn get_unchecked_mut(self, slice: &mut [T]) -> &mut [T] { - (0...self.end).get_unchecked_mut(slice) + inclusive(0, self.end).get_unchecked_mut(slice) } #[inline] fn index(self, slice: &[T]) -> &[T] { - (0...self.end).index(slice) + inclusive(0, self.end).index(slice) } #[inline] fn index_mut(self, slice: &mut [T]) -> &mut [T] { - (0...self.end).index_mut(slice) + inclusive(0, self.end).index_mut(slice) } } -- cgit 1.4.1-3-g733a5