nstd_vec_get_mut

Function nstd_vec_get_mut 

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

Returns a pointer to the element at index pos in vec.

§Note

It is highly advised to copy the return value onto the stack because the pointer can easily become invalid if the vector is mutated.

§Parameters:

  • NSTDVec *vec - The vector to read an element from.

  • NSTDUInt pos - The position of the element to get, starting at 0.

§Returns

NSTDAnyMut element - A pointer to the element at pos or NSTD_NULL if pos is out of the vector’s boundaries.

§Example

use nstd_sys::{
    alloc::NSTD_ALLOCATOR,
    core::slice::{nstd_core_slice_get, nstd_core_slice_new},
    vec::{nstd_vec_from_slice, nstd_vec_get, nstd_vec_get_mut, nstd_vec_len},
};

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

unsafe {
    let numbers = [639i64, -429i64, 440i64];
    let numbers = nstd_core_slice_new(numbers.as_ptr().cast(), SIZE, ALIGN, 3).unwrap();
    let mut vec = nstd_vec_from_slice(&NSTD_ALLOCATOR, &numbers).unwrap();
    for i in 0..nstd_vec_len(&vec) {
        let vv = nstd_vec_get_mut(&mut vec, i).cast::<i64>();
        assert!(!vv.is_null());
        *vv = -*vv;
    }
    for i in 0..nstd_vec_len(&vec) {
        let sv = nstd_core_slice_get(&numbers, i).cast::<i64>();
        let vv = nstd_vec_get(&vec, i).cast::<i64>();
        assert!(!sv.is_null() && !vv.is_null());
        assert!(-*sv == *vv);
    }
}