You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some libraries have range types in their public interface. To use the new range types with such a library, users will need to add explicit conversions.
136
151
137
-
To reduce the need for the above conversions, we recommend making the following changes to libraries:
152
+
To reduce the burden of explicit conversions, libraries should make the following backwards-compatible changes:
138
153
139
154
- Change any function parameters from legacy `Range*` types to `impl Into<Range*>`
140
155
Or if applicable, `impl RangeBounds<_>`
@@ -181,25 +196,7 @@ where Range<T>: IntoIterator
181
196
}
182
197
```
183
198
184
-
- When the range is treated as mutable iterator, call `.into_iter()` before using it
185
-
186
-
```rust
187
-
// Before
188
-
letmutrange=0..5;
189
-
assert_eq!(range.next(), Some(0));
190
-
range.for_each(|n| {
191
-
// n = 1, 2, 3, 4
192
-
});
193
-
194
-
// After
195
-
letmutrange= (0..5).into_iter();
196
-
assert_eq!(range.next(), Some(0));
197
-
range.for_each(|n| {
198
-
// n = 1, 2, 3, 4
199
-
});
200
-
```
201
-
202
-
- When ranges are used with `std::ops::Index`, add impls for the new range types
199
+
- When your library implements a trait involving ranges, such as `std::ops::Index`, add impls for the new range types
203
200
204
201
```rust
205
202
// Before
@@ -212,6 +209,10 @@ impl Index<Range<usize>> for Bar { ... }
- These changes to libraries should happen when _users_ of a given library transition to the new edition
214
+
- These changes do not require the library itself to transition to the new edition
215
+
215
216
## Diagnostics
216
217
217
218
There is a substantial amount of educational material in the wild which assumes the the range types implement `Iterator`. If a user references this outdated material, it is important that compiler errors guide them to the new solution.
@@ -415,7 +416,7 @@ impl<Idx> Range<Idx> {
415
416
416
417
This change has the potential to cause a significant amount of churn in the ecosystem. There are two main sources of churn:
417
418
- where ranges are assumed to be `Iterator`
418
-
-when `Index<legacy::Range<_>>` is implemented directly
419
+
-trait impls involving ranges, such as `Index<legacy::Range<_>>`
419
420
420
421
Changes will be required to support the new range types, even on older editions. See the [migrating section](#migrating) for specifics.
421
422
@@ -535,7 +536,7 @@ We leave the following items to be decided by the **libs-api** team after this p
535
536
- The set of inherent methods copied from `Iterator` present on the new range types
536
537
- The exact module paths and type names
537
538
+ Should the new types live at `std::ops::range::` instead?
538
-
+`IterRange`, `IterRangeInclusive` or just `Iter`, `IterInclusive`?
539
+
+`IterRange`, `IterRangeInclusive` or just `Iter`, `IterInclusive`? Or `RangeIter`, `RangeInclusiveIter`, ...?
539
540
- Should other range-related items (like `RangeBounds`) also be moved under the `range` module?
540
541
- Should `RangeFrom` even implement `IntoIterator`, or should it require an explicit `.iter()` call? Using it as an iterator [can be a footgun](https://github.com/rust-lang/libs-team/issues/304), usually people want `start..=MAX` instead. Also, it is inconsistent with `RangeTo`, which doesn't implement `IntoIterator` either.
541
542
- Should there be a way to get an iterator that modifies the range in place, rather than taking the range by value? That would allow things like `range.by_ref().next()`.
@@ -549,7 +550,7 @@ impl<Idx> From<legacy::RangeInclusive<Idx>> for RangeInclusive<Idx> {
549
550
# Future possibilities
550
551
[future-possibilities]: #future-possibilities
551
552
552
-
- Hide or deprecate range-related items directly under `ops`, without breaking existing links or triggering deprecation warnings on previous editions.
553
+
- Hide or deprecate range-related items directly under `ops` (without breaking existing links or triggering deprecation warnings on previous editions).
553
554
-`RangeTo(Inclusive)::rev()` that returns an iterator?
554
555
-`IterRangeInclusive` can be optimized to take advantage of the case where the bounds don't occupy the full domain of the index type:
0 commit comments