1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Allows creating owning iterators out of arrays. This is useful for
//! fixed-size iterators. Try writing the following function:
//!
//! ```compile_fail
//! fn pair_iter(a: i32, b: i32) -> impl Iterator<Item=i32> {
//! 	unimplemented!()
//! }
//! ```
//!
//! This is hard to do because most iterators don't own the values they are iterating over. One ugly solution is:
//!
//! ```
//! fn pair_iter(a: i32, b: i32) -> impl Iterator<Item=i32> {
//! 	Some(a).into_iter().chain(Some(b))
//! }
//!
//! assert_eq!(pair_iter(1, 2).collect::<Vec<_>>(), &[1, 2]);
//! ```
//!
//! This crate allows turning arrays into owning iterators, perfect for short, fixed-size iterators.
//!
//! ```
//! extern crate array_iterator;
//! use array_iterator::ArrayIterator;
//!
//! fn pair_iter(a: i32, b: i32) -> impl Iterator<Item=i32> {
//! 	ArrayIterator::new([a, b])
//! }
//!
//! assert_eq!(pair_iter(1, 2).collect::<Vec<_>>(), &[1, 2]);
//! ```

#[cfg(test)]mod tests;

#[derive(Clone)]
pub struct ArrayIterator<T, A: Array<T>> {
	array: std::mem::ManuallyDrop<A>,
	current: usize,
	_item: std::marker::PhantomData<T>,
}

impl<T, A: Array<T>> ArrayIterator<T, A> {
	pub fn new(array: A) -> Self {
		ArrayIterator {
			array: std::mem::ManuallyDrop::new(array),
			current: 0,
			_item: std::marker::PhantomData,
		}
	}
}

impl <T, A: Array<T>> Iterator for ArrayIterator<T, A> {
	type Item = T;

	fn next(&mut self) -> Option<T> {
		let data = self.array.slice_mut();

		if self.current >= data.len() {
			return None
		}

		let u = unsafe { std::mem::uninitialized() };
		let item = std::mem::replace(&mut data[self.current], u);
		self.current += 1;

		Some(item)
	}

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

impl<T, A: Array<T>> std::iter::ExactSizeIterator for ArrayIterator<T, A> {
	fn len(&self) -> usize {
		self.array.slice().len() - self.current
	}
}

impl<T, A: Array<T>> std::iter::FusedIterator for ArrayIterator<T, A> { }

// https://github.com/rust-lang/rust/issues/37572
// unsafe impl<T, A: Array<T>> std::iter::TrustedLen for ArrayIterator<T, A> { }

impl<T, A: Array<T>> Drop for ArrayIterator<T, A> {
	fn drop(&mut self) {
		// We don't actually drop the array itself, just everything inside of it.
		// Hopefully this is fine.
		for _ in self {
		}
	}
}

// TODO: Use FixedSizeArray https://github.com/rust-lang/rust/issues/27778
pub trait Array<T> {
	fn slice(&self) -> &[T];
	fn slice_mut(&mut self) -> &mut [T];
}

impl<T> Array<T> for [T;  0] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  1] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  2] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  3] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  4] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  5] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  6] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  7] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  8] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T;  9] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 10] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 11] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 12] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 13] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 14] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 15] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 16] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 17] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 18] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 19] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 20] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 21] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 22] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 23] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 24] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 25] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 26] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 27] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 28] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 29] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 30] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 31] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 32] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 33] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 34] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 35] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 36] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 37] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 38] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 39] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 40] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 41] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 42] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 43] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 44] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 45] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 46] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 47] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 48] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 49] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 50] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 51] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 52] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 53] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 54] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 55] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 56] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 57] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 58] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 59] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 60] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 61] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 62] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 63] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }
impl<T> Array<T> for [T; 64] { fn slice(&self) -> &[T] { self } fn slice_mut(&mut self) -> &mut [T] { self } }