diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-26 21:20:03 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-26 21:20:03 +0200 |
commit | edb9ff116d35927330977588606edff9ce6642bb (patch) | |
tree | 83c041bd321d47b922f4b8cd549b33412135d343 /src | |
parent | bcb96c4a7f24d4af32cbb0df9818b7d8972eb63a (diff) |
feat: add function create new MultiVec with capacity
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -70,6 +70,22 @@ where } } + pub fn with_capacity(capacity: usize) -> Self + { + let mut this = Self { + _pd: PhantomData, + ptr: NonNull::dangling(), + field_arr_byte_offsets: Vec::new(), + length: 0, + capacity: 0, + layout: None, + }; + + this.do_first_alloc(capacity); + + this + } + /// Pushes a item to the `MultiVec`. /// /// ## Note on performance @@ -90,7 +106,7 @@ where return; } - self.do_first_alloc(); + self.do_first_alloc(1); self.write_item(0, item); @@ -184,16 +200,16 @@ where self.field_arr_byte_offsets = new_field_arr_byte_offsets; } - fn do_first_alloc(&mut self) + fn do_first_alloc(&mut self, capacity: usize) { - let (layout, field_arr_byte_offsets) = Self::create_layout(1); + let (layout, field_arr_byte_offsets) = Self::create_layout(capacity); let Some(ptr) = NonNull::new(unsafe { alloc(layout) }) else { handle_alloc_error(layout); }; self.ptr = ptr; - self.capacity = 1; + self.capacity = capacity; self.field_arr_byte_offsets = field_arr_byte_offsets; self.layout = Some(layout); } |