Appends an element to the back of the list. Returns the index where the element was inserted.
(&mut self, element: T, pool: &mut ListPool<T>)
| 387 | /// Appends an element to the back of the list. |
| 388 | /// Returns the index where the element was inserted. |
| 389 | pub fn push(&mut self, element: T, pool: &mut ListPool<T>) -> usize { |
| 390 | let idx = self.index as usize; |
| 391 | match pool.len_of(self) { |
| 392 | None => { |
| 393 | // This is an empty list. Allocate a block and set length=1. |
| 394 | debug_assert_eq!(idx, 0, "Invalid pool"); |
| 395 | let block = pool.alloc(sclass_for_length(1)); |
| 396 | pool.data[block] = T::new(1); |
| 397 | pool.data[block + 1] = element; |
| 398 | self.index = (block + 1) as u32; |
| 399 | 0 |
| 400 | } |
| 401 | Some(len) => { |
| 402 | // Do we need to reallocate? |
| 403 | let new_len = len + 1; |
| 404 | let block; |
| 405 | if is_sclass_min_length(new_len) { |
| 406 | // Reallocate, preserving length + all old elements. |
| 407 | let sclass = sclass_for_length(len); |
| 408 | block = pool.realloc(idx - 1, sclass, sclass + 1, len + 1); |
| 409 | self.index = (block + 1) as u32; |
| 410 | } else { |
| 411 | block = idx - 1; |
| 412 | } |
| 413 | pool.data[block + new_len] = element; |
| 414 | pool.data[block] = T::new(new_len); |
| 415 | len |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /// Grow list by adding `count` reserved-value elements at the end. |
| 421 | /// |