@@ -202,10 +202,17 @@ impl<T> VecDeque<T> {
202202 len) ;
203203 }
204204
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.
207211 #[ inline]
208212 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+
209216 let dst_high_ptr = self . ptr ( ) . add ( self . head ) ;
210217 let dst_high_len = self . cap ( ) - self . head ;
211218
@@ -216,6 +223,7 @@ impl<T> VecDeque<T> {
216223 ptr:: copy_nonoverlapping ( src_low. as_ptr ( ) , self . ptr ( ) , src_low. len ( ) ) ;
217224
218225 self . head = self . wrap_add ( self . head , src. len ( ) ) ;
226+ debug_assert ! ( self . len( ) == expected_new_len) ;
219227 }
220228
221229 /// Copies a potentially wrapping block of memory len long from src to dest.
@@ -1850,17 +1858,21 @@ impl<T> VecDeque<T> {
18501858 #[ inline]
18511859 #[ stable( feature = "append" , since = "1.4.0" ) ]
18521860 pub fn append ( & mut self , other : & mut Self ) {
1853- // Guarantees there is space in `self` for `other
1854- self . reserve ( other. len ( ) ) ;
1855-
18561861 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 ( ) ) ;
18611864
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+ }
18641876 }
18651877
18661878 /// Retains only the elements specified by the predicate.
0 commit comments