@@ -198,7 +198,13 @@ impl<T: Clone> Vec<T> {
198198 #[ inline]
199199 pub fn from_slice ( values : & [ T ] ) -> Vec < T > {
200200 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+
202208 vector
203209 }
204210
@@ -240,8 +246,9 @@ impl<T: Clone> Vec<T> {
240246 /// ```
241247 #[ inline]
242248 pub fn push_all ( & mut self , other : & [ T ] ) {
249+ self . reserve_additional ( other. len ( ) ) ;
250+
243251 unsafe {
244- self . reserve_additional ( other. len ( ) ) ;
245252 unsafe_push_all_clone ( self , other)
246253 }
247254 }
@@ -323,31 +330,24 @@ impl<T: Clone> Vec<T> {
323330#[ unstable]
324331impl < T : Clone > Clone for Vec < T > {
325332 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 ( ) )
331334 }
332335
333336 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+ }
344341
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)
350345 }
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) ;
351351 }
352352}
353353
0 commit comments