Merge branch 'master' into small-updates

This commit is contained in:
Lokathor
2020-01-26 13:37:57 -07:00
committed by GitHub
2 changed files with 66 additions and 6 deletions
+28 -6
View File
@@ -244,8 +244,9 @@ impl<A: Array> ArrayVec<A> {
);
ArrayVecDrain {
parent: self,
target_start: start,
target_index: start,
target_count: end - start,
target_end: end,
}
}
@@ -780,16 +781,17 @@ impl<A: Array> ArrayVec<A> {
/// See [`ArrayVecDrain::drain`](ArrayVecDrain::<A>::drain)
pub struct ArrayVecDrain<'p, A: Array> {
parent: &'p mut ArrayVec<A>,
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<Self::Item> {
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<A: Array> {
len: usize,
data: A,
}
impl<A: Array> ArrayVecIterator<A> {
/// 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<A: Array> Iterator for ArrayVecIterator<A> {
type Item = A::Item;
#[inline]
@@ -913,6 +929,12 @@ impl<A: Array> Iterator for ArrayVecIterator<A> {
}
}
impl<A: Array> Debug for ArrayVecIterator<A> where A::Item: Debug {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ArrayVecIterator").field(&self.as_slice()).finish()
}
}
impl<A: Array> IntoIterator for ArrayVec<A> {
type Item = A::Item;
type IntoIter = ArrayVecIterator<A>;
+38
View File
@@ -707,6 +707,17 @@ pub enum TinyVecIterator<A: Array> {
#[allow(missing_docs)]
Heap(alloc::vec::IntoIter<A::Item>),
}
impl<A: Array> TinyVecIterator<A> {
/// 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<A: Array> Iterator for TinyVecIterator<A> {
type Item = A::Item;
#[inline]
@@ -746,6 +757,13 @@ impl<A: Array> Iterator for TinyVecIterator<A> {
}
}
}
impl<A: Array> Debug for TinyVecIterator<A> where A::Item: Debug {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("TinyVecIterator").field(&self.as_slice()).finish()
}
}
impl<A: Array> IntoIterator for TinyVec<A> {
type Item = A::Item;
type IntoIter = TinyVecIterator<A>;
@@ -759,6 +777,26 @@ impl<A: Array> IntoIterator for TinyVec<A> {
}
}
impl<'a, A: Array> IntoIterator for &'a mut TinyVec<A> {
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<A> {
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<A: Array> PartialEq for TinyVec<A>
where
A::Item: PartialEq,