Auto merge of #30884 - durka:inclusive-ranges, r=aturon
This PR implements [RFC 1192](https://github.com/rust-lang/rfcs/blob/master/text/1192-inclusive-ranges.md), which is triple-dot syntax for inclusive range expressions. The new stuff is behind two feature gates (one for the syntax and one for the std::ops types). This replaces the deprecated functionality in std::iter. Along the way I simplified the desugaring for all ranges. This is my first contribution to rust which changes more than one character outside of a test or comment, so please review carefully! Some of the individual commit messages have more of my notes. Also thanks for putting up with my dumb questions in #rust-internals. - For implementing `std::ops::RangeInclusive`, I took @Stebalien's suggestion from https://github.com/rust-lang/rfcs/pull/1192#issuecomment-137864421. It seemed to me to make the implementation easier and increase type safety. If that stands, the RFC should be amended to avoid confusion. - I also kind of like @glaebhoerl's [idea](https://github.com/rust-lang/rfcs/pull/1254#issuecomment-147815299), which is unified inclusive/exclusive range syntax something like `x>..=y`. We can experiment with this while everything is behind a feature gate. - There are a couple of FIXMEs left (see the last commit). I didn't know what to do about `RangeArgument` and I haven't added `Index` impls yet. Those should be discussed/finished before merging. cc @Gankro since you [complained](https://www.reddit.com/r/rust/comments/3xkfro/what_happened_to_inclusive_ranges/cy5j0yq) cc #27777 #30877 rust-lang/rust#1192 rust-lang/rfcs#1254 relevant to #28237 (tracking issue)
This commit is contained in:
commit
8484831d29
46 changed files with 983 additions and 390 deletions
|
@ -1460,6 +1460,62 @@ mod traits {
|
|||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inclusive_range",
|
||||
reason = "recently added, follows RFC",
|
||||
issue = "28237")]
|
||||
impl ops::Index<ops::RangeInclusive<usize>> for str {
|
||||
type Output = str;
|
||||
|
||||
#[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)
|
||||
}
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "inclusive_range",
|
||||
reason = "recently added, follows RFC",
|
||||
issue = "28237")]
|
||||
impl ops::Index<ops::RangeToInclusive<usize>> for str {
|
||||
type Output = str;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
|
||||
// SNAP 4d3eebf change this to `0...index.end`
|
||||
self.index(ops::RangeInclusive::NonEmpty { start: 0, end: index.end })
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inclusive_range",
|
||||
reason = "recently added, follows RFC",
|
||||
issue = "28237")]
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "inclusive_range",
|
||||
reason = "recently added, follows RFC",
|
||||
issue = "28237")]
|
||||
impl ops::IndexMut<ops::RangeToInclusive<usize>> for str {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
|
||||
// SNAP 4d3eebf change this to `0...index.end`
|
||||
self.index_mut(ops::RangeInclusive::NonEmpty { start: 0, end: index.end })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Methods for string slices
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue