Same as [`std::vec::Vec::resize`] but returns an error on allocation failure.
(&mut self, new_len: usize, value: T)
| 171 | /// Same as [`std::vec::Vec::resize`] but returns an error on allocation |
| 172 | /// failure. |
| 173 | pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), OutOfMemory> |
| 174 | where |
| 175 | T: TryClone, |
| 176 | { |
| 177 | match new_len.cmp(&self.len()) { |
| 178 | Ordering::Less => self.truncate(new_len), |
| 179 | Ordering::Equal => {} |
| 180 | Ordering::Greater => { |
| 181 | let delta = new_len - self.len(); |
| 182 | self.reserve(delta)?; |
| 183 | // Minimize `try_clone` calls by always pushing `value` directly |
| 184 | // as the last element. |
| 185 | for _ in 0..delta - 1 { |
| 186 | self.push(value.try_clone()?)?; |
| 187 | } |
| 188 | self.push(value)?; |
| 189 | } |
| 190 | } |
| 191 | Ok(()) |
| 192 | } |
| 193 | |
| 194 | /// Same as [`std::vec::Vec::resize_with`] but returns an error on |
| 195 | /// allocation failure. |