Skip to content

Commit 62f98a2

Browse files
committed
btree: use Option's unwrap_unchecked()
Now that rust-lang#81383 is available, start using it. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent c4e33b5 commit 62f98a2

File tree

5 files changed

+13
-31
lines changed

5 files changed

+13
-31
lines changed

library/alloc/src/collections/btree/map.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use core::ptr;
1111
use super::borrow::DormantMutRef;
1212
use super::node::{self, marker, ForceResult::*, Handle, NodeRef, Root};
1313
use super::search::SearchResult::*;
14-
use super::unwrap_unchecked;
1514

1615
mod entry;
1716
pub use entry::{Entry, OccupiedEntry, VacantEntry};
@@ -1386,7 +1385,7 @@ impl<K, V> Drop for IntoIter<K, V> {
13861385

13871386
unsafe {
13881387
let mut node =
1389-
unwrap_unchecked(ptr::read(&self.0.front)).into_node().forget_type();
1388+
ptr::read(&self.0.front).unwrap_unchecked().into_node().forget_type();
13901389
while let Some(parent) = node.deallocate_and_ascend() {
13911390
node = parent.into_node().forget_type();
13921391
}
@@ -1711,7 +1710,7 @@ impl<'a, K, V> Range<'a, K, V> {
17111710
}
17121711

17131712
unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
1714-
unsafe { unwrap_unchecked(self.front.as_mut()).next_unchecked() }
1713+
unsafe { self.front.as_mut().unwrap_unchecked().next_unchecked() }
17151714
}
17161715
}
17171716

@@ -1800,7 +1799,7 @@ impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
18001799

18011800
impl<'a, K, V> Range<'a, K, V> {
18021801
unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
1803-
unsafe { unwrap_unchecked(self.back.as_mut()).next_back_unchecked() }
1802+
unsafe { self.back.as_mut().unwrap_unchecked().next_back_unchecked() }
18041803
}
18051804
}
18061805

@@ -1846,7 +1845,7 @@ impl<'a, K, V> RangeMut<'a, K, V> {
18461845
}
18471846

18481847
unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
1849-
unsafe { unwrap_unchecked(self.front.as_mut()).next_unchecked() }
1848+
unsafe { self.front.as_mut().unwrap_unchecked().next_unchecked() }
18501849
}
18511850

18521851
/// Returns an iterator of references over the remaining items.
@@ -1876,7 +1875,7 @@ impl<K, V> FusedIterator for RangeMut<'_, K, V> {}
18761875

18771876
impl<'a, K, V> RangeMut<'a, K, V> {
18781877
unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
1879-
unsafe { unwrap_unchecked(self.back.as_mut()).next_back_unchecked() }
1878+
unsafe { self.back.as_mut().unwrap_unchecked().next_back_unchecked() }
18801879
}
18811880
}
18821881

library/alloc/src/collections/btree/mod.rs

-16
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,6 @@ trait Recover<Q: ?Sized> {
1919
fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
2020
}
2121

22-
/// Same purpose as `Option::unwrap` but doesn't always guarantee a panic
23-
/// if the option contains no value.
24-
/// SAFETY: the caller must ensure that the option contains a value.
25-
#[inline(always)]
26-
pub unsafe fn unwrap_unchecked<T>(val: Option<T>) -> T {
27-
val.unwrap_or_else(|| {
28-
if cfg!(debug_assertions) {
29-
panic!("'unchecked' unwrap on None in BTreeMap");
30-
} else {
31-
unsafe {
32-
core::intrinsics::unreachable();
33-
}
34-
}
35-
})
36-
}
37-
3822
#[cfg(test)]
3923
/// XorShiftRng
4024
struct DeterministicRng {

library/alloc/src/collections/btree/navigate.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::ptr;
66

77
use super::node::{marker, ForceResult::*, Handle, NodeRef};
88
use super::search::SearchResult;
9-
use super::unwrap_unchecked;
109

1110
/// Finds the leaf edges delimiting a specified range in or underneath a node.
1211
///
@@ -310,7 +309,7 @@ macro_rules! def_next_kv_uncheched_dealloc {
310309
Err(last_edge) => {
311310
unsafe {
312311
let parent_edge = last_edge.into_node().deallocate_and_ascend();
313-
unwrap_unchecked(parent_edge).forget_node_type()
312+
parent_edge.unwrap_unchecked().forget_node_type()
314313
}
315314
}
316315
}
@@ -331,7 +330,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Ed
331330
pub unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
332331
super::mem::replace(self, |leaf_edge| {
333332
let kv = leaf_edge.next_kv();
334-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
333+
let kv = unsafe { kv.ok().unwrap_unchecked() };
335334
(kv.next_leaf_edge(), kv.into_kv())
336335
})
337336
}
@@ -344,7 +343,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Ed
344343
pub unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
345344
super::mem::replace(self, |leaf_edge| {
346345
let kv = leaf_edge.next_back_kv();
347-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
346+
let kv = unsafe { kv.ok().unwrap_unchecked() };
348347
(kv.next_back_leaf_edge(), kv.into_kv())
349348
})
350349
}
@@ -359,7 +358,7 @@ impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::E
359358
pub unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
360359
let kv = super::mem::replace(self, |leaf_edge| {
361360
let kv = leaf_edge.next_kv();
362-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
361+
let kv = unsafe { kv.ok().unwrap_unchecked() };
363362
(unsafe { ptr::read(&kv) }.next_leaf_edge(), kv)
364363
});
365364
// Doing this last is faster, according to benchmarks.
@@ -374,7 +373,7 @@ impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::E
374373
pub unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
375374
let kv = super::mem::replace(self, |leaf_edge| {
376375
let kv = leaf_edge.next_back_kv();
377-
let kv = unsafe { unwrap_unchecked(kv.ok()) };
376+
let kv = unsafe { kv.ok().unwrap_unchecked() };
378377
(unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv)
379378
});
380379
// Doing this last is faster, according to benchmarks.

library/alloc/src/collections/btree/remove.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::map::MIN_LEN;
22
use super::node::{marker, ForceResult::*, Handle, LeftOrRight::*, NodeRef};
3-
use super::unwrap_unchecked;
43

54
impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV> {
65
/// Removes a key-value pair from the tree, and returns that pair, as well as
@@ -77,12 +76,12 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
7776
// the element we were asked to remove. Prefer the left adjacent KV,
7877
// for the reasons listed in `choose_parent_kv`.
7978
let left_leaf_kv = self.left_edge().descend().last_leaf_edge().left_kv();
80-
let left_leaf_kv = unsafe { unwrap_unchecked(left_leaf_kv.ok()) };
79+
let left_leaf_kv = unsafe { left_leaf_kv.ok().unwrap_unchecked() };
8180
let (left_kv, left_hole) = left_leaf_kv.remove_leaf_kv(handle_emptied_internal_root);
8281

8382
// The internal node may have been stolen from or merged. Go back right
8483
// to find where the original KV ended up.
85-
let mut internal = unsafe { unwrap_unchecked(left_hole.next_kv().ok()) };
84+
let mut internal = unsafe { left_hole.next_kv().ok().unwrap_unchecked() };
8685
let old_kv = internal.replace_kv(left_kv.0, left_kv.1);
8786
let pos = internal.next_leaf_edge();
8887
(old_kv, pos)

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
#![feature(nll)]
112112
#![feature(nonnull_slice_from_raw_parts)]
113113
#![feature(auto_traits)]
114+
#![feature(option_result_unwrap_unchecked)]
114115
#![feature(or_patterns)]
115116
#![feature(pattern)]
116117
#![feature(ptr_internals)]

0 commit comments

Comments
 (0)