Make BTreeMap::clone() not allocate when cloning an empty tree.

This commit is contained in:
Nicholas Nethercote 2018-06-29 17:02:38 +10:00
parent a2be769fd5
commit f46f05bae8

@ -213,7 +213,16 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
}
}
clone_subtree(self.root.as_ref())
if self.len() == 0 {
// Ideally we'd call `BTreeMap::new` here, but that has the `K:
// Ord` constraint, which this method lacks.
BTreeMap {
root: node::Root::shared_empty_root(),
length: 0,
}
} else {
clone_subtree(self.root.as_ref())
}
}
}