Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement range syntax #19858
Conversation
rust-highfive
assigned
aturon
Dec 15, 2014
This comment has been minimized.
This comment has been minimized.
|
Yay! Delighted to see this, @nick29581 |
sfackler
reviewed
Dec 15, 2014
| #[lang="full_range"] | ||
| pub struct FullRange; | ||
|
|
||
| /// A range which i bounded at both ends. |
This comment has been minimized.
This comment has been minimized.
sfackler
reviewed
Dec 15, 2014
|
|
||
| return None; | ||
| } | ||
| } |
This comment has been minimized.
This comment has been minimized.
sfackler
reviewed
Dec 15, 2014
| impl<Idx: Clone + Countable> Iterator<Idx> for RangeFrom<Idx> { | ||
| #[inline] | ||
| fn next(&mut self) -> Option<Idx> { | ||
| // Deliberately overflow so we loop forever. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
sfackler
Dec 15, 2014
Member
Now that I think about it, does it even make sense for this to implement Iterator?
This comment has been minimized.
This comment has been minimized.
nrc
Dec 15, 2014
Author
Member
I had a bit of a discussion about this on IRC and the consensus was yes it makes sense (although basically only to use take on) and that looping forever was probably what you would expect, rather than stopping at the max int (it's also more efficient).
This comment has been minimized.
This comment has been minimized.
aturon
Dec 16, 2014
Member
Note, we've long had this iterator: http://static.rust-lang.org/doc/master/std/iter/fn.count.html
This notation will just serve to make it a bit more convenient. I'm hoping that we can drop many of the bare fns in std::iter in favor of this notation.
huonw
reviewed
Dec 15, 2014
| #[inline] | ||
| fn decrement(&mut self) { *self -= 1; } | ||
| #[inline] | ||
| fn difference(a: &$t, b: &$t) -> uint { (*a - *b) as uint } |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
nrc
Dec 15, 2014
Author
Member
I guess T should be u64 for u64/i64 and we require T=uint for to implement iterator. We need the snapshot for that, but hopefully it can happen before this lands
huonw
reviewed
Dec 15, 2014
| /// The difference between two countable objects. | ||
| /// Temporarily a uint, should be an associated type, but | ||
| // FIXME(#19391) needs a snapshot | ||
| fn difference(a: &Self, b: &Self) -> uint; |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
nrc
Dec 15, 2014
Author
Member
Urgh, yes, kinda, the problem is that then it can overflow. I think I should just comment better that this is the magnitude of the difference and that we assume a > b (I could debug_assert that)
huonw
reviewed
Dec 15, 2014
| } | ||
| } | ||
|
|
||
| impl<Idx: Copy> Copy for Range<Idx> {} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
huonw
reviewed
Dec 15, 2014
| #[test] | ||
| fn test_range() { | ||
| let r = Range { start: 2u, end: 10 }; | ||
| for (i, ri) in r.enumerate() { |
This comment has been minimized.
This comment has been minimized.
huonw
Dec 15, 2014
Member
This test doesn't detect the iteration being shorter than expected (or not occur at all), could be something like
let mut r = Range { ... };
let mut i = 0;
for ri in r {
assert!(...);
assert!(...);
i += 1;
}
assert_eq!(i, 8);
This comment has been minimized.
This comment has been minimized.
|
@nick29581 left some comments -- though possibly on a stale commit? In any case, I'm basically fine with landing this as-is in terms of naming and placement of the |
This comment has been minimized.
This comment has been minimized.
|
@aturon I like the |
This comment has been minimized.
This comment has been minimized.
|
@nick29581 What about making it return |
This comment has been minimized.
This comment has been minimized.
|
@aturon it is for now, yes (if the trait is meant to be more generally useful, then I guess it should handle more here, but perhaps we should ere on the side of YAGNI here.). Does size_hint need to be fast? I.e., is it worth trying to avoid the branch? If not, then Option seems fine for now. |
nrc
assigned
nikomatsakis
and unassigned
aturon
Dec 15, 2014
This comment has been minimized.
This comment has been minimized.
|
Usually I also agree that YAGNI seems best here. We recently axed a lot of the On Mon, Dec 15, 2014 at 3:48 PM, Nick Cameron notifications@github.com
|
This comment has been minimized.
This comment has been minimized.
|
Is something like the following in this PR? Just a thought I had a few minutes ago.
|
This comment has been minimized.
This comment has been minimized.
|
@sinistersnare yes, that will work, you don't even need the brackets around the range |
This comment has been minimized.
This comment has been minimized.
|
Exciting. |
This comment has been minimized.
This comment has been minimized.
|
So this looks reasonable, though I'm wondering if we can just desugar these expressions in the front-end and save ourselves a lot of weird bugs down the line. This seems like a classic case. |
This comment has been minimized.
This comment has been minimized.
|
r+ modulo comments and in particular the question about using a common supertype, though perhaps we can discuss whether to desugar etc on IRC |
nrc
force-pushed the
nrc:ranges
branch
from
97a4ffd
to
ef4a1f4
Dec 18, 2014
bors
added a commit
that referenced
this pull request
Dec 18, 2014
quantheory
reviewed
Dec 20, 2014
| /// Change self to the previous object. | ||
| fn step_back(&mut self); | ||
| /// The steps_between two step objects. | ||
| /// a should always be less than b, so the result should never be negtive. |
This comment has been minimized.
This comment has been minimized.
quantheory
reviewed
Dec 20, 2014
| let _ = 0u..4+4-3; | ||
| let _ = 0..foo(); | ||
|
|
||
| // Test we can use tow different types with a common supertype. |
This comment has been minimized.
This comment has been minimized.
nrc
force-pushed the
nrc:ranges
branch
from
ef4a1f4
to
4d5d24d
Dec 23, 2014
bors
added a commit
that referenced
this pull request
Dec 23, 2014
nrc
added some commits
Dec 13, 2014
nrc
added some commits
Dec 13, 2014
nrc
force-pushed the
nrc:ranges
branch
from
4d5d24d
to
e82215d
Dec 23, 2014
This comment has been minimized.
This comment has been minimized.
|
r=aturon,nikomatsakis |
This comment has been minimized.
This comment has been minimized.
|
@bors retry |
This comment has been minimized.
This comment has been minimized.
|
saw approval from aturon |
This comment has been minimized.
This comment has been minimized.
|
merging nick29581/rust/ranges = e82215d into auto |
This comment has been minimized.
This comment has been minimized.
|
status: {"merge_sha": "0e4d1a02deb0e4d4c3c36516713f2d97b16d5f00"} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
status: {"merge_sha": "e64a8193b02ce72ef183274994a25eae281cb89c"} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
fast-forwarding master to auto = e64a819 |
This comment has been minimized.
This comment has been minimized.
|
fast-forwarding master to auto = e64a819 |
nrc commentedDec 15, 2014
Closes #19794
r? @aturon for the first patch
r? @nikomatsakis for the rest