@@ -202,10 +202,17 @@ impl<T> VecDeque<T> {
202
202
len) ;
203
203
}
204
204
205
- /// Copies all values from `src` to `self`, wrapping around if needed.
206
- /// Assumes capacity is sufficient.
205
+ /// Copies all values from `src` to the back of `self`, wrapping around if needed.
206
+ ///
207
+ /// # Safety
208
+ ///
209
+ /// The capacity must be sufficient to hold self.len() + src.len() elements.
210
+ /// If so, this function never panics.
207
211
#[ inline]
208
212
unsafe fn copy_slice ( & mut self , src : & [ T ] ) {
213
+ let expected_new_len = self . len ( ) + src. len ( ) ;
214
+ debug_assert ! ( self . capacity( ) >= expected_new_len) ;
215
+
209
216
let dst_high_ptr = self . ptr ( ) . add ( self . head ) ;
210
217
let dst_high_len = self . cap ( ) - self . head ;
211
218
@@ -216,6 +223,7 @@ impl<T> VecDeque<T> {
216
223
ptr:: copy_nonoverlapping ( src_low. as_ptr ( ) , self . ptr ( ) , src_low. len ( ) ) ;
217
224
218
225
self . head = self . wrap_add ( self . head , src. len ( ) ) ;
226
+ debug_assert ! ( self . len( ) == expected_new_len) ;
219
227
}
220
228
221
229
/// Copies a potentially wrapping block of memory len long from src to dest.
@@ -1850,17 +1858,21 @@ impl<T> VecDeque<T> {
1850
1858
#[ inline]
1851
1859
#[ stable( feature = "append" , since = "1.4.0" ) ]
1852
1860
pub fn append ( & mut self , other : & mut Self ) {
1853
- // Guarantees there is space in `self` for `other
1854
- self . reserve ( other. len ( ) ) ;
1855
-
1856
1861
unsafe {
1857
- let ( src_high, src_low) = other. as_slices ( ) ;
1858
- self . copy_slice ( src_low) ;
1859
- self . copy_slice ( src_high) ;
1860
- }
1862
+ // Guarantees there is space in `self` for `other`.
1863
+ self . reserve ( other. len ( ) ) ;
1861
1864
1862
- // Some values now exist in both `other` and `self` but are made inaccessible in `other`.
1863
- other. tail = other. head ;
1865
+ {
1866
+ let ( src_high, src_low) = other. as_slices ( ) ;
1867
+
1868
+ // This is only safe because copy_slice never panics when capacity is sufficient.
1869
+ self . copy_slice ( src_low) ;
1870
+ self . copy_slice ( src_high) ;
1871
+ }
1872
+
1873
+ // Some values now exist in both `other` and `self` but are made inaccessible in `other`.
1874
+ other. tail = other. head ;
1875
+ }
1864
1876
}
1865
1877
1866
1878
/// Retains only the elements specified by the predicate.
0 commit comments