Skip to content

Commit dfd78d1

Browse files
author
bors-servo
authored
Auto merge of #140 - mbrubeck:unreachable, r=emilio
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 -->
2 parents a775b5f + 6510c42 commit dfd78d1

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smallvec"
3-
version = "0.6.8"
3+
version = "0.6.9"
44
authors = ["Simon Sapin <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
repository = "https://github.com/servo/rust-smallvec"
@@ -22,7 +22,6 @@ name = "smallvec"
2222
path = "lib.rs"
2323

2424
[dependencies]
25-
unreachable = "1.0.0"
2625
serde = { version = "1", optional = true }
2726

2827
[dev_dependencies]

lib.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ use alloc::vec::Vec;
4646
#[cfg(feature = "serde")]
4747
extern crate serde;
4848

49-
extern crate unreachable;
50-
use unreachable::UncheckedOptionExt;
51-
5249
#[cfg(not(feature = "std"))]
5350
mod std {
5451
pub use core::*;
@@ -131,13 +128,24 @@ macro_rules! smallvec {
131128
});
132129
}
133130

131+
/// Hint to the optimizer that any code path which calls this function is
132+
/// statically unreachable and can be removed.
133+
///
134+
/// Equivalent to `std::hint::unreachable_unchecked` but works in older versions of Rust.
135+
#[inline]
136+
pub unsafe fn unreachable() -> ! {
137+
enum Void {}
138+
let x: &Void = mem::transmute(1usize);
139+
match *x {}
140+
}
141+
134142
/// `panic!()` in debug builds, optimization hint in release.
135143
#[cfg(not(feature = "union"))]
136144
macro_rules! debug_unreachable {
137145
() => { debug_unreachable!("entered unreachable code") };
138146
($e:expr) => {
139147
if cfg!(not(debug_assertions)) {
140-
unreachable::unreachable();
148+
unreachable();
141149
} else {
142150
panic!($e);
143151
}
@@ -758,7 +766,7 @@ impl<A: Array> SmallVec<A> {
758766
pub fn swap_remove(&mut self, index: usize) -> A::Item {
759767
let len = self.len();
760768
self.swap(len - 1, index);
761-
unsafe { self.pop().unchecked_unwrap() }
769+
self.pop().unwrap_or_else(|| unsafe { unreachable() })
762770
}
763771

764772
/// Remove all elements from the vector.

0 commit comments

Comments
 (0)