summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2018-06-29 17:02:38 +1000
committerNicholas Nethercote <nnethercote@mozilla.com>2018-07-02 13:03:31 +1000
commitf46f05bae8d81246f1ad93ccee051a13c9be393e (patch)
tree4d3f7645c7a5ee3bfaebb31c80e48c62d43224f8 /src/liballoc
parenta2be769fd50403a07c45677f8f285491c8e90d74 (diff)
downloadrust-f46f05bae8d81246f1ad93ccee051a13c9be393e.tar.gz
rust-f46f05bae8d81246f1ad93ccee051a13c9be393e.zip
Make `BTreeMap::clone()` not allocate when cloning an empty tree.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/collections/btree/map.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/liballoc/collections/btree/map.rs b/src/liballoc/collections/btree/map.rs
index e6e454446e2..2aad3149bb2 100644
--- a/src/liballoc/collections/btree/map.rs
+++ b/src/liballoc/collections/btree/map.rs
@@ -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())
+        }
     }
 }