Skip to content

Commit f837c73

Browse files
authored
Rollup merge of #68468 - ssomers:btreemap_prefer_middle, r=Mark-Simulacrum
BTreeMap: tag and explain unsafe internal functions or assert preconditions #68418 concluded that it's not desirable to tag all internal functions with preconditions as being unsafe. This PR does it to some functions, documents why, and elsewhere enforces the preconditions with asserts.
2 parents 12c9562 + ba87a50 commit f837c73

File tree

3 files changed

+132
-119
lines changed

3 files changed

+132
-119
lines changed

src/liballoc/collections/btree/map.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2096,8 +2096,13 @@ where
20962096
}
20972097
}
20982098

2099-
let front = Handle::new_edge(min_node, min_edge);
2100-
let back = Handle::new_edge(max_node, max_edge);
2099+
// Safety guarantee: `min_edge` is always in range for `min_node`, because
2100+
// `min_edge` is unconditionally calculated for each iteration's value of `min_node`,
2101+
// either (if not found) as the edge index returned by `search_linear`,
2102+
// or (if found) as the KV index returned by `search_linear`, possibly + 1.
2103+
// Likewise for `max_node` versus `max_edge`.
2104+
let front = unsafe { Handle::new_edge(min_node, min_edge) };
2105+
let back = unsafe { Handle::new_edge(max_node, max_edge) };
21012106
match (front.force(), back.force()) {
21022107
(Leaf(f), Leaf(b)) => {
21032108
return (f, b);

0 commit comments

Comments
 (0)