@@ -198,7 +198,13 @@ impl<T: Clone> Vec<T> {
198
198
#[ inline]
199
199
pub fn from_slice ( values : & [ T ] ) -> Vec < T > {
200
200
let mut vector = Vec :: with_capacity ( values. len ( ) ) ;
201
- vector. push_all ( values) ;
201
+
202
+ // Directly call `unsafe_push_all_clone` so we can skip a call to
203
+ // `reserve_addtional`.
204
+ unsafe {
205
+ unsafe_push_all_clone ( & mut vector, values) ;
206
+ }
207
+
202
208
vector
203
209
}
204
210
@@ -240,8 +246,9 @@ impl<T: Clone> Vec<T> {
240
246
/// ```
241
247
#[ inline]
242
248
pub fn push_all ( & mut self , other : & [ T ] ) {
249
+ self . reserve_additional ( other. len ( ) ) ;
250
+
243
251
unsafe {
244
- self . reserve_additional ( other. len ( ) ) ;
245
252
unsafe_push_all_clone ( self , other)
246
253
}
247
254
}
@@ -323,31 +330,24 @@ impl<T: Clone> Vec<T> {
323
330
#[ unstable]
324
331
impl < T : Clone > Clone for Vec < T > {
325
332
fn clone ( & self ) -> Vec < T > {
326
- unsafe {
327
- let mut vector = Vec :: with_capacity ( self . len ) ;
328
- unsafe_push_all_clone ( & mut vector, self . as_slice ( ) ) ;
329
- vector
330
- }
333
+ Vec :: from_slice ( self . as_slice ( ) )
331
334
}
332
335
333
336
fn clone_from ( & mut self , other : & Vec < T > ) {
334
- unsafe {
335
- // drop anything in self that will not be overwritten
336
- if self . len ( ) > other. len ( ) {
337
- self . truncate ( other. len ( ) )
338
- }
339
-
340
- // reuse the contained values' allocations/resources.
341
- for ( place, thing) in self . mut_iter ( ) . zip ( other. iter ( ) ) {
342
- place. clone_from ( thing)
343
- }
337
+ // drop anything in self that will not be overwritten
338
+ if self . len ( ) > other. len ( ) {
339
+ self . truncate ( other. len ( ) )
340
+ }
344
341
345
- // self.len <= other.len due to the truncate above, so the
346
- // slice here is always in-bounds.
347
- let slice = other. slice_from ( self . len ( ) ) ;
348
- self . reserve_additional ( slice. len ( ) ) ;
349
- unsafe_push_all_clone ( self , slice)
342
+ // reuse the contained values' allocations/resources.
343
+ for ( place, thing) in self . mut_iter ( ) . zip ( other. iter ( ) ) {
344
+ place. clone_from ( thing)
350
345
}
346
+
347
+ // self.len <= other.len due to the truncate above, so the
348
+ // slice here is always in-bounds.
349
+ let slice = other. slice_from ( self . len ( ) ) ;
350
+ self . push_all ( slice) ;
351
351
}
352
352
}
353
353
0 commit comments