Skip to content

Commit c45fdfc

Browse files
Use iter::repeat_n to implement Vec::extend_with
1 parent 5986ff0 commit c45fdfc

File tree

1 file changed

+1
-25
lines changed

1 file changed

+1
-25
lines changed

library/alloc/src/vec/mod.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -3124,31 +3124,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
31243124
#[track_caller]
31253125
/// Extend the vector by `n` clones of value.
31263126
fn extend_with(&mut self, n: usize, value: T) {
3127-
self.reserve(n);
3128-
3129-
unsafe {
3130-
let mut ptr = self.as_mut_ptr().add(self.len());
3131-
// Use SetLenOnDrop to work around bug where compiler
3132-
// might not realize the store through `ptr` through self.set_len()
3133-
// don't alias.
3134-
let mut local_len = SetLenOnDrop::new(&mut self.len);
3135-
3136-
// Write all elements except the last one
3137-
for _ in 1..n {
3138-
ptr::write(ptr, value.clone());
3139-
ptr = ptr.add(1);
3140-
// Increment the length in every step in case clone() panics
3141-
local_len.increment_len(1);
3142-
}
3143-
3144-
if n > 0 {
3145-
// We can write the last element directly without cloning needlessly
3146-
ptr::write(ptr, value);
3147-
local_len.increment_len(1);
3148-
}
3149-
3150-
// len set by scope guard
3151-
}
3127+
self.extend_trusted(iter::repeat_n(value, n));
31523128
}
31533129
}
31543130

0 commit comments

Comments
 (0)