@@ -31,8 +31,8 @@ use crate::HeaderVec;
3131pub struct Drain < ' a , H , T > {
3232 /// Index of tail to preserve
3333 pub ( super ) tail_start : usize ,
34- /// Length of tail
35- pub ( super ) tail_len : usize ,
34+ /// End index of tail to preserve
35+ pub ( super ) tail_end : usize ,
3636 /// Current remaining range to remove
3737 pub ( super ) iter : slice:: Iter < ' a , T > ,
3838 pub ( super ) vec : NonNull < HeaderVec < H , T > > ,
@@ -115,13 +115,17 @@ impl<H, T> Drain<'_, H, T> {
115115 if tail != ( start + unyielded_len) {
116116 let src = source_vec. as_ptr ( ) . add ( tail) ;
117117 let dst = start_ptr. add ( unyielded_len) ;
118- ptr:: copy ( src, dst, this. tail_len ) ;
118+ ptr:: copy ( src, dst, this. tail_len ( ) ) ;
119119 }
120120 }
121121
122- source_vec. set_len ( start + unyielded_len + this. tail_len ) ;
122+ source_vec. set_len ( start + unyielded_len + this. tail_len ( ) ) ;
123123 }
124124 }
125+
126+ pub ( crate ) fn tail_len ( & self ) -> usize {
127+ self . tail_end - self . tail_start
128+ }
125129}
126130
127131impl < H , T > AsRef < [ T ] > for Drain < ' _ , H , T > {
@@ -164,7 +168,7 @@ impl<H, T> Drop for Drain<'_, H, T> {
164168
165169 impl < H , T > Drop for DropGuard < ' _ , ' _ , H , T > {
166170 fn drop ( & mut self ) {
167- if self . 0 . tail_len > 0 {
171+ if self . 0 . tail_len ( ) > 0 {
168172 unsafe {
169173 let source_vec = self . 0 . vec . as_mut ( ) ;
170174 // memmove back untouched tail, update to new length
@@ -173,9 +177,9 @@ impl<H, T> Drop for Drain<'_, H, T> {
173177 if tail != start {
174178 let src = source_vec. as_ptr ( ) . add ( tail) ;
175179 let dst = source_vec. as_mut_ptr ( ) . add ( start) ;
176- ptr:: copy ( src, dst, self . 0 . tail_len ) ;
180+ ptr:: copy ( src, dst, self . 0 . tail_len ( ) ) ;
177181 }
178- source_vec. set_len ( start + self . 0 . tail_len ) ;
182+ source_vec. set_len ( start + self . 0 . tail_len ( ) ) ;
179183 }
180184 }
181185 }
@@ -193,8 +197,8 @@ impl<H, T> Drop for Drain<'_, H, T> {
193197 unsafe {
194198 let vec = vec. as_mut ( ) ;
195199 let old_len = vec. len ( ) ;
196- vec. set_len ( old_len + drop_len + self . tail_len ) ;
197- vec. truncate ( old_len + self . tail_len ) ;
200+ vec. set_len ( old_len + drop_len + self . tail_len ( ) ) ;
201+ vec. truncate ( old_len + self . tail_len ( ) ) ;
198202 }
199203
200204 return ;
0 commit comments