Merge pull request #104 from clarcharr/clone_into_iter

Implement Clone, Debug for IntoIter
This commit is contained in:
bluss
2018-10-30 19:51:06 +01:00
committed by GitHub
2 changed files with 43 additions and 0 deletions
+24
View File
@@ -748,6 +748,30 @@ impl<A: Array> Drop for IntoIter<A> {
} }
} }
impl<A: Array> Clone for IntoIter<A>
where
A::Item: Clone,
{
fn clone(&self) -> IntoIter<A> {
self.v[self.index.to_usize()..]
.iter()
.cloned()
.collect::<ArrayVec<A>>()
.into_iter()
}
}
impl<A: Array> fmt::Debug for IntoIter<A>
where
A::Item: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list()
.entries(&self.v[self.index.to_usize()..])
.finish()
}
}
/// A draining iterator for `ArrayVec`. /// A draining iterator for `ArrayVec`.
pub struct Drain<'a, A> pub struct Drain<'a, A>
where A: Array, where A: Array,
+19
View File
@@ -55,6 +55,7 @@ fn test_drop() {
let flag = &Cell::new(0); let flag = &Cell::new(0);
#[derive(Clone)]
struct Bump<'a>(&'a Cell<i32>); struct Bump<'a>(&'a Cell<i32>);
impl<'a> Drop for Bump<'a> { impl<'a> Drop for Bump<'a> {
@@ -105,6 +106,24 @@ fn test_drop() {
assert_eq!(flag.get(), 3); assert_eq!(flag.get(), 3);
} }
// test cloning into_iter
flag.set(0);
{
let mut array = ArrayVec::<[_; 3]>::new();
array.push(Bump(flag));
array.push(Bump(flag));
array.push(Bump(flag));
let mut iter = array.into_iter();
assert_eq!(flag.get(), 0);
iter.next();
assert_eq!(flag.get(), 1);
let clone = iter.clone();
assert_eq!(flag.get(), 1);
drop(clone);
assert_eq!(flag.get(), 3);
drop(iter);
assert_eq!(flag.get(), 5);
}
} }
#[test] #[test]