Skip to content

Commit f46f05b

Browse files
committed
Make BTreeMap::clone() not allocate when cloning an empty tree.
1 parent a2be769 commit f46f05b

File tree

1 file changed

+10
-1
lines changed
  • src/liballoc/collections/btree

1 file changed

+10
-1
lines changed

Diff for: src/liballoc/collections/btree/map.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,16 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
213213
}
214214
}
215215

216-
clone_subtree(self.root.as_ref())
216+
if self.len() == 0 {
217+
// Ideally we'd call `BTreeMap::new` here, but that has the `K:
218+
// Ord` constraint, which this method lacks.
219+
BTreeMap {
220+
root: node::Root::shared_empty_root(),
221+
length: 0,
222+
}
223+
} else {
224+
clone_subtree(self.root.as_ref())
225+
}
217226
}
218227
}
219228

0 commit comments

Comments
 (0)