1 | // Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
|
2 | //
|
3 | // The idea is: The length field in SetLenOnDrop is a local variable
|
4 | // that the optimizer will see does not alias with any stores through the Vec's data
|
5 | // pointer. This is a workaround for alias analysis issue #32155
|
6 | pub(super) struct SetLenOnDrop<'a> {
|
7 | len: &'a mut usize,
|
8 | local_len: usize,
|
9 | }
|
10 |
|
11 | impl<'a> SetLenOnDrop<'a> {
|
12 | #[inline (always)]
|
13 | pub(super) fn new(len: &'a mut usize) -> Self {
|
14 | SetLenOnDrop {
|
15 | local_len: *len,
|
16 | len,
|
17 | }
|
18 | }
|
19 |
|
20 | #[inline (always)]
|
21 | pub(super) fn increment_len(&mut self, increment: usize) {
|
22 | self.local_len += increment;
|
23 | }
|
24 | }
|
25 |
|
26 | impl Drop for SetLenOnDrop<'_> {
|
27 | #[inline (always)]
|
28 | fn drop(&mut self) {
|
29 | *self.len = self.local_len;
|
30 | }
|
31 | }
|
32 | |