Skip to content

Commit a7d5d94

Browse files
committed
better handling of iterator edge cases
1 parent f9918e7 commit a7d5d94

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

capnp/src/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ impl <U, T : IndexMove<u32, U>> ::std::iter::Iterator for ListIter<T, U> {
137137
}
138138

139139
fn nth(&mut self, p: usize) -> Option<U>{
140-
if p < self.size as usize {
140+
if self.index + (p as u32) < self.size {
141141
self.index += p as u32;
142142
let result = self.list.index_move(self.index);
143143
self.index += 1;
144144
Some(result)
145145
} else {
146+
self.index = self.size;
146147
None
147148
}
148149
}

capnpc/test/test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,5 +1461,16 @@ mod tests {
14611461
assert_eq!(c, s.get_u_int32_field());
14621462
c += 1;
14631463
}
1464+
1465+
{
1466+
let mut overflow_iter = structs.iter();
1467+
assert!(overflow_iter.nth(4).is_some());
1468+
1469+
// The first four elements have been consumed, so going another 4 should overflow.
1470+
assert!(overflow_iter.nth(4).is_none());
1471+
1472+
// The previous call pushed us to the end, even though it returned None.
1473+
assert!(overflow_iter.next().is_none());
1474+
}
14641475
}
14651476
}

0 commit comments

Comments
 (0)