nstd_vec_get

Function nstd_vec_get 

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

Returns an immutable 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:

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

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

§Returns

NSTDAny 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_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 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);
    }
}