Skip to content

Commit

Permalink
Auto merge of #140 - mbrubeck:unreachable, r=emilio
Browse files Browse the repository at this point in the history
Remove dependency on unmaintained 'unreachable' crate

Fixes #128 by inlining the tiny amount of code we use from `unreachable` and its dependency `void`.  Eventually this can be replaced with `std::hint::unrechable_unchecked` but this will require bumping our minumum supported Rust version.

This will prevent build breakage from users building with broken versions of the `void` crate, as in crossbeam-rs/crossbeam#312.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/140)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Feb 16, 2019
2 parents a775b5f + 6510c42 commit dfd78d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smallvec"
version = "0.6.8"
version = "0.6.9"
authors = ["Simon Sapin <simon.sapin@exyr.org>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/servo/rust-smallvec"
Expand All @@ -22,7 +22,6 @@ name = "smallvec"
path = "lib.rs"

[dependencies]
unreachable = "1.0.0"
serde = { version = "1", optional = true }

[dev_dependencies]
Expand Down
18 changes: 13 additions & 5 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ use alloc::vec::Vec;
#[cfg(feature = "serde")]
extern crate serde;

extern crate unreachable;
use unreachable::UncheckedOptionExt;

#[cfg(not(feature = "std"))]
mod std {
pub use core::*;
Expand Down Expand Up @@ -131,13 +128,24 @@ 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.
#[inline]
pub unsafe fn unreachable() -> ! {
enum Void {}
let x: &Void = mem::transmute(1usize);
match *x {}
}

/// `panic!()` in debug builds, optimization hint in release.
#[cfg(not(feature = "union"))]
macro_rules! debug_unreachable {
() => { debug_unreachable!("entered unreachable code") };
($e:expr) => {
if cfg!(not(debug_assertions)) {
unreachable::unreachable();
unreachable();
} else {
panic!($e);
}
Expand Down Expand Up @@ -758,7 +766,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);
unsafe { self.pop().unchecked_unwrap() }
self.pop().unwrap_or_else(|| unsafe { unreachable() })
}

/// Remove all elements from the vector.
Expand Down

0 comments on commit dfd78d1

Please sign in to comment.