diff options
| author | Mark Simulacrum <mark.simulacrum@gmail.com> | 2017-05-24 19:50:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-24 19:50:01 -0600 |
| commit | 00c87a6486428b072199809b051beea1124f616f (patch) | |
| tree | bf99a9d33045f51496702bbf61788d8e4adf6b68 /src/libcore/str | |
| parent | 989c8e86e16ddfa146cd3ba2557d9becb2841a89 (diff) | |
| parent | 7eaca60f3b3b783ffa1e80ccf91e820f9436b3a3 (diff) | |
Rollup merge of #42134 - scottmcm:rangeinclusive-struct, r=aturon
Make RangeInclusive just a two-field struct Not being an enum improves ergonomics and consistency, especially since NonEmpty variant wasn't prevented from being 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 merged https://github.com/rust-lang/rfcs/pull/1980; tracking issue https://github.com/rust-lang/rust/issues/28237. This is definitely a breaking change to anything consuming `RangeInclusive` directly (not as an Iterator) or constructing it without using the sugar. Is there some change that would make sense before this so compilation failures could be compatibly fixed ahead of time? r? @aturon (as FCP proposer on the RFC)
Diffstat (limited to 'src/libcore/str')
| -rw-r--r-- | src/libcore/str/mod.rs | 59 |
1 files changed, 19 insertions, 40 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 7fb941c091f..547a4899c71 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -1724,15 +1724,12 @@ mod traits { #[inline] fn index(&self, index: ops::RangeInclusive<usize>) -> &str { - match index { - 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 } => - self.index(start .. end+1) - } + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index(index.start .. index.end+1) } } + #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] @@ -1741,7 +1738,9 @@ mod traits { #[inline] fn index(&self, index: ops::RangeToInclusive<usize>) -> &str { - self.index(0...index.end) + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index(.. index.end+1) } } @@ -1751,13 +1750,9 @@ mod traits { impl ops::IndexMut<ops::RangeInclusive<usize>> for str { #[inline] fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str { - match index { - ops::RangeInclusive::Empty { .. } => &mut self[0..0], // `&mut ""` doesn't work - ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() => - panic!("attempted to index str up to maximum usize"), - ops::RangeInclusive::NonEmpty { start, end } => - self.index_mut(start .. end+1) - } + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index_mut(index.start .. index.end+1) } } #[unstable(feature = "inclusive_range", @@ -1766,7 +1761,9 @@ mod traits { impl ops::IndexMut<ops::RangeToInclusive<usize>> for str { #[inline] fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str { - self.index_mut(0...index.end) + assert!(index.end != usize::max_value(), + "attempted to index str up to maximum usize"); + self.index_mut(.. index.end+1) } } @@ -1948,45 +1945,27 @@ mod traits { type Output = str; #[inline] fn get(self, slice: &str) -> Option<&Self::Output> { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.get(slice) + (self.start..self.end+1).get(slice) } #[inline] fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.get_mut(slice) + (self.start..self.end+1).get_mut(slice) } #[inline] unsafe fn get_unchecked(self, slice: &str) -> &Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - 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 str) -> &mut Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - 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: &str) -> &Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.index(slice) + (self.start..self.end+1).index(slice) } #[inline] fn index_mut(self, slice: &mut str) -> &mut Self::Output { - match self { - ops::RangeInclusive::Empty { .. } => 0..0, - ops::RangeInclusive::NonEmpty { start, end } => start..end+1, - }.index_mut(slice) + (self.start..self.end+1).index_mut(slice) } } |
