diff --git a/src/lib.rs b/src/lib.rs index db22f1d..8aeab4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1725,6 +1725,21 @@ where fn clone(&self) -> SmallVec { SmallVec::from(self.as_slice()) } + + fn clone_from(&mut self, source: &Self) { + // Inspired from `impl Clone for Vec`. + + // drop anything that will not be overwritten + self.truncate(source.len()); + + // self.len <= other.len due to the truncate above, so the + // slices here are always in-bounds. + let (init, tail) = source.split_at(self.len()); + + // reuse the contained values' allocations/resources. + self.clone_from_slice(init); + self.extend(tail.iter().cloned()); + } } impl PartialEq> for SmallVec diff --git a/src/tests.rs b/src/tests.rs index 349a48c..f30dfa2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -925,3 +925,25 @@ fn test_insert_many_overflow() { v.insert_many(0, iter); assert_eq!(&*v, &[0, 2, 4, 123]); } + +#[test] +fn test_clone_from() { + let mut a: SmallVec<[u8; 2]> = SmallVec::new(); + a.push(1); + a.push(2); + a.push(3); + + let mut b: SmallVec<[u8; 2]> = SmallVec::new(); + b.push(10); + + let mut c: SmallVec<[u8; 2]> = SmallVec::new(); + c.push(20); + c.push(21); + c.push(22); + + a.clone_from(&b); + assert_eq!(&*a, &[10]); + + b.clone_from(&c); + assert_eq!(&*b, &[20, 21, 22]); +}