Function nstd_sys::vec::nstd_vec_new_with_cap
source · #[no_mangle]
pub extern "C" fn nstd_vec_new_with_cap(
element_size: NSTDUInt,
cap: NSTDUInt
) -> NSTDVecAvailable on crate feature
nstd_vec only.Expand description
Creates a new vector initialized with the given capacity.
Note
This will return a “null vector” (a vector that has not allocated yet) on error.
Parameters:
-
NSTDUInt element_size- The size in bytes of each value in the vector. -
NSTDUInt cap- The initial capacity for the vector.
Returns
NSTDVec vec - The new vector.
Panics
This function will panic if either element_size or cap are zero.
Example
use nstd_sys::{
core::slice::{nstd_core_slice_get, nstd_core_slice_new},
vec::{nstd_vec_extend, nstd_vec_get, nstd_vec_len, nstd_vec_new_with_cap},
alloc::NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
};
const SIZE: usize = core::mem::size_of::<i16>();
let numbers = [642i16, 324i16, 190i16];
let numbers = nstd_core_slice_new(numbers.as_ptr().cast(), SIZE, 3);
let mut vec = nstd_vec_new_with_cap(SIZE, 3);
unsafe {
assert!(nstd_vec_extend(&mut vec, &numbers) == NSTD_ALLOC_ERROR_NONE);
for i in 0..nstd_vec_len(&vec) {
let sv = nstd_core_slice_get(&numbers, i).cast::<i16>();
let vv = nstd_vec_get(&vec, i).cast::<i16>();
assert!(!sv.is_null() && !vv.is_null());
assert!(*sv == *vv);
}
}