Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ impl<V: Copy, const N: usize> Iterator for IntoIter<V, N> {
if self.pos >= self.next {
None
} else {
let v = unsafe { self.items.add(self.pos).read() };
let v = self.items[self.pos];
self.pos += 1;
Some(v)
}
}
}

impl<'a, V: Copy + 'a, const N: usize> Stack<V, N> {
/// Into-iterate them.
impl<V: Copy, const N: usize> IntoIterator for Stack<V, N> {
type Item = V;
type IntoIter = IntoIter<V, N>;

#[inline]
pub const fn into_iter(&self) -> IntoIter<V, N> {
fn into_iter(self) -> Self::IntoIter {
IntoIter {
pos: 0,
next: self.next,
items: self.items.as_ptr(),
items: self.items,
}
}
}
Expand Down Expand Up @@ -96,9 +98,5 @@ fn push_and_into_iterate() {
let mut p: Stack<u64, 16> = Stack::new();
unsafe { p.push_unchecked(1) };
unsafe { p.push_unchecked(2) };
let mut sum = 0;
for x in p.into_iter() {
sum += x;
}
assert_eq!(3, sum);
assert_eq!(vec![1, 2], p.into_iter().collect::<Vec<_>>());
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ pub struct IntoIter<V: Copy, const N: usize> {
/// The next available position in the array.
next: usize,
/// The fixed-size array of values.
items: *const V,
items: [V; N],
}
Loading