nstd_vec_new_with_cap

Function nstd_vec_new_with_cap 

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

Creates a new vector initialized with the given capacity.

§Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

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

  • NSTDUInt align - The alignment of each value in the vector.

  • NSTDUInt cap - The initial capacity for the vector.

§Returns

NSTDOptionalVec vec - The new vector on success, or an uninitialized “none” variant if allocation fails.

§Example

use nstd_sys::{
    alloc::NSTD_ALLOCATOR,
    core::{
        alloc::NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
        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>();
const ALIGN: usize = core::mem::align_of::<i16>();

unsafe {
    let numbers = [642i16, 324i16, 190i16];
    let numbers = nstd_core_slice_new(numbers.as_ptr().cast(), SIZE, ALIGN, 3).unwrap();
    let mut vec = nstd_vec_new_with_cap(&NSTD_ALLOCATOR, SIZE, ALIGN, 3).unwrap();
    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);
    }
}