diff --git a/src/arrayvec.rs b/src/arrayvec.rs index 5ca8eab..b652ed0 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -244,8 +244,9 @@ impl ArrayVec { ); ArrayVecDrain { parent: self, + target_start: start, target_index: start, - target_count: end - start, + target_end: end, } } @@ -780,16 +781,17 @@ impl ArrayVec { /// See [`ArrayVecDrain::drain`](ArrayVecDrain::::drain) pub struct ArrayVecDrain<'p, A: Array> { parent: &'p mut ArrayVec, + target_start: usize, target_index: usize, - target_count: usize, + target_end: usize, } impl<'p, A: Array> Iterator for ArrayVecDrain<'p, A> { type Item = A::Item; #[inline] fn next(&mut self) -> Option { - if self.target_count > 0 { - let out = self.parent.remove(self.target_index); - self.target_count -= 1; + if self.target_index != self.target_end { + let out = take(&mut self.parent[self.target_index]); + self.target_index += 1; Some(out) } else { None @@ -799,7 +801,13 @@ impl<'p, A: Array> Iterator for ArrayVecDrain<'p, A> { impl<'p, A: Array> Drop for ArrayVecDrain<'p, A> { #[inline] fn drop(&mut self) { - for _ in self {} + // Changed because it was moving `self`, it's also more clear and the std does the same + self.for_each(drop); + // Implementation very similar to [`ArrayVec::remove`](ArrayVec::remove) + let count = self.target_end - self.target_start; + let targets: &mut [A::Item] = &mut self.parent.deref_mut()[self.target_start..]; + targets.rotate_left(count); + self.parent.len -= count; } } @@ -874,6 +882,14 @@ pub struct ArrayVecIterator { len: usize, data: A, } + +impl ArrayVecIterator { + /// Returns the remaining items of this iterator as a slice. + pub fn as_slice(&self) -> &[A::Item] { + &self.data.as_slice()[self.base..self.len] + } +} + impl Iterator for ArrayVecIterator { type Item = A::Item; #[inline] @@ -913,6 +929,12 @@ impl Iterator for ArrayVecIterator { } } +impl Debug for ArrayVecIterator where A::Item: Debug { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + f.debug_tuple("ArrayVecIterator").field(&self.as_slice()).finish() + } +} + impl IntoIterator for ArrayVec { type Item = A::Item; type IntoIter = ArrayVecIterator; diff --git a/src/tinyvec.rs b/src/tinyvec.rs index 477b445..019839e 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -724,6 +724,17 @@ pub enum TinyVecIterator { #[allow(missing_docs)] Heap(alloc::vec::IntoIter), } + +impl TinyVecIterator { + /// Returns the remaining items of this iterator as a slice. + pub fn as_slice(&self) -> &[A::Item] { + match self { + TinyVecIterator::Inline(a) => a.as_slice(), + TinyVecIterator::Heap(v) => v.as_slice(), + } + } +} + impl Iterator for TinyVecIterator { type Item = A::Item; #[inline] @@ -763,6 +774,13 @@ impl Iterator for TinyVecIterator { } } } + +impl Debug for TinyVecIterator where A::Item: Debug { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + f.debug_tuple("TinyVecIterator").field(&self.as_slice()).finish() + } +} + impl IntoIterator for TinyVec { type Item = A::Item; type IntoIter = TinyVecIterator; @@ -776,6 +794,26 @@ impl IntoIterator for TinyVec { } } +impl<'a, A: Array> IntoIterator for &'a mut TinyVec { + type Item = &'a mut A::Item; + type IntoIter = alloc::slice::IterMut<'a, A::Item>; + #[inline(always)] + #[must_use] + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + +impl<'a, A: Array> IntoIterator for &'a TinyVec { + type Item = &'a A::Item; + type IntoIter = alloc::slice::Iter<'a, A::Item>; + #[inline(always)] + #[must_use] + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + impl PartialEq for TinyVec where A::Item: PartialEq,