Shortens the list down to `len` elements. Does nothing if the list is already shorter than `len`.
(&mut self, new_len: usize, pool: &mut ListPool<T>)
| 598 | /// |
| 599 | /// Does nothing if the list is already shorter than `len`. |
| 600 | pub fn truncate(&mut self, new_len: usize, pool: &mut ListPool<T>) { |
| 601 | if new_len == 0 { |
| 602 | self.clear(pool); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | match pool.len_of(self) { |
| 607 | None => return, |
| 608 | Some(len) => { |
| 609 | if len <= new_len { |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | let block; |
| 614 | let idx = self.index as usize; |
| 615 | let sclass = sclass_for_length(len); |
| 616 | let new_sclass = sclass_for_length(new_len); |
| 617 | if sclass != new_sclass { |
| 618 | block = pool.realloc(idx - 1, sclass, new_sclass, new_len + 1); |
| 619 | self.index = (block + 1) as u32; |
| 620 | } else { |
| 621 | block = idx - 1; |
| 622 | } |
| 623 | pool.data[block] = T::new(new_len); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /// Grow the list by inserting `count` elements at `index`. |
| 629 | /// |