Skip to content

Commit

Permalink
Make use of core::hint::unreachable_unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Jun 16, 2019
1 parent 9e5a961 commit 71f79c4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! debug_unreachable {
};
($e:expr) => {
if cfg!(not(debug_assertions)) {
crate::utils::unreachable();
core::hint::unreachable_unchecked();
} else {
panic!($e);
}
Expand Down
5 changes: 3 additions & 2 deletions src/small_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::small_vec_visitor::SmallVecVisitor;
#[cfg(feature = "specialization")]
use crate::spec_from::SpecFrom;
use crate::utils::{deallocate, unreachable};
use crate::utils::deallocate;
#[cfg(not(feature = "const_generics"))]
use crate::Array;
use crate::{
Expand All @@ -13,6 +13,7 @@ use core::{
cmp::{Eq, Ord, Ordering, PartialOrd},
fmt::{self, Debug},
hash::{Hash, Hasher},
hint::unreachable_unchecked,
iter::{repeat, FromIterator},
mem::{self, MaybeUninit},
ops::{Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo},
Expand Down Expand Up @@ -435,7 +436,7 @@ impl<A: Array> SmallVec<A> {
pub fn swap_remove(&mut self, index: usize) -> A::Item {
let len = self.len();
self.swap(len - 1, index);
self.pop().unwrap_or_else(|| unsafe { unreachable() })
self.pop().unwrap_or_else(|| unsafe { unreachable_unchecked() })
}

/// Remove all elements from the vector.
Expand Down
15 changes: 1 addition & 14 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
use core::mem;

pub unsafe fn deallocate<T>(ptr: *mut T, capacity: usize) {
let _vec: Vec<T> = Vec::from_raw_parts(ptr, 0, capacity);
// Let it drop.
}

/// Hint to the optimizer that any code path which calls this function is
/// statically unreachable and can be removed.
///
/// Equivalent to `std::hint::unreachable_unchecked` but works in older versions of Rust.
#[inline]
pub unsafe fn unreachable() -> ! {
enum Void {}
let x: &Void = mem::transmute(1usize);
match *x {}
}
}

0 comments on commit 71f79c4

Please sign in to comment.