summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-08-26 21:20:03 +0200
committerHampusM <hampus@hampusmat.com>2024-08-26 21:20:03 +0200
commitedb9ff116d35927330977588606edff9ce6642bb (patch)
tree83c041bd321d47b922f4b8cd549b33412135d343 /src/lib.rs
parentbcb96c4a7f24d4af32cbb0df9818b7d8972eb63a (diff)
feat: add function create new MultiVec with capacity
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs24
1 files changed, 20 insertions, 4 deletions
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);
}