1 // SPDX-License-Identifier: Apache-2.0 OR MIT
3 // Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
5 // The idea is: The length field in SetLenOnDrop is a local variable
6 // that the optimizer will see does not alias with any stores through the Vec's data
7 // pointer. This is a workaround for alias analysis issue #32155
8 pub(super) struct SetLenOnDrop<'a> {
13 impl<'a> SetLenOnDrop<'a> {
15 pub(super) fn new(len: &'a mut usize) -> Self {
16 SetLenOnDrop { local_len: *len, len }
20 pub(super) fn increment_len(&mut self, increment: usize) {
21 self.local_len += increment;
25 pub(super) fn current_len(&self) -> usize {
30 impl Drop for SetLenOnDrop<'_> {
33 *self.len = self.local_len;