From 9001b1e948bc4428015a523bd4af05a4164b1fdf Mon Sep 17 00:00:00 2001 From: Charles Gleason Date: Fri, 22 Nov 2019 14:22:06 -0500 Subject: [PATCH] Implement clone_from for BTree collections --- src/liballoc/collections/btree/map.rs | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/liballoc/collections/btree/map.rs b/src/liballoc/collections/btree/map.rs index dbd033c76b7e3..22437efd40dd7 100644 --- a/src/liballoc/collections/btree/map.rs +++ b/src/liballoc/collections/btree/map.rs @@ -206,6 +206,35 @@ impl Clone for BTreeMap { clone_subtree(self.root.as_ref()) } } + + fn clone_from(&mut self, other: &Self) { + if let Some(key) = { + if self.len() > other.len() { + let diff = self.len() - other.len(); + if diff <= other.len() { + self.iter().nth_back(diff - 1).map(|pair| (*pair.0).clone()) + } else { + self.iter().nth(other.len()).map(|pair| (*pair.0).clone()) + } + } else { + None + } + } { + self.split_off(&key); + } + let mut siter = self.range_mut(..); + let mut oiter = other.iter(); + while siter.front != siter.back { + if let Some((ok, ov)) = oiter.next() { + let (sk, sv) = unsafe { siter.next_unchecked() }; + sk.clone_from(ok); + sv.clone_from(ov); + } else { + break ; + } + } + self.extend(oiter.map(|(k, v)| ((*k).clone(), (*v).clone()))); + } } impl super::Recover for BTreeMap