From edb9ff116d35927330977588606edff9ce6642bb Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 26 Aug 2024 21:20:03 +0200 Subject: feat: add function create new MultiVec with capacity --- src/lib.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 698e641..228b7e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); } -- cgit v1.2.3-18-g5258