Merge pull request #24 from bluss/clone_from

impl clone_from for ArrayVec and ArrayString
This commit is contained in:
bluss
2016-02-06 20:22:40 +01:00
3 changed files with 73 additions and 0 deletions
+30
View File
@@ -624,6 +624,28 @@ impl<A: Array> Clone for ArrayVec<A>
fn clone(&self) -> Self {
self.iter().cloned().collect()
}
fn clone_from(&mut self, rhs: &Self) {
// recursive case for the common prefix
let prefix = cmp::min(self.len(), rhs.len());
{
let a = &mut self[..prefix];
let b = &rhs[..prefix];
for i in 0..prefix {
a[i].clone_from(&b[i]);
}
}
if prefix < self.len() {
// rhs was shorter
for _ in 0..self.len() - prefix {
self.pop();
}
} else {
for elt in &rhs[self.len()..] {
self.push(elt.clone());
}
}
}
}
impl<A: Array> Hash for ArrayVec<A>
@@ -642,6 +664,14 @@ impl<A: Array> PartialEq for ArrayVec<A>
}
}
impl<A: Array> PartialEq<[A::Item]> for ArrayVec<A>
where A::Item: PartialEq
{
fn eq(&self, other: &[A::Item]) -> bool {
**self == *other
}
}
impl<A: Array> Eq for ArrayVec<A> where A::Item: Eq { }
impl<A: Array> Borrow<[A::Item]> for ArrayVec<A> {