| 105 | } |
| 106 | |
| 107 | pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), CapacityError<T>> { |
| 108 | if index > self.len() { |
| 109 | panic_oob!("try_insert", index, self.len()) |
| 110 | } |
| 111 | if self.len() == self.capacity() { |
| 112 | return Err(CapacityError::new(element)); |
| 113 | } |
| 114 | let len = self.len(); |
| 115 | |
| 116 | // follows is just like Vec<T> |
| 117 | unsafe { |
| 118 | // infallible |
| 119 | // The spot to put the new value |
| 120 | { |
| 121 | let p: *mut _ = self.get_unchecked_ptr(index); |
| 122 | // Shift everything over to make space. (Duplicating the |
| 123 | // `index`th element into two consecutive places.) |
| 124 | ptr::copy(p, p.offset(1), len - index); |
| 125 | // Write it in, overwriting the first copy of the `index`th |
| 126 | // element. |
| 127 | ptr::write(p, element); |
| 128 | } |
| 129 | self.set_len(len + 1); |
| 130 | } |
| 131 | Ok(()) |
| 132 | } |
| 133 | |
| 134 | pub fn pop(&mut self) -> Option<T> { |
| 135 | if self.is_empty() { |