Skip to content

Commit 2e3592e

Browse files
committed
Reduce duplication for arena-spec
Cleanup for rust-lang#78569
1 parent a5fbaed commit 2e3592e

File tree

1 file changed

+21
-33
lines changed
  • compiler/rustc_arena/src

1 file changed

+21
-33
lines changed

compiler/rustc_arena/src/lib.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -136,51 +136,39 @@ where
136136
impl<T, const N: usize> IterExt<T> for std::array::IntoIter<T, N> {
137137
#[inline]
138138
fn alloc_from_iter(self, arena: &TypedArena<T>) -> &mut [T] {
139-
let len = self.len();
140-
if len == 0 {
141-
return &mut [];
142-
}
143-
// Move the content to the arena by copying and then forgetting it
144-
unsafe {
145-
let start_ptr = arena.alloc_raw_slice(len);
146-
self.as_slice().as_ptr().copy_to_nonoverlapping(start_ptr, len);
147-
mem::forget(self);
148-
slice::from_raw_parts_mut(start_ptr, len)
149-
}
139+
alloc_from_iter(arena, self.len(), self.as_slice().as_ptr(), || mem::forget(self))
150140
}
151141
}
152142

153143
impl<T> IterExt<T> for Vec<T> {
154144
#[inline]
155145
fn alloc_from_iter(mut self, arena: &TypedArena<T>) -> &mut [T] {
156-
let len = self.len();
157-
if len == 0 {
158-
return &mut [];
159-
}
160-
// Move the content to the arena by copying and then forgetting it
161-
unsafe {
162-
let start_ptr = arena.alloc_raw_slice(len);
163-
self.as_ptr().copy_to_nonoverlapping(start_ptr, len);
164-
self.set_len(0);
165-
slice::from_raw_parts_mut(start_ptr, len)
166-
}
146+
alloc_from_iter(arena, self.len(), self.as_ptr(), || unsafe { self.set_len(0) })
167147
}
168148
}
169149

170150
impl<A: smallvec::Array> IterExt<A::Item> for SmallVec<A> {
171151
#[inline]
172152
fn alloc_from_iter(mut self, arena: &TypedArena<A::Item>) -> &mut [A::Item] {
173-
let len = self.len();
174-
if len == 0 {
175-
return &mut [];
176-
}
177-
// Move the content to the arena by copying and then forgetting it
178-
unsafe {
179-
let start_ptr = arena.alloc_raw_slice(len);
180-
self.as_ptr().copy_to_nonoverlapping(start_ptr, len);
181-
self.set_len(0);
182-
slice::from_raw_parts_mut(start_ptr, len)
183-
}
153+
alloc_from_iter(arena, self.len(), self.as_ptr(), || unsafe { self.set_len(0) })
154+
}
155+
}
156+
157+
/// Reduce duplication for multiple specializations of alloc_from_iter.
158+
#[inline]
159+
fn alloc_from_iter<T, F>(arena: &TypedArena<T>, len: usize, ptr: *const T, clean: F) -> &mut [T]
160+
where
161+
F: FnOnce(),
162+
{
163+
if len == 0 {
164+
return &mut [];
165+
}
166+
// Move the content to the arena by copying and then forgetting it
167+
unsafe {
168+
let start_ptr = arena.alloc_raw_slice(len);
169+
ptr.copy_to_nonoverlapping(start_ptr, len);
170+
clean();
171+
slice::from_raw_parts_mut(start_ptr, len)
184172
}
185173
}
186174

0 commit comments

Comments
 (0)