@@ -1156,7 +1156,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
11561156 #[ inline]
11571157 #[ stable( feature = "deque_extras_15" , since = "1.5.0" ) ]
11581158 pub fn as_slices ( & self ) -> ( & [ T ] , & [ T ] ) {
1159- let ( a_range, b_range) = self . slice_ranges ( ..) ;
1159+ let ( a_range, b_range) = self . slice_ranges ( .., self . len ) ;
11601160 // SAFETY: `slice_ranges` always returns valid ranges into
11611161 // the physical buffer.
11621162 unsafe { ( & * self . buffer_range ( a_range) , & * self . buffer_range ( b_range) ) }
@@ -1190,7 +1190,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
11901190 #[ inline]
11911191 #[ stable( feature = "deque_extras_15" , since = "1.5.0" ) ]
11921192 pub fn as_mut_slices ( & mut self ) -> ( & mut [ T ] , & mut [ T ] ) {
1193- let ( a_range, b_range) = self . slice_ranges ( ..) ;
1193+ let ( a_range, b_range) = self . slice_ranges ( .., self . len ) ;
11941194 // SAFETY: `slice_ranges` always returns valid ranges into
11951195 // the physical buffer.
11961196 unsafe { ( & mut * self . buffer_range ( a_range) , & mut * self . buffer_range ( b_range) ) }
@@ -1232,19 +1232,28 @@ impl<T, A: Allocator> VecDeque<T, A> {
12321232
12331233 /// Given a range into the logical buffer of the deque, this function
12341234 /// return two ranges into the physical buffer that correspond to
1235- /// the given range.
1236- fn slice_ranges < R > ( & self , range : R ) -> ( Range < usize > , Range < usize > )
1235+ /// the given range. The `len` parameter should usually just be `self.len`;
1236+ /// the reason it's passed explicitly is that if the deque is wrapped in
1237+ /// a `Drain`, then `self.len` is not actually the length of the deque.
1238+ ///
1239+ /// # Safety
1240+ ///
1241+ /// This function is always safe to call. For the resulting ranges to be valid
1242+ /// ranges into the physical buffer, the caller must ensure that the result of
1243+ /// calling `slice::range(range, ..len)` represents a valid range into the
1244+ /// logical buffer, and that all elements in that range are initialized.
1245+ fn slice_ranges < R > ( & self , range : R , len : usize ) -> ( Range < usize > , Range < usize > )
12371246 where
12381247 R : RangeBounds < usize > ,
12391248 {
1240- let Range { start, end } = slice:: range ( range, ..self . len ) ;
1249+ let Range { start, end } = slice:: range ( range, ..len) ;
12411250 let len = end - start;
12421251
12431252 if len == 0 {
12441253 ( 0 ..0 , 0 ..0 )
12451254 } else {
1246- // `slice::range` guarantees that `start <= end <= self. len`.
1247- // because `len != 0`, we know that `start < end`, so `start < self. len`
1255+ // `slice::range` guarantees that `start <= end <= len`.
1256+ // because `len != 0`, we know that `start < end`, so `start < len`
12481257 // and the indexing is valid.
12491258 let wrapped_start = self . to_physical_idx ( start) ;
12501259
@@ -1290,7 +1299,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
12901299 where
12911300 R : RangeBounds < usize > ,
12921301 {
1293- let ( a_range, b_range) = self . slice_ranges ( range) ;
1302+ let ( a_range, b_range) = self . slice_ranges ( range, self . len ) ;
12941303 // SAFETY: The ranges returned by `slice_ranges`
12951304 // are valid ranges into the physical buffer, so
12961305 // it's ok to pass them to `buffer_range` and
@@ -1330,7 +1339,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
13301339 where
13311340 R : RangeBounds < usize > ,
13321341 {
1333- let ( a_range, b_range) = self . slice_ranges ( range) ;
1342+ let ( a_range, b_range) = self . slice_ranges ( range, self . len ) ;
13341343 // SAFETY: The ranges returned by `slice_ranges`
13351344 // are valid ranges into the physical buffer, so
13361345 // it's ok to pass them to `buffer_range` and
0 commit comments