Skip to content

Commit bf7587f

Browse files
committed
iter: fix range_inclusive when start > stop
1 parent 1f961c7 commit bf7587f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/libstd/iter.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,17 +1790,17 @@ pub fn range_inclusive<A: Add<A, A> + Ord + Clone + One>(start: A, stop: A) -> R
17901790
RangeInclusive{range: range(start, stop), done: false}
17911791
}
17921792

1793-
impl<A: Add<A, A> + Ord + Clone> Iterator<A> for RangeInclusive<A> {
1793+
impl<A: Add<A, A> + Eq + Ord + Clone> Iterator<A> for RangeInclusive<A> {
17941794
#[inline]
17951795
fn next(&mut self) -> Option<A> {
17961796
match self.range.next() {
17971797
Some(x) => Some(x),
17981798
None => {
1799-
if self.done {
1800-
None
1801-
} else {
1799+
if !self.done && self.range.state == self.range.stop {
18021800
self.done = true;
18031801
Some(self.range.stop.clone())
1802+
} else {
1803+
None
18041804
}
18051805
}
18061806
}
@@ -2712,6 +2712,8 @@ mod tests {
27122712
fn test_range_inclusive() {
27132713
assert_eq!(range_inclusive(0i, 5).collect::<~[int]>(), ~[0i, 1, 2, 3, 4, 5]);
27142714
assert_eq!(range_inclusive(0i, 5).invert().collect::<~[int]>(), ~[5i, 4, 3, 2, 1, 0]);
2715+
assert_eq!(range_inclusive(200, -5).collect::<~[int]>(), ~[]);
2716+
assert_eq!(range_inclusive(200, 200).collect::<~[int]>(), ~[200]);
27152717
}
27162718

27172719
#[test]
@@ -2720,6 +2722,8 @@ mod tests {
27202722
assert_eq!(range_step(20i, 0, -5).collect::<~[int]>(), ~[20, 15, 10, 5]);
27212723
assert_eq!(range_step(20i, 0, -6).collect::<~[int]>(), ~[20, 14, 8, 2]);
27222724
assert_eq!(range_step(200u8, 255, 50).collect::<~[u8]>(), ~[200u8, 250]);
2725+
assert_eq!(range_step(200, -5, 1).collect::<~[int]>(), ~[]);
2726+
assert_eq!(range_step(200, 200, 1).collect::<~[int]>(), ~[]);
27232727
}
27242728

27252729
#[test]
@@ -2728,6 +2732,8 @@ mod tests {
27282732
assert_eq!(range_step_inclusive(20i, 0, -5).collect::<~[int]>(), ~[20, 15, 10, 5, 0]);
27292733
assert_eq!(range_step_inclusive(20i, 0, -6).collect::<~[int]>(), ~[20, 14, 8, 2]);
27302734
assert_eq!(range_step_inclusive(200u8, 255, 50).collect::<~[u8]>(), ~[200u8, 250]);
2735+
assert_eq!(range_step_inclusive(200, -5, 1).collect::<~[int]>(), ~[]);
2736+
assert_eq!(range_step_inclusive(200, 200, 1).collect::<~[int]>(), ~[200]);
27312737
}
27322738

27332739
#[test]

0 commit comments

Comments
 (0)