nstd_vec_remove

Function nstd_vec_remove 

Source
#[unsafe(no_mangle)]
pub extern "C" fn nstd_vec_remove( vec: &mut NSTDVec<'_>, index: NSTDUInt, ) -> NSTDErrorCode
Available on crate feature vec only.
Expand description

Removes the element at index in a vector.

§Parameters:

  • NSTDVec *vec - The vector.

  • NSTDUInt index - The index of the element to remove.

§Returns

NSTDErrorCode errc - Nonzero if index is invalid.

§Example

use nstd_sys::{
    alloc::NSTD_ALLOCATOR,
    core::slice::nstd_core_slice_new,
    vec::{nstd_vec_from_slice, nstd_vec_get, nstd_vec_remove},
};

const SIZE: usize = core::mem::size_of::<u32>();
const ALIGN: usize = core::mem::align_of::<u32>();

unsafe {
    let slice: [u32; 5] = [1, 2, 3, 4, 5];
    let slice = nstd_core_slice_new(slice.as_ptr().cast(), SIZE, ALIGN, 5).unwrap();
    let mut vec = nstd_vec_from_slice(&NSTD_ALLOCATOR, &slice).unwrap();
    assert!(nstd_vec_remove(&mut vec, 0) == 0);
    assert!(nstd_vec_remove(&mut vec, 3) == 0);
    for i in 0..3 {
        let v = nstd_vec_get(&vec, i);
        assert!(!v.is_null());
        assert!(*v.cast::<u32>() == (i + 2) as u32);
    }
}