@@ -417,6 +417,26 @@ impl<T, A: Allocator> VecDeque<T, A> {
417
417
}
418
418
}
419
419
420
+ /// Append all values from `src` to `self`, wrapping around if needed.
421
+ /// Assumes capacity is sufficient.
422
+ #[ inline]
423
+ unsafe fn append_slice ( & mut self , src : & [ T ] ) {
424
+ debug_assert ! ( self . len( ) + src. len( ) + 1 <= self . cap( ) ) ;
425
+ let head_room = self . cap ( ) - self . head ;
426
+ if self . head < self . tail || src. len ( ) <= head_room {
427
+ unsafe {
428
+ ptr:: copy_nonoverlapping ( src. as_ptr ( ) , self . ptr ( ) . add ( self . head ) , src. len ( ) ) ;
429
+ }
430
+ } else {
431
+ let ( left, right) = src. split_at ( head_room) ;
432
+ unsafe {
433
+ ptr:: copy_nonoverlapping ( left. as_ptr ( ) , self . ptr ( ) . add ( self . head ) , left. len ( ) ) ;
434
+ ptr:: copy_nonoverlapping ( right. as_ptr ( ) , self . ptr ( ) , right. len ( ) ) ;
435
+ }
436
+ }
437
+ self . head = self . wrap_add ( self . head , src. len ( ) ) ;
438
+ }
439
+
420
440
/// Frobs the head and tail sections around to handle the fact that we
421
441
/// just reallocated. Unsafe because it trusts old_capacity.
422
442
#[ inline]
@@ -2088,8 +2108,14 @@ impl<T, A: Allocator> VecDeque<T, A> {
2088
2108
#[ inline]
2089
2109
#[ stable( feature = "append" , since = "1.4.0" ) ]
2090
2110
pub fn append ( & mut self , other : & mut Self ) {
2091
- // naive impl
2092
- self . extend ( other. drain ( ..) ) ;
2111
+ self . reserve ( other. len ( ) ) ;
2112
+ unsafe {
2113
+ let ( left, right) = other. as_slices ( ) ;
2114
+ self . append_slice ( left) ;
2115
+ self . append_slice ( right) ;
2116
+ }
2117
+ // Silently drop values in `other`.
2118
+ other. tail = other. head ;
2093
2119
}
2094
2120
2095
2121
/// Retains only the elements specified by the predicate.
0 commit comments