nstd_core_slice_get

Function nstd_core_slice_get 

Source
#[unsafe(no_mangle)]
pub const extern "C" fn nstd_core_slice_get( slice: &NSTDSlice, pos: NSTDUInt, ) -> NSTDAny
Available on crate feature core only.
Expand description

Returns an immutable pointer to the element at index pos in slice.

§Parameters:

  • const NSTDSlice *slice - The slice 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 slice’s boundaries.

§Example

use nstd_sys::core::slice::{nstd_core_slice_get, nstd_core_slice_new};

const STRIDE: usize = core::mem::size_of::<i32>();
const ALIGN: usize = core::mem::align_of::<i32>();

unsafe {
    let numbers: [i32; 3] = [33, 103, 45];
    let slice =
        nstd_core_slice_new(numbers.as_ptr().cast(), STRIDE, ALIGN, numbers.len()).unwrap();

    assert!(*nstd_core_slice_get(&slice, 0).cast::<i32>() == 33);
    assert!(*nstd_core_slice_get(&slice, 1).cast::<i32>() == 103);
    assert!(*nstd_core_slice_get(&slice, 2).cast::<i32>() == 45);
    assert!(nstd_core_slice_get(&slice, 3).is_null());
}