|
|
|
@ -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;
|
|
|
|
|