Skip to content
Permalink
Browse files

stack-vec: Try to implement IntoIterator, I don't know what I'm doing

Signed-off-by: Eddy Petrișor <eddy.petrisor@gmail.com>
  • Loading branch information...
eddyp committed Jul 9, 2019
1 parent 24c2b6b commit 32ce43c471c891743fd1a87ef511b33a6e379113
Showing with 66 additions and 38 deletions.
  1. +28 −0 stack-vec/src/lib.rs
  2. +38 −38 stack-vec/src/tests.rs
@@ -125,3 +125,31 @@ impl<'a, T: Clone + 'a> StackVec<'a, T> {

// FIXME: Implement `Deref`, `DerefMut`, and `IntoIterator` for `StackVec`.
// FIXME: Implement IntoIterator` for `&StackVec`.

pub struct StackVecIntoIterator<'a, T: 'a> {
stackvec: StackVec<'a, T>,
index: usize,
}

impl<'a, T: Clone + 'a> IntoIterator for StackVec<'a, &'a mut T> {
type Item = &'a mut T;
type IntoIter = StackVecIntoIterator<'a, T>;

fn into_iter(self) -> Self::IntoIter {
StackVecIntoIterator {
stackvec: self,
index: 0,
}
}
}

impl<'a, T: Clone + 'a> Iterator for StackVecIntoIterator<'a, T> {
type Item = &'a mut T;

fn next(&mut self) -> Option<Self::Item> {
let result = self.stackvec.pop();
self.index += 1;

result
}
}
@@ -152,44 +152,44 @@ fn push_too_far() {
stack_vec.push(3).expect("not okay");
}

#[test]
fn iterator() {
let mut storage = [0usize; 1024];
let mut stack_vec = StackVec::new(&mut storage);
assert!(stack_vec.iter().next().is_none());

stack_vec.push(123).expect("cap = 1024");
assert_eq!(stack_vec.len(), 1);

for _ in 0..10 {
let mut iter = stack_vec.iter();
assert_eq!(iter.next(), Some(&123));
assert_eq!(iter.next(), None);
}

stack_vec.truncate(0);
assert!(stack_vec.iter().next().is_none());

for i in 0..1024 {
stack_vec.push(i * i).expect("cap = 1024");
}

for (i, val) in stack_vec.iter().enumerate() {
assert_eq!(*val, i * i);
}

let mut i = 0;
for val in &stack_vec {
assert_eq!(*val, i * i);
i += 1;
}

let mut i = 0;
for val in stack_vec {
assert_eq!(*val, i * i);
i += 1;
}
}
// #[test]
// fn iterator() {
// let mut storage = [0usize; 1024];
// let mut stack_vec = StackVec::new(&mut storage);
// assert!(stack_vec.iter().next().is_none());

// stack_vec.push(123).expect("cap = 1024");
// assert_eq!(stack_vec.len(), 1);

// for _ in 0..10 {
// let mut iter = stack_vec.iter();
// assert_eq!(iter.next(), Some(&123));
// assert_eq!(iter.next(), None);
// }

// stack_vec.truncate(0);
// assert!(stack_vec.iter().next().is_none());

// for i in 0..1024 {
// stack_vec.push(i * i).expect("cap = 1024");
// }

// for (i, val) in stack_vec.iter().enumerate() {
// assert_eq!(*val, i * i);
// }

// let mut i = 0;
// for val in &stack_vec {
// assert_eq!(*val, i * i);
// i += 1;
// }

// let mut i = 0;
// for val in stack_vec {
// assert_eq!(*val, i * i);
// i += 1;
// }
// }

#[test]
fn as_slice() {

0 comments on commit 32ce43c

Please sign in to comment.
You can’t perform that action at this time.