]> git.lizzy.rs Git - rust.git/commitdiff
Make `BTreeMap::clone()` not allocate when cloning an empty tree.
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 29 Jun 2018 07:02:38 +0000 (17:02 +1000)
committerNicholas Nethercote <nnethercote@mozilla.com>
Mon, 2 Jul 2018 03:03:31 +0000 (13:03 +1000)
src/liballoc/collections/btree/map.rs

index e6e454446e232de826fe5a77a4837d4d5a2679e1..2aad3149bb2110556729dc2b0e57f77e1f669738 100644 (file)
@@ -213,7 +213,16 @@ fn clone_subtree<K: Clone, V: Clone>(node: node::NodeRef<marker::Immut,
             }
         }
 
-        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())
+        }
     }
 }