Skip to content

Commit 85440ce

Browse files
committed
Make use of core::hint::unreachable_unchecked
1 parent ef7c328 commit 85440ce

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ macro_rules! debug_unreachable {
66
};
77
($e:expr) => {
88
if cfg!(not(debug_assertions)) {
9-
crate::utils::unreachable();
9+
core::hint::unreachable_unchecked();
1010
} else {
1111
panic!($e);
1212
}

src/small_vec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::small_vec_visitor::SmallVecVisitor;
33
#[cfg(feature = "specialization")]
44
use crate::spec_from::SpecFrom;
5-
use crate::utils::{deallocate, unreachable};
5+
use crate::utils::deallocate;
66
#[cfg(not(feature = "const_generics"))]
77
use crate::Array;
88
use crate::{
@@ -13,6 +13,7 @@ use core::{
1313
cmp::{Eq, Ord, Ordering, PartialOrd},
1414
fmt::{self, Debug},
1515
hash::{Hash, Hasher},
16+
hint::unreachable_unchecked,
1617
iter::{repeat, FromIterator},
1718
mem::{self, MaybeUninit},
1819
ops::{Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo},
@@ -435,7 +436,7 @@ impl<A: Array> SmallVec<A> {
435436
pub fn swap_remove(&mut self, index: usize) -> A::Item {
436437
let len = self.len();
437438
self.swap(len - 1, index);
438-
self.pop().unwrap_or_else(|| unsafe { unreachable() })
439+
self.pop().unwrap_or_else(|| unsafe { unreachable_unchecked() })
439440
}
440441

441442
/// Remove all elements from the vector.

src/utils.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
use core::mem;
2-
31
pub unsafe fn deallocate<T>(ptr: *mut T, capacity: usize) {
42
let _vec: Vec<T> = Vec::from_raw_parts(ptr, 0, capacity);
53
// Let it drop.
6-
}
7-
8-
/// Hint to the optimizer that any code path which calls this function is
9-
/// statically unreachable and can be removed.
10-
///
11-
/// Equivalent to `std::hint::unreachable_unchecked` but works in older versions of Rust.
12-
#[inline]
13-
pub unsafe fn unreachable() -> ! {
14-
enum Void {}
15-
let x: &Void = mem::transmute(1usize);
16-
match *x {}
17-
}
4+
}

0 commit comments

Comments
 (0)