@@ -14,8 +14,7 @@ use core::fmt;
14
14
use core:: hash:: { Hash , Hasher } ;
15
15
use core:: iter:: { once, repeat_with, FromIterator , FusedIterator } ;
16
16
use core:: mem:: { self , replace, ManuallyDrop } ;
17
- use core:: ops:: Bound :: { Excluded , Included , Unbounded } ;
18
- use core:: ops:: { Index , IndexMut , RangeBounds , Try } ;
17
+ use core:: ops:: { Index , IndexMut , Range , RangeBounds , Try } ;
19
18
use core:: ptr:: { self , NonNull } ;
20
19
use core:: slice;
21
20
@@ -1083,24 +1082,16 @@ impl<T> VecDeque<T> {
1083
1082
self . tail == self . head
1084
1083
}
1085
1084
1086
- fn range_start_end < R > ( & self , range : R ) -> ( usize , usize )
1085
+ fn range_tail_head < R > ( & self , range : R ) -> ( usize , usize )
1087
1086
where
1088
1087
R : RangeBounds < usize > ,
1089
1088
{
1090
- let len = self . len ( ) ;
1091
- let start = match range. start_bound ( ) {
1092
- Included ( & n) => n,
1093
- Excluded ( & n) => n + 1 ,
1094
- Unbounded => 0 ,
1095
- } ;
1096
- let end = match range. end_bound ( ) {
1097
- Included ( & n) => n + 1 ,
1098
- Excluded ( & n) => n,
1099
- Unbounded => len,
1100
- } ;
1101
- assert ! ( start <= end, "lower bound was too large" ) ;
1102
- assert ! ( end <= len, "upper bound was too large" ) ;
1103
- ( start, end)
1089
+ // SAFETY: This buffer is only used to check the range.
1090
+ let buffer = unsafe { slice:: from_raw_parts ( self . ptr ( ) , self . len ( ) ) } ;
1091
+ let Range { start, end } = buffer. check_range ( range) ;
1092
+ let tail = self . wrap_add ( self . tail , start) ;
1093
+ let head = self . wrap_add ( self . tail , end) ;
1094
+ ( tail, head)
1104
1095
}
1105
1096
1106
1097
/// Creates an iterator that covers the specified range in the `VecDeque`.
@@ -1131,9 +1122,7 @@ impl<T> VecDeque<T> {
1131
1122
where
1132
1123
R : RangeBounds < usize > ,
1133
1124
{
1134
- let ( start, end) = self . range_start_end ( range) ;
1135
- let tail = self . wrap_add ( self . tail , start) ;
1136
- let head = self . wrap_add ( self . tail , end) ;
1125
+ let ( tail, head) = self . range_tail_head ( range) ;
1137
1126
Iter {
1138
1127
tail,
1139
1128
head,
@@ -1174,9 +1163,7 @@ impl<T> VecDeque<T> {
1174
1163
where
1175
1164
R : RangeBounds < usize > ,
1176
1165
{
1177
- let ( start, end) = self . range_start_end ( range) ;
1178
- let tail = self . wrap_add ( self . tail , start) ;
1179
- let head = self . wrap_add ( self . tail , end) ;
1166
+ let ( tail, head) = self . range_tail_head ( range) ;
1180
1167
IterMut {
1181
1168
tail,
1182
1169
head,
@@ -1230,7 +1217,7 @@ impl<T> VecDeque<T> {
1230
1217
// When finished, the remaining data will be copied back to cover the hole,
1231
1218
// and the head/tail values will be restored correctly.
1232
1219
//
1233
- let ( start , end ) = self . range_start_end ( range) ;
1220
+ let ( drain_tail , drain_head ) = self . range_tail_head ( range) ;
1234
1221
1235
1222
// The deque's elements are parted into three segments:
1236
1223
// * self.tail -> drain_tail
@@ -1248,8 +1235,6 @@ impl<T> VecDeque<T> {
1248
1235
// T t h H
1249
1236
// [. . . o o x x o o . . .]
1250
1237
//
1251
- let drain_tail = self . wrap_add ( self . tail , start) ;
1252
- let drain_head = self . wrap_add ( self . tail , end) ;
1253
1238
let head = self . head ;
1254
1239
1255
1240
// "forget" about the values after the start of the drain until after
0 commit comments