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

Issue:
implement `iter` for lists
Currently only `struct_list` implements `iter`. For `data_list`, `text_list`, `list_list`, `enum_list` and `primitive_list` it seems to be missing.



Discussion / hints:

Fix patch:
diff --git a/src/data_list.rs b/src/data_list.rs
index ba2e58253..7a9490644 100644
--- a/src/data_list.rs
+++ b/src/data_list.rs
@@ -21,7 +21,7 @@
 
 //! List of sequences of bytes.
 
-use traits::{FromPointerReader, FromPointerBuilder};
+use traits::{FromPointerReader, FromPointerBuilder, IndexMove, ListIter};
 use private::layout::*;
 use Result;
 
@@ -44,6 +44,11 @@ impl <'a> Reader<'a> {
     }
 
     pub fn len(&self) -> u32 { self.reader.len() }
+
+    pub fn iter(self) -> ListIter<Reader<'a>, Result<::data::Reader<'a>>>{
+        let l = self.len();
+        ListIter::new(self, l)
+    }
 }
 
 impl <'a> FromPointerReader<'a> for Reader<'a> {
@@ -52,6 +57,12 @@ impl <'a> FromPointerReader<'a> for Reader<'a> {
     }
 }
 
+impl <'a>  IndexMove<u32, Result<::data::Reader<'a>>> for Reader<'a>{
+    fn index_move(&self, index : u32) -> Result<::data::Reader<'a>> {
+        self.get(index)
+    }
+}
+
 impl <'a> Reader<'a> {
     pub fn get(self, index : u32) -> Result<::data::Reader<'a>> {
         assert!(index <  self.len());
diff --git a/src/enum_list.rs b/src/enum_list.rs
index e812c7d39..44dae87ad 100644
--- a/src/enum_list.rs
+++ b/src/enum_list.rs
@@ -21,7 +21,7 @@
 
 //! List of enums.
 
-use traits::{FromPointerReader, FromPointerBuilder, ToU16, FromU16};
+use traits::{FromPointerReader, FromPointerBuilder, ToU16, FromU16, ListIter, IndexMove};
 use private::layout::{ListReader, ListBuilder, PointerReader, PointerBuilder,
                       TwoBytes, PrimitiveElement};
 use {NotInSchema, Result};
@@ -49,6 +49,10 @@ impl <'a, T: FromU16> Reader<'a, T> {
 
     pub fn len(&self) -> u32 { self.reader.len() }
 
+    pub fn iter(self) -> ListIter<Reader<'a, T>, ::std::result::Result<T, NotInSchema>>{
+        let l = self.len();
+        ListIter::new(self, l)
+    }
 }
 
 impl <'a, T : FromU16> FromPointerReader<'a> for Reader<'a, T> {
@@ -58,6 +62,12 @@ impl <'a, T : FromU16> FromPointerReader<'a> for Reader<'a, T> {
     }
 }
 
+impl <'a, T: FromU16>  IndexMove<u32, ::std::result::Result<T, NotInSchema>> for Reader<'a, T>{
+    fn index_move(&self, index : u32) -> ::std::result::Result<T, NotInSchema> {
+        self.get(index)
+    }
+}
+
 impl <'a, T : FromU16> Reader<'a, T> {
     pub fn get(&self, index : u32) -> ::std::result::Result<T, NotInSchema> {
         assert!(index < self.len());
diff --git a/src/primitive_list.rs b/src/primitive_list.rs
index 91239462f..3daddf383 100644
--- a/src/primitive_list.rs
+++ b/src/primitive_list.rs
@@ -21,7 +21,7 @@
 
 //! List of primitives.
 
-use traits::{FromPointerReader, FromPointerBuilder};
+use traits::{FromPointerReader, FromPointerBuilder, IndexMove, ListIter};
 use private::layout::{ListReader, ListBuilder, PointerReader, PointerBuilder,
                       PrimitiveElement};
 use Result;
@@ -48,6 +48,11 @@ impl <'a, T : PrimitiveElement> Reader<'a, T> {
     }
 
     pub fn len(&self) -> u32 { self.reader.len() }
+
+    pub fn iter(self) -> ListIter<Reader<'a, T>, T>{
+        let l = self.len();
+        ListIter::new(self, l)
+    }
 }
 
 impl <'a, T : PrimitiveElement> FromPointerReader<'a> for Reader<'a, T> {
@@ -57,6 +62,12 @@ impl <'a, T : PrimitiveElement> FromPointerReader<'a> for Reader<'a, T> {
     }
 }
 
+impl <'a, T: PrimitiveElement>  IndexMove<u32, T> for Reader<'a, T>{
+    fn index_move(&self, index : u32) -> T {
+        self.get(index)
+    }
+}
+
 impl <'a, T : PrimitiveElement> Reader<'a, T> {
     pub fn get(&self, index : u32) -> T {
         assert!(index < self.len());
diff --git a/src/text_list.rs b/src/text_list.rs
index 235d17618..39e8b6b76 100644
--- a/src/text_list.rs
+++ b/src/text_list.rs
@@ -21,7 +21,7 @@
 
 //! List of strings containing UTF-8 encoded text.
 
-use traits::{FromPointerReader, FromPointerBuilder};
+use traits::{FromPointerReader, FromPointerBuilder, IndexMove, ListIter};
 use private::layout::{ListBuilder, ListReader, Pointer, PointerBuilder, PointerReader};
 use Result;
 
@@ -44,6 +44,11 @@ impl <'a> Reader<'a> {
     }
 
     pub fn len(&self) -> u32 { self.reader.len() }
+
+    pub fn iter(self) -> ListIter<Reader<'a>, Result<::text::Reader<'a>>>{
+        let l = self.len();
+        ListIter::new(self, l)
+    }
 }
 
 impl <'a> FromPointerReader<'a> for Reader<'a> {
@@ -52,6 +57,12 @@ impl <'a> FromPointerReader<'a> for Reader<'a> {
     }
 }
 
+impl <'a>  IndexMove<u32, Result<::text::Reader<'a>>> for Reader<'a>{
+    fn index_move(&self, index : u32) -> Result<::text::Reader<'a>> {
+        self.get(index)
+    }
+}
+
 impl <'a> Reader<'a> {
     pub fn get(self, index : u32) -> Result<::text::Reader<'a>> {
         assert!(index <  self.len());
diff --git a/src/traits.rs b/src/traits.rs
index ed8670da8..52bcda68d 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -111,7 +111,7 @@ pub struct ListIter<T, U> {
     size : u32,
 }
 
-impl <T, U> ListIter<T, U> {
+impl <T, U> ListIter<T, U>{
     pub fn new(list : T, size : u32) -> ListIter<T, U> {
         ListIter { list : list, index : 0, size : size, marker : ::std::marker::PhantomData }
     }
@@ -128,4 +128,24 @@ impl <U, T : IndexMove<u32, U>> ::std::iter::Iterator for ListIter<T, U> {
             return None;
         }
     }
+
+    fn size_hint(&self) -> (usize, Option<usize>){
+        (self.size as usize, Some(self.size as usize))
+    }
+
+    fn nth(&mut self, p: usize) -> Option<U>{
+        if p < self.size as usize {
+            self.index = p as u32;
+            let result = self.list.index_move(self.index);
+            return Some(result)
+        } else {
+            None
+        }
+    }
+}
+
+impl <U, T: IndexMove<u32, U>> ::std::iter::ExactSizeIterator for ListIter<T, U>{
+    fn len(&self) -> usize{
+        self.size as usize
+    }
 }


Test patch: