Merge pull request #24 from bluss/clone_from
impl clone_from for ArrayVec and ArrayString
This commit is contained in:
@@ -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
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user