Skip to content

Commit 924683a

Browse files
author
bors-servo
authored
Auto merge of #169 - servo:rollup, r=emilio
Rebase of various PRs. This rebases #164, #165 and #166, and addresses my review comments in the first (removing the function given there's an stable alternative in our new minimum required rust version). Closes #164 Closes #165 Closes #166
2 parents 690d65e + b3fbc21 commit 924683a

File tree

1 file changed

+15
-108
lines changed

1 file changed

+15
-108
lines changed

lib.rs

+15-108
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use std::borrow::{Borrow, BorrowMut};
5757
use std::cmp;
5858
use std::fmt;
5959
use std::hash::{Hash, Hasher};
60+
use std::hint::unreachable_unchecked;
6061
#[cfg(feature = "std")]
6162
use std::io;
6263
use std::iter::{repeat, FromIterator, IntoIterator};
@@ -66,7 +67,7 @@ use std::mem;
6667
use std::mem::MaybeUninit;
6768
use std::ops;
6869
use std::ptr;
69-
use std::slice;
70+
use std::slice::{self, SliceIndex};
7071

7172
/// Creates a [`SmallVec`] containing the arguments.
7273
///
@@ -126,17 +127,6 @@ macro_rules! smallvec {
126127
});
127128
}
128129

129-
/// Hint to the optimizer that any code path which calls this function is
130-
/// statically unreachable and can be removed.
131-
///
132-
/// Equivalent to `std::hint::unreachable_unchecked` but works in older versions of Rust.
133-
#[inline]
134-
pub unsafe fn unreachable() -> ! {
135-
enum Void {}
136-
let x: &Void = mem::transmute(1usize);
137-
match *x {}
138-
}
139-
140130
/// `panic!()` in debug builds, optimization hint in release.
141131
#[cfg(not(feature = "union"))]
142132
macro_rules! debug_unreachable {
@@ -145,61 +135,13 @@ macro_rules! debug_unreachable {
145135
};
146136
($e:expr) => {
147137
if cfg!(not(debug_assertions)) {
148-
unreachable();
138+
unreachable_unchecked();
149139
} else {
150140
panic!($e);
151141
}
152142
};
153143
}
154144

155-
/// Common operations implemented by both `Vec` and `SmallVec`.
156-
///
157-
/// This can be used to write generic code that works with both `Vec` and `SmallVec`.
158-
///
159-
/// ## Example
160-
///
161-
/// ```rust
162-
/// use smallvec::{VecLike, SmallVec};
163-
///
164-
/// fn initialize<V: VecLike<u8>>(v: &mut V) {
165-
/// for i in 0..5 {
166-
/// v.push(i);
167-
/// }
168-
/// }
169-
///
170-
/// let mut vec = Vec::new();
171-
/// initialize(&mut vec);
172-
///
173-
/// let mut small_vec = SmallVec::<[u8; 8]>::new();
174-
/// initialize(&mut small_vec);
175-
/// ```
176-
#[deprecated(note = "Use `Extend` and `Deref<[T]>` instead")]
177-
pub trait VecLike<T>:
178-
ops::Index<usize, Output = T>
179-
+ ops::IndexMut<usize>
180-
+ ops::Index<ops::Range<usize>, Output = [T]>
181-
+ ops::IndexMut<ops::Range<usize>>
182-
+ ops::Index<ops::RangeFrom<usize>, Output = [T]>
183-
+ ops::IndexMut<ops::RangeFrom<usize>>
184-
+ ops::Index<ops::RangeTo<usize>, Output = [T]>
185-
+ ops::IndexMut<ops::RangeTo<usize>>
186-
+ ops::Index<ops::RangeFull, Output = [T]>
187-
+ ops::IndexMut<ops::RangeFull>
188-
+ ops::DerefMut<Target = [T]>
189-
+ Extend<T>
190-
{
191-
/// Append an element to the vector.
192-
fn push(&mut self, value: T);
193-
}
194-
195-
#[allow(deprecated)]
196-
impl<T> VecLike<T> for Vec<T> {
197-
#[inline]
198-
fn push(&mut self, value: T) {
199-
Vec::push(self, value);
200-
}
201-
}
202-
203145
/// Trait to be implemented by a collection that can be extended from a slice
204146
///
205147
/// ## Example
@@ -780,7 +722,8 @@ impl<A: Array> SmallVec<A> {
780722
pub fn swap_remove(&mut self, index: usize) -> A::Item {
781723
let len = self.len();
782724
self.swap(len - 1, index);
783-
self.pop().unwrap_or_else(|| unsafe { unreachable() })
725+
self.pop()
726+
.unwrap_or_else(|| unsafe { unreachable_unchecked() })
784727
}
785728

786729
/// Remove all elements from the vector.
@@ -1332,30 +1275,19 @@ impl<A: Array> From<A> for SmallVec<A> {
13321275
}
13331276
}
13341277

1335-
macro_rules! impl_index {
1336-
($index_type: ty, $output_type: ty) => {
1337-
impl<A: Array> ops::Index<$index_type> for SmallVec<A> {
1338-
type Output = $output_type;
1339-
#[inline]
1340-
fn index(&self, index: $index_type) -> &$output_type {
1341-
&(&**self)[index]
1342-
}
1343-
}
1278+
impl<A: Array, I: SliceIndex<[A::Item]>> ops::Index<I> for SmallVec<A> {
1279+
type Output = I::Output;
13441280

1345-
impl<A: Array> ops::IndexMut<$index_type> for SmallVec<A> {
1346-
#[inline]
1347-
fn index_mut(&mut self, index: $index_type) -> &mut $output_type {
1348-
&mut (&mut **self)[index]
1349-
}
1350-
}
1351-
};
1281+
fn index(&self, index: I) -> &I::Output {
1282+
&(**self)[index]
1283+
}
13521284
}
13531285

1354-
impl_index!(usize, A::Item);
1355-
impl_index!(ops::Range<usize>, [A::Item]);
1356-
impl_index!(ops::RangeFrom<usize>, [A::Item]);
1357-
impl_index!(ops::RangeTo<usize>, [A::Item]);
1358-
impl_index!(ops::RangeFull, [A::Item]);
1286+
impl<A: Array, I: SliceIndex<[A::Item]>> ops::IndexMut<I> for SmallVec<A> {
1287+
fn index_mut(&mut self, index: I) -> &mut I::Output {
1288+
&mut (&mut **self)[index]
1289+
}
1290+
}
13591291

13601292
impl<A: Array> ExtendFromSlice<A::Item> for SmallVec<A>
13611293
where
@@ -1366,14 +1298,6 @@ where
13661298
}
13671299
}
13681300

1369-
#[allow(deprecated)]
1370-
impl<A: Array> VecLike<A::Item> for SmallVec<A> {
1371-
#[inline]
1372-
fn push(&mut self, value: A::Item) {
1373-
SmallVec::push(self, value);
1374-
}
1375-
}
1376-
13771301
impl<A: Array> FromIterator<A::Item> for SmallVec<A> {
13781302
fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> {
13791303
let mut v = SmallVec::new();
@@ -2236,23 +2160,6 @@ mod tests {
22362160
assert_eq!(vec.drain().len(), 3);
22372161
}
22382162

2239-
#[test]
2240-
#[allow(deprecated)]
2241-
fn veclike_deref_slice() {
2242-
use super::VecLike;
2243-
2244-
fn test<T: VecLike<i32>>(vec: &mut T) {
2245-
assert!(!vec.is_empty());
2246-
assert_eq!(vec.len(), 3);
2247-
2248-
vec.sort();
2249-
assert_eq!(&vec[..], [1, 2, 3]);
2250-
}
2251-
2252-
let mut vec = SmallVec::<[i32; 2]>::from(&[3, 1, 2][..]);
2253-
test(&mut vec);
2254-
}
2255-
22562163
#[test]
22572164
fn shrink_to_fit_unspill() {
22582165
let mut vec = SmallVec::<[u8; 2]>::from_iter(0..3);

0 commit comments

Comments
 (0)