implement more methods and traits for VectorOf...
MODIFY
src/templ/vector.rs
src/templ/vector.rs
+use std::iter::FusedIterator;
+
use libc::size_t;
pub trait Vector<'i> {
@@ -111,6 +113,10 @@ impl<T, S> Iterator for VectorIterator<T>
fn size_hint(&self) -> (usize, Option<usize>) {
(self.vec.len(), None)
}
+
+ fn nth(&mut self, n: usize) -> Option<Self::Item> {
+ self.vec.get(n).ok()
+ }
}
impl<T, S> ExactSizeIterator for VectorIterator<T>
@@ -122,6 +128,10 @@ impl<T, S> ExactSizeIterator for VectorIterator<T>
}
}
+impl<T, S> FusedIterator for VectorIterator<T>
+ where
+ T: for<'i> Vector<'i, Storage=S> {}
+
pub struct VectorRefIterator<'v, T> {
vec: &'v T,
i: size_t,
@@ -148,6 +158,10 @@ impl<T, S> Iterator for VectorRefIterator<'_, T>
fn size_hint(&self) -> (usize, Option<usize>) {
(self.vec.len(), None)
}
+
+ fn nth(&mut self, n: usize) -> Option<Self::Item> {
+ self.vec.get(n).ok()
+ }
}
impl<T, S> ExactSizeIterator for VectorRefIterator<'_, T>
@@ -158,3 +172,7 @@ impl<T, S> ExactSizeIterator for VectorRefIterator<'_, T>
self.vec.len()
}
}
+
+impl<T, S> FusedIterator for VectorRefIterator<'_, T>
+ where
+ T: for<'i> Vector<'i, Storage=S> {}

MODIFY
tests/vector.rs
tests/vector.rs
@@ -217,6 +217,22 @@ fn swap() -> Result<()> {
Ok(())
}
+#[test]
+fn nth() -> Result<()> {
+ {
+ let mut vec = VectorOfint::new();
+ assert_eq!(None, vec.iter().nth(0));
+ vec.push(1);
+ vec.push(2);
+ vec.push(3);
+ assert_eq!(Some(1), vec.iter().nth(0));
+ assert_eq!(Some(2), vec.iter().nth(1));
+ assert_eq!(Some(3), vec.iter().nth(2));
+ }
+
+ Ok(())
+}
+
#[test]
fn out_of_bounds() -> Result<()> {
let mut vec = VectorOfdouble::new();

implement more methods and traits for VectorOf...