Repository: capnproto/capnproto-rust (language: rust)

Issue:
traits.ListIter incorrectly implementing ExactSizeIterator
The documentation for ExactSizeIterator.len() states:
> Returns the exact remaining length of the iterator.

https://docs.rs/capnp/0.19.5/capnp/traits/struct.ListIter.html#impl-ExactSizeIterator-for-ListIter%3CT,+U%3E

It looks like the implementation returns the total length of the iterator not the length remaining.


Discussion / hints:

Fix patch:
diff --git a/capnp/src/traits.rs b/capnp/src/traits.rs
index 41fa755ec..c84c2081c 100644
--- a/capnp/src/traits.rs
+++ b/capnp/src/traits.rs
@@ -169,7 +169,7 @@ impl<U, T: IndexMove<u32, U>> ::core::iter::Iterator for ListIter<T, U> {
 
 impl<U, T: IndexMove<u32, U>> ::core::iter::ExactSizeIterator for ListIter<T, U> {
     fn len(&self) -> usize {
-        self.size as usize
+        self.size as usize - self.index as usize
     }
 }
 
@@ -234,7 +234,7 @@ impl<U, T: IndexMove<u16, U>> ::core::iter::Iterator for ShortListIter<T, U> {
 
 impl<U, T: IndexMove<u16, U>> ::core::iter::ExactSizeIterator for ShortListIter<T, U> {
     fn len(&self) -> usize {
-        self.size as usize
+        self.size as usize - self.index as usize
     }
 }
 


Test patch: