From ecaeb59f4eae86a0338f7b69b039eb116516b680 Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Fri, 4 Oct 2019 23:59:37 +0200 Subject: [PATCH] Deprecate unreachable function (#164) This is designed to merged after #162, as it deprecates a function that should be no longer necessary on newer Rust versions. --- lib.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib.rs b/lib.rs index 583b046..b8951ba 100644 --- a/lib.rs +++ b/lib.rs @@ -57,6 +57,7 @@ use std::borrow::{Borrow, BorrowMut}; use std::cmp; use std::fmt; use std::hash::{Hash, Hasher}; +use std::hint::unreachable_unchecked; #[cfg(feature = "std")] use std::io; use std::iter::{repeat, FromIterator, IntoIterator}; @@ -129,12 +130,11 @@ macro_rules! smallvec { /// 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. +/// Equivalent to `std::hint::unreachable_unchecked`. #[inline] +#[deprecated(note = "Use std::hint::unreachable_unchecked instead")] pub unsafe fn unreachable() -> ! { - enum Void {} - let x: &Void = mem::transmute(1usize); - match *x {} + unreachable_unchecked() } /// `panic!()` in debug builds, optimization hint in release. @@ -145,7 +145,7 @@ macro_rules! debug_unreachable { }; ($e:expr) => { if cfg!(not(debug_assertions)) { - unreachable(); + unreachable_unchecked(); } else { panic!($e); } @@ -780,7 +780,8 @@ impl SmallVec { 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.