Skip to content

Commit 76a8049

Browse files
committed
Optimize btree's unwrap_unchecked
1 parent 4d43423 commit 76a8049

File tree

1 file changed

+10
-9
lines changed
  • library/alloc/src/collections/btree

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ trait Recover<Q: ?Sized> {
1313
fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
1414
}
1515

16+
/// Same purpose as `Option::unwrap` but doesn't always guarantee a panic
17+
/// if the option contains no value.
18+
/// SAFETY: the caller must ensure that the option contains a value.
1619
#[inline(always)]
1720
pub unsafe fn unwrap_unchecked<T>(val: Option<T>) -> T {
18-
val.unwrap_or_else(|| {
19-
if cfg!(debug_assertions) {
20-
panic!("'unchecked' unwrap on None in BTreeMap");
21-
} else {
22-
unsafe {
23-
core::intrinsics::unreachable();
24-
}
25-
}
26-
})
21+
if cfg!(debug_assertions) {
22+
val.expect("'unchecked' unwrap on None in BTreeMap")
23+
} else {
24+
val.unwrap()
25+
// val.unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() })
26+
// ...is considerably slower
27+
}
2728
}

0 commit comments

Comments
 (0)