Repository navigation
Closed
0 / 10 of 1 issue completed
Copy link
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Description
Feature gate: #![feature(new_range_api)]
This is a tracking issue for the library additions that are part of RFC 3550: New range types
Tracking issue for the language change: #123741
This feature introduces new types implementing Copy and IntoIterator that are meant to replace the legacy range types, which are !Copy and implement Iterator directly.
Public API
// core::range
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Range<Idx> {
pub start: Idx,
pub end: Idx,
}
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct RangeInclusive<Idx> {
pub start: Idx,
pub last: Idx, // formerly end
}
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct RangeFrom<Idx> {
pub start: Idx,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct RangeToInclusive<Idx> {
pub last: Idx,
}
pub struct IterRange<Idx> { <private fields> }
pub struct IterRangeInclusive { <private fields> }
pub struct IterRangeFrom { <private fields> }
impl<Idx: Step> Iterator for IterRange<Idx> { ... }
impl<Idx: Step> Iterator for IterRangeInclusive<Idx> { ... }
impl<Idx: Step> Iterator for IterRangeFrom<Idx> { ... }
impl<Idx: Step> IntoIterator for Range<Idx> {
type IntoIter = IterRange<Idx>;
...
}
impl<Idx: Step> IntoIterator for RangeInclusive<Idx> {
type IntoIter = IterRangeInclusive<Idx>;
...
}
impl<Idx: Step> IntoIterator for RangeFrom<Idx> {
type IntoIter = IterRangeFrom<Idx>;
...
}See the RFC for more details.
Steps / History
- Implementation: Add
new_range_apifor RFC 3550 #125751 - Inclusive ranges: rename
endfield tolast - Optimize implementation
- Final comment period (FCP)1
- Stabilization PRs
Unresolved Questions
- Figure out a way to have
IterRangeFromoverflow panic only after yielding the maximum value, without impacting release mode Useoverflow_checksintrinsic soIterRangeFromyields MAX before panicking in debug #148703 - How can we / should we optimize
IterRangeInclusivenow that it is not bound by the API constraints of legacyRangeInclusive? - The set of inherent methods copied from
Iteratorpresent on the new range types (Decide which methods to add to newRangetypes #125589) - The exact module paths and type names
- Should the new types live at
std::ops::range::instead? IterRange,IterRangeInclusiveor justIter,IterInclusive? OrRangeIter,RangeInclusiveIter, ...? library: RenameIterRange*toRange*Iter#149547
- Should the new types live at
- Should other range-related items (like
RangeBounds) also be moved under therangemodule? - Should
RangeFromeven implementIntoIterator, or should it require an explicit.iter()call? Using it as an iterator can be a footgun, usually people wantstart..=MAXinstead. Also, it is inconsistent withRangeTo, which doesn't implementIntoIteratoreither. But, it's extremely useful forit.zip(1..) - 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(). - Should there be an infallible conversion from legacy to new
RangeInclusive?
impl<Idx> From<legacy::RangeInclusive<Idx>> for RangeInclusive<Idx> {
// How do we handle the `exhausted` case?
}Footnotes
Reacted by fwcd, M, Dmitry Pytaylo, musjj, Marcin Anforowicz, José Luis Cruz, Christian Schwarz, Vilim Lendvaj, Hennadii Chernyshchyk, Alix Bott and 11 more
Reacted by fwcd, Muhammad Mominul Huque, Petr Gladkikh, Alisa Sireneva, musjj, Marcin Anforowicz, Vilim Lendvaj, Michael Yang, runiq, teor and 3 more
Reacted by León Orell Valerian Liehr, musjj, Scott Olson and Andrew Farkas
Reacted by Andrew Farkas
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-langRelevant to the language teamT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Type
Projects
- StatusShow more project fieldsAccepted RFC
Activity
joshtriplett commentedon Jun 4, 2024
Capturing something from a verbal discussion:
In theory, since the new range types aren't iterators directly, and just get turned into iterators, we could automatically handle
for i in 10..0and do the expected thing. However, @eholk pointed out that while that'd be intuitive for integer literals, it'd be surprising for the generalm..nto implicitly do reverse iteration. So, we shouldn't do this.joshtriplett commentedon Jun 4, 2024
We discussed this in today's @rust-lang/libs-api meeting, and our conclusion was: the usefulness of
.zip(n..)is high enough that we should make this work. We should ensure that it yields values up to the maximum integer value, and then panics if asked for another value.Amanieu commentedon Jun 5, 2024
We discussed the unresolved questions in the libs-api meeting yesterday.
There was a lot of opposition to inherent methods, especially
mapandrev, because they would need to behave differently on a value type than on an iterator.mapwould be expected to work likeOption::mapand apply a function to the start and end bounds.revwould be expected to return a range (not an iterator).However there was some support for adding an
iterinherent method which would be a short-hand for.clone().into_iter(). We felt that the additional 5 characters ofinto_iter(especially the underscore) would make people's code too verbose for such a common operation.We were mostly happy with how things are currently set out in the PR.
It should implement
IntoIteratorsince, as mentioned, it is useful for things likeit.zip(1..).We also discussed the
RangeFromiterator: the implementation should add a hiddenboolso that any overflow panics happen after returning the maximum integer value, not before. To avoid issues with LLVM's loop optimizations, we may want thisboolto only be used in debug builds: in release builds it may acceptable to allow the iterator to wrap.This should be deferred for a future API proposal, only if it turns out there is a need for this.
Yes there should be. The case of converting an exhausted
RangeInclusiveiterator should be extremely rare in practice, so it's fine to panic in that case.pitaj commentedon Jun 10, 2024
I understand the arguments for purity here, but the immense utility these offer should be taken into account. I'll remove them from the PR, but I think the question should be reconsidered before the edition change is stabilized.
Arguments for `map` and `rev`
I think range types are in a very unique position, here.
mapandrevareWhile shaving five characters and the shift necessary for the underscore is helpful, the remaining seven characters and two shifts for
.iter()is still a burden and needless visual noise.My position is that there is no other meaning we could assign to
mapthat would be acceptable given the history of using it in this position.mapis expected to map the bounds, not the yielded elementsmapto mean "map the bounds" would be a sizeable hazard, because it could silently change the meaning offor (1..5).map(|n| n*2) { ... }across an edition boundary.map_boundsanyways.Rangeis not likeOptionorVec. It's not commonly used to hold values, it's purpose is to generate values.revis expected to return something likeRevRangeIMO this argument is stronger, because
for x in (1..5).rev()would still work. That covers one of the most common use-cases. However, I don't see much use for aRevRangetype.What would it implement besides
IntoIterator?RangeBoundsIndexis_emptyandcontainsbut those are direction-agnosticIt it only serves as an iterable, we may as well just use
Rev<IterRange<_>>anyways.Rollup merge of rust-lang#125751 - pitaj:new_range_api, r=jhpratt
Unrolled build for rust-lang#125751
scottmcm commentedon Aug 1, 2024
Hmm, so in release mode the iterator would have extra size that's not used? Seems like a shame, though I guess there's precedent with Fuse.
yara-blue commentedon Sep 16, 2024
I wish we had a
maptransforming the bounds of a Range. Though I get that we can not get it since it would get really confusing if the range types implementedIntoIterator.Therefore I really like the idea of a
map_boundsfunction. Re-creating a range simply to map the bounds takes a lot of space for a simple thing. That tents to get me out of the flow of reading any involved algorithm. For me it would be most usefull ifmap_boundsis added to the RangeBounds trait.Example use case:
I have log store that tracks logs from a sensor net. It uses a timeseries database. The database stores logs based on their time in seconds, the log store api wants jiff::Timestamp. When fetching a range I need to convert between those.
I use
RangeInclusiveeven thoughimpl RangeBoundswould be nicer however if I where to do the latter the code for converting the Range would become even longer.edit: added use case
balt-dev commentedon Sep 25, 2024
Thought: What's stopping anyone from making this an
enumoverRange,RangeFull,RangeInclusive, etc?sendittothenewts commentedon Dec 23, 2024
At present, the
endmember ofRangeInclusiveandRangeToInclusivehas the same name but slightly different semantics to theendmember ofRangeandRangeTo. This strikes me as a bit of a footgun, as a seemingly-innocuous change like replacing..=n-1with..ncan introduce off-by-one errors (see rust-lang/rust-clippy#3307 (comment)). I suggest renaming theendmember ofRangeInclusiveandRangeToInclusive, perhaps tolast, consistent with theIterRangeInclusive::lastmethod.pitaj commentedon Dec 23, 2024
It's a good idea but we probably don't want to break code using
RangeToInclusivejust for this.Open an ACP to discuss.
PetrGlad commentedon Dec 25, 2024
I am in favor of using any
IntoIteratortype infor, so I do not understand why@joshtriplett can you give an example where this is a problem?
I can imagine If a bound type prefers to iterate in an unusual way, it is responsibility of its author. I do not think that
forsyntax is a place that should gate-keep it...10..0 is empty, is this what you meant?
scottmcm commentedon Dec 25, 2024
It's absolutely correct that
m..nshould always only go forwards. We don't want to make the normal case pay for the extra check whether it needs to go backways -- if someone want to go backwards, they can pickRev<Range<T>>orEither<Range<T>, Rev<Range<T>>>depending which thing they actually want.Also, keeping
for i in m..nconsistent withmy_slice[m..n]is good, and we can't return a conditionally-reversed slice from that.87 remaining items
Rollup merge of rust-lang#154459 - tgross35:destabilize-range-iter-re…
Auto merge of #154459 - tgross35:destabilize-range-iter-remainder, r=…
Rollup merge of rust-lang#154459 - tgross35:destabilize-range-iter-re…
Rollup merge of rust-lang#154459 - tgross35:destabilize-range-iter-re…
Auto merge of #154459 - tgross35:destabilize-range-iter-remainder, r=…
Rollup merge of rust-lang#154620 - pitaj:stabilize-new_range_api, r=t…
Rollup merge of rust-lang#154620 - pitaj:stabilize-new_range_api, r=t…
Unrolled build for #154620
Rollup merge of rust-lang#156629 - tgross35:range-module-stabilizatio…
Rollup merge of rust-lang#156629 - tgross35:range-module-stabilizatio…
Unrolled build for #156629
Rollup merge of #128666 - pitaj:intrinsic-overflow_checks, r=BoxyUwU
Rollup merge of rust-lang#128666 - pitaj:intrinsic-overflow_checks, r…
Rollup merge of #128666 - pitaj:intrinsic-overflow_checks, r=BoxyUwU