Skip to content

Commit 182af10

Browse files
committed
Declare unsafe all functions that can't handle shared roots
1 parent 5e8897b commit 182af10

File tree

2 files changed

+121
-143
lines changed

2 files changed

+121
-143
lines changed

src/liballoc/collections/btree/map.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
156156

157157
let mut in_edge = leaf.first_edge();
158158
while let Ok(kv) = in_edge.right_kv() {
159-
let (k, v) = kv.into_kv();
159+
let (k, v) = unsafe { kv.into_kv() };
160160
in_edge = kv.right_edge();
161161

162-
out_node.push(k.clone(), v.clone());
162+
unsafe { out_node.push(k.clone(), v.clone()) };
163163
out_tree.length += 1;
164164
}
165165
}
@@ -170,10 +170,10 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
170170
let mut out_tree = clone_subtree(internal.first_edge().descend());
171171

172172
{
173-
let mut out_node = out_tree.root.push_level();
173+
let mut out_node = unsafe { out_tree.root.push_level() };
174174
let mut in_edge = internal.first_edge();
175175
while let Ok(kv) = in_edge.right_kv() {
176-
let (k, v) = kv.into_kv();
176+
let (k, v) = unsafe { kv.into_kv() };
177177
in_edge = kv.right_edge();
178178

179179
let k = (*k).clone();
@@ -189,7 +189,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
189189
(root, length)
190190
};
191191

192-
out_node.push(k, v, subroot);
192+
unsafe { out_node.push(k, v, subroot) };
193193
out_tree.length += 1 + sublength;
194194
}
195195
}
@@ -218,7 +218,7 @@ where
218218

219219
fn get(&self, key: &Q) -> Option<&K> {
220220
match search::search_tree(self.root.as_ref(), key) {
221-
Found(handle) => Some(handle.into_kv().0),
221+
Found(handle) => Some(unsafe { handle.into_kv().0 }),
222222
GoDown(_) => None,
223223
}
224224
}
@@ -237,7 +237,7 @@ where
237237
fn replace(&mut self, key: K) -> Option<K> {
238238
self.ensure_root_is_owned();
239239
match search::search_tree::<marker::Mut<'_>, K, (), K>(self.root.as_mut(), &key) {
240-
Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)),
240+
Found(handle) => Some(mem::replace(unsafe { handle.into_kv_mut().0 }, key)),
241241
GoDown(handle) => {
242242
VacantEntry { key, handle, length: &mut self.length, _marker: PhantomData }
243243
.insert(());
@@ -536,7 +536,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
536536
Q: Ord,
537537
{
538538
match search::search_tree(self.root.as_ref(), key) {
539-
Found(handle) => Some(handle.into_kv().1),
539+
Found(handle) => Some(unsafe { handle.into_kv().1 }),
540540
GoDown(_) => None,
541541
}
542542
}
@@ -563,7 +563,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
563563
Q: Ord,
564564
{
565565
match search::search_tree(self.root.as_ref(), k) {
566-
Found(handle) => Some(handle.into_kv()),
566+
Found(handle) => Some(unsafe { handle.into_kv() }),
567567
GoDown(_) => None,
568568
}
569569
}
@@ -592,7 +592,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
592592
K: Borrow<T>,
593593
{
594594
let front = first_leaf_edge(self.root.as_ref());
595-
front.right_kv().ok().map(Handle::into_kv)
595+
let handle = front.right_kv().ok()?;
596+
Some(unsafe { handle.into_kv() })
596597
}
597598

598599
/// Returns the first entry in the map for in-place manipulation.
@@ -653,7 +654,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
653654
K: Borrow<T>,
654655
{
655656
let back = last_leaf_edge(self.root.as_ref());
656-
back.left_kv().ok().map(Handle::into_kv)
657+
let handle = back.left_kv().ok()?;
658+
unsafe { Some(handle.into_kv()) }
657659
}
658660

659661
/// Returns the last entry in the map for in-place manipulation.
@@ -744,7 +746,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
744746
Q: Ord,
745747
{
746748
match search::search_tree(self.root.as_mut(), key) {
747-
Found(handle) => Some(handle.into_kv_mut().1),
749+
Found(handle) => Some(unsafe { handle.into_kv_mut().1 }),
748750
GoDown(_) => None,
749751
}
750752
}
@@ -995,7 +997,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
995997
for (key, value) in iter {
996998
// Try to push key-value pair into the current leaf node.
997999
if cur_node.len() < node::CAPACITY {
998-
cur_node.push(key, value);
1000+
unsafe { cur_node.push(key, value) };
9991001
} else {
10001002
// No space left, go up and push there.
10011003
let mut open_node;
@@ -1015,7 +1017,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10151017
}
10161018
Err(node) => {
10171019
// We are at the top, create a new root node and push there.
1018-
open_node = node.into_root_mut().push_level();
1020+
open_node = unsafe { node.into_root_mut().push_level() };
10191021
break;
10201022
}
10211023
}
@@ -1025,9 +1027,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
10251027
let tree_height = open_node.height() - 1;
10261028
let mut right_tree = node::Root::new_leaf();
10271029
for _ in 0..tree_height {
1028-
right_tree.push_level();
1030+
unsafe { right_tree.push_level() };
10291031
}
1030-
open_node.push(key, value, right_tree);
1032+
unsafe { open_node.push(key, value, right_tree) };
10311033

10321034
// Go down to the right-most leaf again.
10331035
cur_node = last_leaf_edge(open_node.forget_type()).into_node();
@@ -1102,7 +1104,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
11021104
let mut right = Self::new();
11031105
right.root = node::Root::new_leaf();
11041106
for _ in 0..(self.root.as_ref().height()) {
1105-
right.root.push_level();
1107+
unsafe { right.root.push_level() };
11061108
}
11071109

11081110
{
@@ -2387,7 +2389,7 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
23872389
let mut ins_edge;
23882390

23892391
let mut cur_parent = match self.handle.insert(self.key, value) {
2390-
(Fit(handle), _) => return handle.into_kv_mut().1,
2392+
(Fit(handle), _) => return unsafe { handle.into_kv_mut().1 },
23912393
(Split(left, k, v, right), ptr) => {
23922394
ins_k = k;
23932395
ins_v = v;
@@ -2409,7 +2411,7 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
24092411
}
24102412
},
24112413
Err(root) => {
2412-
root.push_level().push(ins_k, ins_v, ins_edge);
2414+
unsafe { root.push_level().push(ins_k, ins_v, ins_edge) };
24132415
return unsafe { &mut *out_ptr };
24142416
}
24152417
}
@@ -2431,7 +2433,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
24312433
/// ```
24322434
#[stable(feature = "map_entry_keys", since = "1.10.0")]
24332435
pub fn key(&self) -> &K {
2434-
self.handle.reborrow().into_kv().0
2436+
unsafe { self.handle.reborrow().into_kv().0 }
24352437
}
24362438

24372439
/// Take ownership of the key and value from the map.
@@ -2475,7 +2477,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
24752477
/// ```
24762478
#[stable(feature = "rust1", since = "1.0.0")]
24772479
pub fn get(&self) -> &V {
2478-
self.handle.reborrow().into_kv().1
2480+
unsafe { self.handle.reborrow().into_kv().1 }
24792481
}
24802482

24812483
/// Gets a mutable reference to the value in the entry.
@@ -2506,7 +2508,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
25062508
/// ```
25072509
#[stable(feature = "rust1", since = "1.0.0")]
25082510
pub fn get_mut(&mut self) -> &mut V {
2509-
self.handle.kv_mut().1
2511+
unsafe { self.handle.kv_mut().1 }
25102512
}
25112513

25122514
/// Converts the entry into a mutable reference to its value.
@@ -2532,7 +2534,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
25322534
/// ```
25332535
#[stable(feature = "rust1", since = "1.0.0")]
25342536
pub fn into_mut(self) -> &'a mut V {
2535-
self.handle.into_kv_mut().1
2537+
unsafe { self.handle.into_kv_mut().1 }
25362538
}
25372539

25382540
/// Sets the value of the entry with the `OccupiedEntry`'s key,
@@ -2584,17 +2586,17 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
25842586

25852587
let (small_leaf, old_key, old_val) = match self.handle.force() {
25862588
Leaf(leaf) => {
2587-
let (hole, old_key, old_val) = leaf.remove();
2589+
let (hole, old_key, old_val) = unsafe { leaf.remove() };
25882590
(hole.into_node(), old_key, old_val)
25892591
}
25902592
Internal(mut internal) => {
2591-
let key_loc = internal.kv_mut().0 as *mut K;
2592-
let val_loc = internal.kv_mut().1 as *mut V;
2593+
let key_loc = unsafe { internal.kv_mut().0 as *mut K };
2594+
let val_loc = unsafe { internal.kv_mut().1 as *mut V };
25932595

25942596
let to_remove = first_leaf_edge(internal.right_edge().descend()).right_kv().ok();
25952597
let to_remove = unsafe { unwrap_unchecked(to_remove) };
25962598

2597-
let (hole, key, val) = to_remove.remove();
2599+
let (hole, key, val) = unsafe { to_remove.remove() };
25982600

25992601
let old_key = unsafe { mem::replace(&mut *key_loc, key) };
26002602
let old_val = unsafe { mem::replace(&mut *val_loc, val) };

0 commit comments

Comments
 (0)