Guarantee vec.clear/truncate is O(1) for trivial types
This commit is contained in:
parent
74d5c70b17
commit
223600ac2c
1 changed files with 17 additions and 13 deletions
|
@ -685,6 +685,7 @@ impl<T> Vec<T> {
|
|||
/// [`drain`]: #method.drain
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn truncate(&mut self, len: usize) {
|
||||
if mem::needs_drop::<T>() {
|
||||
let current_len = self.len;
|
||||
unsafe {
|
||||
let mut ptr = self.as_mut_ptr().add(self.len);
|
||||
|
@ -701,6 +702,9 @@ impl<T> Vec<T> {
|
|||
ptr::drop_in_place(ptr);
|
||||
}
|
||||
}
|
||||
} else if len <= self.len {
|
||||
self.len = len;
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts a slice containing the entire vector.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue