diff --git a/src/lib.rs b/src/lib.rs index dc35429..3c0da20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -900,7 +900,7 @@ impl ThinVec { self.extend(other.drain(..)) } - pub fn drain(&mut self, range: R) -> Drain + pub fn drain(&mut self, range: R) -> Drain<'_, T> where R: RangeBounds, { @@ -1118,6 +1118,7 @@ impl AsRef<[T]> for ThinVec { } impl Extend for ThinVec { + #[inline] fn extend(&mut self, iter: I) where I: IntoIterator, @@ -1131,7 +1132,7 @@ impl Extend for ThinVec { } impl fmt::Debug for ThinVec { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } @@ -1266,8 +1267,18 @@ where T: Clone, { fn clone(&self) -> ThinVec { - let mut new_vec = ThinVec::with_capacity(self.len()); - new_vec.extend(self.iter().cloned()); + let len = self.len(); + let mut new_vec = ThinVec::::with_capacity(len); + let mut data_raw = new_vec.data_raw(); + for x in self.iter() { + unsafe { + ptr::write(data_raw, x.clone()); + data_raw = data_raw.add(1); + } + } + unsafe { + new_vec.set_len(len); // could be the singleton + } new_vec } } @@ -1292,7 +1303,7 @@ pub struct IntoIter { start: usize, } -pub struct Drain<'a, T: 'a> { +pub struct Drain<'a, T> { iter: IterMut<'a, T>, vec: *mut ThinVec, end: usize,