Skip to content

Commit fe9bc1b

Browse files
author
Lukas Markeffsky
committed
reduce amount of math
1 parent 3b791a4 commit fe9bc1b

File tree

1 file changed

+12
-14
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+12
-14
lines changed

library/alloc/src/collections/vec_deque/drain.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub struct Drain<
2727
drain_len: usize,
2828
// index into the logical array, not the physical one (always lies in [0..deque.len))
2929
idx: usize,
30-
// number of elements after the drain range
31-
tail_len: usize,
30+
// number of elements remaining after dropping the drain
31+
new_len: usize,
3232
remaining: usize,
3333
// Needed to make Drain covariant over T
3434
_marker: PhantomData<&'a T>,
@@ -41,12 +41,12 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
4141
drain_len: usize,
4242
) -> Self {
4343
let orig_len = mem::replace(&mut deque.len, drain_start);
44-
let tail_len = orig_len - drain_start - drain_len;
44+
let new_len = orig_len - drain_len;
4545
Drain {
4646
deque: NonNull::from(deque),
4747
drain_len,
4848
idx: drain_start,
49-
tail_len,
49+
new_len,
5050
remaining: drain_len,
5151
_marker: PhantomData,
5252
}
@@ -79,7 +79,7 @@ impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
7979
f.debug_tuple("Drain")
8080
.field(&self.drain_len)
8181
.field(&self.idx)
82-
.field(&self.tail_len)
82+
.field(&self.new_len)
8383
.field(&self.remaining)
8484
.finish()
8585
}
@@ -111,18 +111,16 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
111111

112112
let drain_start = source_deque.len();
113113
let drain_len = self.0.drain_len;
114-
let drain_end = drain_start + drain_len;
115-
116-
let orig_len = self.0.tail_len + drain_end;
114+
let new_len = self.0.new_len;
117115

118116
if T::IS_ZST {
119117
// no need to copy around any memory if T is a ZST
120-
source_deque.len = orig_len - drain_len;
118+
source_deque.len = new_len;
121119
return;
122120
}
123121

124122
let head_len = drain_start;
125-
let tail_len = self.0.tail_len;
123+
let tail_len = new_len - head_len;
126124

127125
match (head_len, tail_len) {
128126
(0, 0) => {
@@ -131,10 +129,10 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
131129
}
132130
(0, _) => {
133131
source_deque.head = source_deque.to_physical_idx(drain_len);
134-
source_deque.len = orig_len - drain_len;
132+
source_deque.len = new_len;
135133
}
136134
(_, 0) => {
137-
source_deque.len = orig_len - drain_len;
135+
source_deque.len = new_len;
138136
}
139137
_ => unsafe {
140138
if head_len <= tail_len {
@@ -144,14 +142,14 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
144142
head_len,
145143
);
146144
source_deque.head = source_deque.to_physical_idx(drain_len);
147-
source_deque.len = orig_len - drain_len;
145+
source_deque.len = new_len;
148146
} else {
149147
source_deque.wrap_copy(
150148
source_deque.to_physical_idx(head_len + drain_len),
151149
source_deque.to_physical_idx(head_len),
152150
tail_len,
153151
);
154-
source_deque.len = orig_len - drain_len;
152+
source_deque.len = new_len;
155153
}
156154
},
157155
}

0 commit comments

Comments
 (0)