Skip to content

Commit 200d03a

Browse files
Rollup merge of #112263 - GrishaVar:remove-extend-element, r=scottmcm
Remove ExtendElement, ExtendWith, extend_with Related to #104624, broken up into two commits. The first removes wrapper trait ExtendWith and its only implementer struct ExtendElement. The second may have perf issues so may be reverted/removed if no alternate fix is found; it should be profiled. r? `@scottmcm`
2 parents aabffef + dd2bd03 commit 200d03a

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

Diff for: library/alloc/src/vec/mod.rs

+7-23
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
23552355
let len = self.len();
23562356

23572357
if new_len > len {
2358-
self.extend_with(new_len - len, ExtendElement(value))
2358+
self.extend_with(new_len - len, value)
23592359
} else {
23602360
self.truncate(new_len);
23612361
}
@@ -2469,26 +2469,10 @@ impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
24692469
}
24702470
}
24712471

2472-
// This code generalizes `extend_with_{element,default}`.
2473-
trait ExtendWith<T> {
2474-
fn next(&mut self) -> T;
2475-
fn last(self) -> T;
2476-
}
2477-
2478-
struct ExtendElement<T>(T);
2479-
impl<T: Clone> ExtendWith<T> for ExtendElement<T> {
2480-
fn next(&mut self) -> T {
2481-
self.0.clone()
2482-
}
2483-
fn last(self) -> T {
2484-
self.0
2485-
}
2486-
}
2487-
2488-
impl<T, A: Allocator> Vec<T, A> {
2472+
impl<T: Clone, A: Allocator> Vec<T, A> {
24892473
#[cfg(not(no_global_oom_handling))]
2490-
/// Extend the vector by `n` values, using the given generator.
2491-
fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) {
2474+
/// Extend the vector by `n` clones of value.
2475+
fn extend_with(&mut self, n: usize, value: T) {
24922476
self.reserve(n);
24932477

24942478
unsafe {
@@ -2500,15 +2484,15 @@ impl<T, A: Allocator> Vec<T, A> {
25002484

25012485
// Write all elements except the last one
25022486
for _ in 1..n {
2503-
ptr::write(ptr, value.next());
2487+
ptr::write(ptr, value.clone());
25042488
ptr = ptr.add(1);
2505-
// Increment the length in every step in case next() panics
2489+
// Increment the length in every step in case clone() panics
25062490
local_len.increment_len(1);
25072491
}
25082492

25092493
if n > 0 {
25102494
// We can write the last element directly without cloning needlessly
2511-
ptr::write(ptr, value.last());
2495+
ptr::write(ptr, value);
25122496
local_len.increment_len(1);
25132497
}
25142498

Diff for: library/alloc/src/vec/spec_from_elem.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::ptr;
33
use crate::alloc::Allocator;
44
use crate::raw_vec::RawVec;
55

6-
use super::{ExtendElement, IsZero, Vec};
6+
use super::{IsZero, Vec};
77

88
// Specialization trait used for Vec::from_elem
99
pub(super) trait SpecFromElem: Sized {
@@ -13,7 +13,7 @@ pub(super) trait SpecFromElem: Sized {
1313
impl<T: Clone> SpecFromElem for T {
1414
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> {
1515
let mut v = Vec::with_capacity_in(n, alloc);
16-
v.extend_with(n, ExtendElement(elem));
16+
v.extend_with(n, elem);
1717
v
1818
}
1919
}
@@ -25,7 +25,7 @@ impl<T: Clone + IsZero> SpecFromElem for T {
2525
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
2626
}
2727
let mut v = Vec::with_capacity_in(n, alloc);
28-
v.extend_with(n, ExtendElement(elem));
28+
v.extend_with(n, elem);
2929
v
3030
}
3131
}

0 commit comments

Comments
 (0)