Skip to content

impl IntoIterator for GenericArray #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2016
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
97 changes: 97 additions & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use nodrop::NoDrop;
use std::cmp;
use std::ptr;
use super::{GenericArray, ArrayLength};

/// An iterator that moves out of a `GenericArray`
pub struct GenericArrayIter<T, N: ArrayLength<T>> {
// Invariants: index <= index_back <= N
// Only values in array[index..index_back] are alive at any given time.
// Values from array[..index] and array[index_back..] are already moved/dropped.
array: NoDrop<GenericArray<T, N>>,
index: usize,
index_back: usize,
}

impl<T, N> IntoIterator for GenericArray<T, N> where N: ArrayLength<T> {
type Item = T;
type IntoIter = GenericArrayIter<T, N>;

fn into_iter(self) -> Self::IntoIter {
GenericArrayIter {
array: NoDrop::new(self),
index: 0,
index_back: N::to_usize(),
}
}
}

impl<T, N> Drop for GenericArrayIter<T, N> where N: ArrayLength<T> {
fn drop(&mut self) {
// Drop values that are still alive.
for p in &mut self.array[self.index..self.index_back] {
unsafe { ptr::drop_in_place(p); }
}
}
}

impl<T, N> Iterator for GenericArrayIter<T, N> where N: ArrayLength<T> {
type Item = T;

fn next(&mut self) -> Option<T> {
if self.len() > 0 {
unsafe {
let p = self.array.get_unchecked(self.index);
self.index += 1;
Some(ptr::read(p))
}
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}

fn count(self) -> usize {
self.len()
}

fn nth(&mut self, n: usize) -> Option<T> {
// First consume values prior to the nth.
let ndrop = cmp::min(n, self.len());
for p in &mut self.array[self.index..self.index + ndrop] {
self.index += 1;
unsafe { ptr::drop_in_place(p); }
}

self.next()
}

fn last(mut self) -> Option<T> {
// Note, everything else will correctly drop first as `self` leaves scope.
self.next_back()
}
}

impl<T, N> DoubleEndedIterator for GenericArrayIter<T, N> where N: ArrayLength<T> {
fn next_back(&mut self) -> Option<T> {
if self.len() > 0 {
self.index_back -= 1;
unsafe {
let p = self.array.get_unchecked(self.index_back);
Some(ptr::read(p))
}
} else {
None
}
}
}

impl<T, N> ExactSizeIterator for GenericArrayIter<T, N> where N: ArrayLength<T> {
fn len(&self) -> usize {
self.index_back - self.index
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern crate core as std;
extern crate typenum;
extern crate nodrop;
pub mod arr;
pub mod iter;
pub use iter::GenericArrayIter;
use nodrop::NoDrop;
use typenum::uint::{Unsigned, UTerm, UInt};
use typenum::bit::{B0, B1};
Expand Down
5 changes: 5 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ fn test_arr() {
let test: GenericArray<u32, U3> = arr![u32; 1, 2, 3];
assert_eq!(test[1], 2);
}

#[test]
fn test_iter_flat_map() {
assert!((0..5).flat_map(|i| arr![i32; 2 * i, 2 * i + 1]).eq(0..10));
}