Stop peeling the last iteration of the loop in Vec::repeat_with

This commit is contained in:
Scott McMurray 2022-11-24 03:12:54 -08:00
parent 1c966e7f15
commit a8954f1f6a

View File

@ -2163,7 +2163,7 @@ impl<T, A: Allocator> Vec<T, A> {
{ {
let len = self.len(); let len = self.len();
if new_len > len { if new_len > len {
self.extend_with(new_len - len, ExtendFunc(f)); self.extend_trusted(iter::repeat_with(f).take(new_len - len));
} else { } else {
self.truncate(new_len); self.truncate(new_len);
} }
@ -2491,16 +2491,6 @@ impl<T: Clone> ExtendWith<T> for ExtendElement<T> {
} }
} }
struct ExtendFunc<F>(F);
impl<T, F: FnMut() -> T> ExtendWith<T> for ExtendFunc<F> {
fn next(&mut self) -> T {
(self.0)()
}
fn last(mut self) -> T {
(self.0)()
}
}
impl<T, A: Allocator> Vec<T, A> { impl<T, A: Allocator> Vec<T, A> {
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
/// Extend the vector by `n` values, using the given generator. /// Extend the vector by `n` values, using the given generator.