alloc/vec/
set_len_on_drop.rs

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
6pub(super) struct SetLenOnDrop<'a> {
7    len: &'a mut usize,
8    local_len: usize,
9}
10
11impl<'a> SetLenOnDrop<'a> {
12    #[inline]
13    pub(super) fn new(len: &'a mut usize) -> Self {
14        SetLenOnDrop { local_len: *len, len }
15    }
16
17    #[inline]
18    pub(super) fn increment_len(&mut self, increment: usize) {
19        self.local_len += increment;
20    }
21
22    #[inline]
23    pub(super) fn current_len(&self) -> usize {
24        self.local_len
25    }
26}
27
28impl Drop for SetLenOnDrop<'_> {
29    #[inline]
30    fn drop(&mut self) {
31        *self.len = self.local_len;
32    }
33}