diff --git a/src/vec-insert-remove.md b/src/vec-insert-remove.md index 365ddb6..1f2e2d9 100644 --- a/src/vec-insert-remove.md +++ b/src/vec-insert-remove.md @@ -16,13 +16,14 @@ using the old len. pub fn insert(&mut self, index: usize, elem: T) { // Note: `<=` because it's valid to insert after everything // which would be equivalent to push. - assert!(self.len >= index, "index is out of bounds"); + assert!(index <= self.len, "index out of bounds"); if self.cap == self.len { self.grow(); } unsafe { if index < self.len { + // ptr::copy(src, dest, len): "copy from source to dest len elems" ptr::copy( self.ptr.as_ptr().offset(index as isize), self.ptr.as_ptr().offset(index as isize + 1), @@ -41,7 +42,7 @@ Remove behaves in the opposite manner. We need to shift all the elements from ```rust,ignore pub fn remove(&mut self, index: usize) -> T { // Note: `<` because it's *not* valid to remove after everything - assert!(self.len > index, "index is out of bounds"); + assert!(index < self.len, "index out of bounds"); unsafe { self.len -= 1; diff --git a/src/vec-into-iter.md b/src/vec-into-iter.md index 528664a..880e5c8 100644 --- a/src/vec-into-iter.md +++ b/src/vec-into-iter.md @@ -133,8 +133,16 @@ impl Drop for IntoIter { if self.cap != 0 { // drop any remaining elements for _ in &mut *self {} + + let align = mem::align_of::(); + let elem_size = mem::size_of::(); + let num_bytes = elem_size * self.cap; + unsafe { - alloc::dealloc(self.buf.as_ptr() as *mut _, alloc::Layout::new::()); + alloc::dealloc( + self.buf.as_ptr() as *mut _, + alloc::Layout::from_size_align_unchecked(num_bytes, align), + ); } } }