diff --git a/lib.rs b/lib.rs index 583b046..59e3304 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}; @@ -66,7 +67,7 @@ use std::mem; use std::mem::MaybeUninit; use std::ops; use std::ptr; -use std::slice; +use std::slice::{self, SliceIndex}; /// Creates a [`SmallVec`] containing the arguments. /// @@ -126,17 +127,6 @@ 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 { @@ -145,61 +135,13 @@ macro_rules! debug_unreachable { }; ($e:expr) => { if cfg!(not(debug_assertions)) { - unreachable(); + unreachable_unchecked(); } else { panic!($e); } }; } -/// Common operations implemented by both `Vec` and `SmallVec`. -/// -/// This can be used to write generic code that works with both `Vec` and `SmallVec`. -/// -/// ## Example -/// -/// ```rust -/// use smallvec::{VecLike, SmallVec}; -/// -/// fn initialize>(v: &mut V) { -/// for i in 0..5 { -/// v.push(i); -/// } -/// } -/// -/// let mut vec = Vec::new(); -/// initialize(&mut vec); -/// -/// let mut small_vec = SmallVec::<[u8; 8]>::new(); -/// initialize(&mut small_vec); -/// ``` -#[deprecated(note = "Use `Extend` and `Deref<[T]>` instead")] -pub trait VecLike: - ops::Index - + ops::IndexMut - + ops::Index, Output = [T]> - + ops::IndexMut> - + ops::Index, Output = [T]> - + ops::IndexMut> - + ops::Index, Output = [T]> - + ops::IndexMut> - + ops::Index - + ops::IndexMut - + ops::DerefMut - + Extend -{ - /// Append an element to the vector. - fn push(&mut self, value: T); -} - -#[allow(deprecated)] -impl VecLike for Vec { - #[inline] - fn push(&mut self, value: T) { - Vec::push(self, value); - } -} - /// Trait to be implemented by a collection that can be extended from a slice /// /// ## Example @@ -780,7 +722,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. @@ -1332,30 +1275,19 @@ impl From for SmallVec { } } -macro_rules! impl_index { - ($index_type: ty, $output_type: ty) => { - impl ops::Index<$index_type> for SmallVec { - type Output = $output_type; - #[inline] - fn index(&self, index: $index_type) -> &$output_type { - &(&**self)[index] - } - } +impl> ops::Index for SmallVec { + type Output = I::Output; - impl ops::IndexMut<$index_type> for SmallVec { - #[inline] - fn index_mut(&mut self, index: $index_type) -> &mut $output_type { - &mut (&mut **self)[index] - } - } - }; + fn index(&self, index: I) -> &I::Output { + &(**self)[index] + } } -impl_index!(usize, A::Item); -impl_index!(ops::Range, [A::Item]); -impl_index!(ops::RangeFrom, [A::Item]); -impl_index!(ops::RangeTo, [A::Item]); -impl_index!(ops::RangeFull, [A::Item]); +impl> ops::IndexMut for SmallVec { + fn index_mut(&mut self, index: I) -> &mut I::Output { + &mut (&mut **self)[index] + } +} impl ExtendFromSlice for SmallVec where @@ -1366,14 +1298,6 @@ where } } -#[allow(deprecated)] -impl VecLike for SmallVec { - #[inline] - fn push(&mut self, value: A::Item) { - SmallVec::push(self, value); - } -} - impl FromIterator for SmallVec { fn from_iter>(iterable: I) -> SmallVec { let mut v = SmallVec::new(); @@ -2236,23 +2160,6 @@ mod tests { assert_eq!(vec.drain().len(), 3); } - #[test] - #[allow(deprecated)] - fn veclike_deref_slice() { - use super::VecLike; - - fn test>(vec: &mut T) { - assert!(!vec.is_empty()); - assert_eq!(vec.len(), 3); - - vec.sort(); - assert_eq!(&vec[..], [1, 2, 3]); - } - - let mut vec = SmallVec::<[i32; 2]>::from(&[3, 1, 2][..]); - test(&mut vec); - } - #[test] fn shrink_to_fit_unspill() { let mut vec = SmallVec::<[u8; 2]>::from_iter(0..3);