Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a3f3fc5

Browse files
committed
refactor: moved SetLenOnDrop to set_len_on_drop
1 parent a2f4bc0 commit a3f3fc5

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ use self::spec_from_elem::SpecFromElem;
109109

110110
mod spec_from_elem;
111111

112+
use self::set_len_on_drop::SetLenOnDrop;
113+
114+
mod set_len_on_drop;
115+
112116
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
113117
///
114118
/// # Examples
@@ -1911,35 +1915,6 @@ impl<T, A: Allocator> Vec<T, A> {
19111915
}
19121916
}
19131917

1914-
// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
1915-
//
1916-
// The idea is: The length field in SetLenOnDrop is a local variable
1917-
// that the optimizer will see does not alias with any stores through the Vec's data
1918-
// pointer. This is a workaround for alias analysis issue #32155
1919-
struct SetLenOnDrop<'a> {
1920-
len: &'a mut usize,
1921-
local_len: usize,
1922-
}
1923-
1924-
impl<'a> SetLenOnDrop<'a> {
1925-
#[inline]
1926-
fn new(len: &'a mut usize) -> Self {
1927-
SetLenOnDrop { local_len: *len, len }
1928-
}
1929-
1930-
#[inline]
1931-
fn increment_len(&mut self, increment: usize) {
1932-
self.local_len += increment;
1933-
}
1934-
}
1935-
1936-
impl Drop for SetLenOnDrop<'_> {
1937-
#[inline]
1938-
fn drop(&mut self) {
1939-
*self.len = self.local_len;
1940-
}
1941-
}
1942-
19431918
impl<T: PartialEq, A: Allocator> Vec<T, A> {
19441919
/// Removes consecutive repeated elements in the vector according to the
19451920
/// [`PartialEq`] trait implementation.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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]
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+
23+
impl Drop for SetLenOnDrop<'_> {
24+
#[inline]
25+
fn drop(&mut self) {
26+
*self.len = self.local_len;
27+
}
28+
}

0 commit comments

Comments
 (0)