@@ -14,8 +14,7 @@ use core::fmt;
1414use core:: hash:: { Hash , Hasher } ;
1515use core:: iter:: { once, repeat_with, FromIterator , FusedIterator } ;
1616use 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 } ;
1918use core:: ptr:: { self , NonNull } ;
2019use core:: slice;
2120
@@ -1083,24 +1082,16 @@ impl<T> VecDeque<T> {
10831082 self . tail == self . head
10841083 }
10851084
1086- fn range_start_end < R > ( & self , range : R ) -> ( usize , usize )
1085+ fn range_tail_head < R > ( & self , range : R ) -> ( usize , usize )
10871086 where
10881087 R : RangeBounds < usize > ,
10891088 {
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)
11041095 }
11051096
11061097 /// Creates an iterator that covers the specified range in the `VecDeque`.
@@ -1131,9 +1122,7 @@ impl<T> VecDeque<T> {
11311122 where
11321123 R : RangeBounds < usize > ,
11331124 {
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) ;
11371126 Iter {
11381127 tail,
11391128 head,
@@ -1174,9 +1163,7 @@ impl<T> VecDeque<T> {
11741163 where
11751164 R : RangeBounds < usize > ,
11761165 {
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) ;
11801167 IterMut {
11811168 tail,
11821169 head,
@@ -1230,7 +1217,7 @@ impl<T> VecDeque<T> {
12301217 // When finished, the remaining data will be copied back to cover the hole,
12311218 // and the head/tail values will be restored correctly.
12321219 //
1233- let ( start , end ) = self . range_start_end ( range) ;
1220+ let ( drain_tail , drain_head ) = self . range_tail_head ( range) ;
12341221
12351222 // The deque's elements are parted into three segments:
12361223 // * self.tail -> drain_tail
@@ -1248,8 +1235,6 @@ impl<T> VecDeque<T> {
12481235 // T t h H
12491236 // [. . . o o x x o o . . .]
12501237 //
1251- let drain_tail = self . wrap_add ( self . tail , start) ;
1252- let drain_head = self . wrap_add ( self . tail , end) ;
12531238 let head = self . head ;
12541239
12551240 // "forget" about the values after the start of the drain until after
0 commit comments