Skip to content

Commit c42d26f

Browse files
committed
replace Drain::tail_len with tail_end
* add tail_len() method upcoming splice needs this, otherwise incrementing the tail_start would require decrementing tail_len as well.
1 parent 76163d6 commit c42d26f

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/drain.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use crate::HeaderVec;
3131
pub 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

127131
impl<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;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl<H, T> HeaderVec<H, T> {
855855
let range_slice = slice::from_raw_parts(self.as_ptr().add(start), end - start);
856856
Drain {
857857
tail_start: end,
858-
tail_len: len - end,
858+
tail_end: len,
859859
iter: range_slice.iter(),
860860
vec: NonNull::from(self),
861861
}

0 commit comments

Comments
 (0)