#[no_mangle]
pub extern "C" fn nstd_vec_new_with_cap(
    allocator: &NSTDAllocator,
    stride: NSTDUInt,
    cap: NSTDUInt
) -> NSTDVec<'_>
Available on crate feature nstd_vec only.
Expand description

Creates a new vector initialized with the given capacity.

If allocation fails, a vector with a capacity of 0 will be returned.

Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

  • NSTDUInt stride - The size in bytes of each value in the vector.

  • NSTDUInt cap - The initial capacity for the vector.

Returns

NSTDVec vec - The new vector.

Example

use nstd_sys::{
    alloc::{NSTDAllocError::NSTD_ALLOC_ERROR_NONE, NSTD_ALLOCATOR},
    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},
};

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).unwrap();
let mut vec = nstd_vec_new_with_cap(&NSTD_ALLOCATOR, 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);
    }
}