|
| 1 | +- Feature Name: inclusive_ranges |
| 2 | +- Start Date: (fill me in with today's date, YYYY-MM-DD) |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Change the Range struct to allow all combinations of inclusive/exclusive ranges. |
| 9 | + |
| 10 | +# Motivation |
| 11 | + |
| 12 | +Regardless of how the range syntax will eventually work, the underlying data |
| 13 | +structure will need to support both inclusive and exclusive ranges. If we don't |
| 14 | +do this now, libraries will define their own way of specifying ranges (as the |
| 15 | +rand crate and the BTreeMap collection have already done) and these custom |
| 16 | +implementations may become entrenched. |
| 17 | + |
| 18 | +# Detailed design |
| 19 | + |
| 20 | +The design is simply to: |
| 21 | + |
| 22 | + 1. Remove the `Unbounded` variant of the `Bound<Idx>` enum and move it to the |
| 23 | + ops module. |
| 24 | + 2. Change `Range` (and all variations there of) to use `Bound<Idx>` instead of |
| 25 | + `Idx` for start and end. |
| 26 | + |
| 27 | +```rust |
| 28 | +pub enum Bound<Idx> { |
| 29 | + Inclusive(Idx), |
| 30 | + Exclusive(Idx), |
| 31 | +} |
| 32 | +pub struct Range<Idx> { |
| 33 | + pub start: Bound<Idx>, |
| 34 | + pub end: Bound<Idx>, |
| 35 | +} |
| 36 | +pub struct RangeFrom<Idx> { |
| 37 | + pub start: Bound<Idx>, |
| 38 | +} |
| 39 | +pub struct RangeTo<Idx> { |
| 40 | + pub end: Bound<Idx>, |
| 41 | +} |
| 42 | +pub struct RangeFull; |
| 43 | +``` |
| 44 | + |
| 45 | +# Drawbacks |
| 46 | + |
| 47 | +* The Range struct becomes larger. |
| 48 | +* When checking the bounds, you have to check if they are inclusive/exclusive. |
| 49 | + |
| 50 | +# Alternatives |
| 51 | + |
| 52 | +One obvious alternative is the following: |
| 53 | + |
| 54 | +```rust |
| 55 | +pub enum Bound<Idx> { |
| 56 | + Inclusive(Idx), |
| 57 | + Exclusive(Idx), |
| 58 | + Unbounded, |
| 59 | +} |
| 60 | +pub struct Range<Idx> { |
| 61 | + pub start: Bound<Idx>, |
| 62 | + pub end: Bound<Idx>, |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +However, this would make it impossible to do things like only allowing full |
| 67 | +ranges (see slicing OsString). |
| 68 | + |
| 69 | +# Unresolved questions |
| 70 | + |
| 71 | +We might want some way to extract inclusive bounds from any *integral* range to |
| 72 | +make bounds checking easier. |
0 commit comments