- Feature Name: inclusive_ranges
- Start Date: (fill me in with today's date, YYYY-MM-DD)
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)
Change the Range struct to allow all combinations of inclusive/exclusive ranges.
Regardless of how the range syntax will eventually work, the underlying data structure will need to support both inclusive and exclusive ranges. If we don't do this now, libraries will define their own way of specifying ranges (as the rand crate and the BTreeMap collection have already done) and these custom implementations may become entrenched.
The design is simply to:
- Remove the
Unbounded
variant of theBound<Idx>
enum and move it to the ops module. - Change
Range
(and all variations there of) to useBound<Idx>
instead ofIdx
for start and end.
pub enum Bound<Idx> {
Inclusive(Idx),
Exclusive(Idx),
}
pub struct Range<Idx> {
pub start: Bound<Idx>,
pub end: Bound<Idx>,
}
pub struct RangeFrom<Idx> {
pub start: Bound<Idx>,
}
pub struct RangeTo<Idx> {
pub end: Bound<Idx>,
}
pub struct RangeFull;
- The Range struct becomes larger.
- When checking the bounds, you have to check if they are inclusive/exclusive.
One obvious alternative is the following:
pub enum Bound<Idx> {
Inclusive(Idx),
Exclusive(Idx),
Unbounded,
}
pub struct Range<Idx> {
pub start: Bound<Idx>,
pub end: Bound<Idx>,
}
However, this would make it impossible to do things like only allowing full ranges (see slicing OsString).
We might want some way to extract inclusive bounds from any integral range to make bounds checking easier.