impl clone_from for ArrayVec and ArrayString

This commit is contained in:
bluss
2016-02-06 01:33:58 +01:00
parent a5f63d3191
commit a17c764f98
3 changed files with 73 additions and 0 deletions
+6
View File
@@ -225,4 +225,10 @@ impl<A: Array<Item=u8> + Copy> Clone for ArrayString<A> {
fn clone(&self) -> ArrayString<A> {
*self
}
fn clone_from(&mut self, rhs: &Self) {
// guaranteed to fit due to types matching.
self.clear();
self.push_str(rhs).ok();
}
}
+30
View File
@@ -607,6 +607,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>
@@ -625,6 +647,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> {