@@ -57,6 +57,7 @@ use std::borrow::{Borrow, BorrowMut};
57
57
use std:: cmp;
58
58
use std:: fmt;
59
59
use std:: hash:: { Hash , Hasher } ;
60
+ use std:: hint:: unreachable_unchecked;
60
61
#[ cfg( feature = "std" ) ]
61
62
use std:: io;
62
63
use std:: iter:: { repeat, FromIterator , IntoIterator } ;
@@ -66,7 +67,7 @@ use std::mem;
66
67
use std:: mem:: MaybeUninit ;
67
68
use std:: ops;
68
69
use std:: ptr;
69
- use std:: slice;
70
+ use std:: slice:: { self , SliceIndex } ;
70
71
71
72
/// Creates a [`SmallVec`] containing the arguments.
72
73
///
@@ -126,17 +127,6 @@ macro_rules! smallvec {
126
127
} ) ;
127
128
}
128
129
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
-
140
130
/// `panic!()` in debug builds, optimization hint in release.
141
131
#[ cfg( not( feature = "union" ) ) ]
142
132
macro_rules! debug_unreachable {
@@ -145,61 +135,13 @@ macro_rules! debug_unreachable {
145
135
} ;
146
136
( $e: expr) => {
147
137
if cfg!( not( debug_assertions) ) {
148
- unreachable ( ) ;
138
+ unreachable_unchecked ( ) ;
149
139
} else {
150
140
panic!( $e) ;
151
141
}
152
142
} ;
153
143
}
154
144
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
-
203
145
/// Trait to be implemented by a collection that can be extended from a slice
204
146
///
205
147
/// ## Example
@@ -780,7 +722,8 @@ impl<A: Array> SmallVec<A> {
780
722
pub fn swap_remove ( & mut self , index : usize ) -> A :: Item {
781
723
let len = self . len ( ) ;
782
724
self . swap ( len - 1 , index) ;
783
- self . pop ( ) . unwrap_or_else ( || unsafe { unreachable ( ) } )
725
+ self . pop ( )
726
+ . unwrap_or_else ( || unsafe { unreachable_unchecked ( ) } )
784
727
}
785
728
786
729
/// Remove all elements from the vector.
@@ -1332,30 +1275,19 @@ impl<A: Array> From<A> for SmallVec<A> {
1332
1275
}
1333
1276
}
1334
1277
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 ;
1344
1280
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
+ }
1352
1284
}
1353
1285
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
+ }
1359
1291
1360
1292
impl < A : Array > ExtendFromSlice < A :: Item > for SmallVec < A >
1361
1293
where
@@ -1366,14 +1298,6 @@ where
1366
1298
}
1367
1299
}
1368
1300
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
-
1377
1301
impl < A : Array > FromIterator < A :: Item > for SmallVec < A > {
1378
1302
fn from_iter < I : IntoIterator < Item = A :: Item > > ( iterable : I ) -> SmallVec < A > {
1379
1303
let mut v = SmallVec :: new ( ) ;
@@ -2236,23 +2160,6 @@ mod tests {
2236
2160
assert_eq ! ( vec. drain( ) . len( ) , 3 ) ;
2237
2161
}
2238
2162
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
-
2256
2163
#[ test]
2257
2164
fn shrink_to_fit_unspill ( ) {
2258
2165
let mut vec = SmallVec :: < [ u8 ; 2 ] > :: from_iter ( 0 ..3 ) ;
0 commit comments