summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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);
}