nstd_vec_from_slice

Function nstd_vec_from_slice 

Source
#[unsafe(no_mangle)]
pub unsafe extern "C" fn nstd_vec_from_slice<'a>( allocator: &'a NSTDAllocator, slice: &NSTDSlice, ) -> NSTDOptionalVec<'a>
Available on crate feature vec only.
Expand description

Creates a new vector from a slice.

§Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

  • const NSTDSlice *slice - The slice to copy data from.

§Returns

NSTDOptionalVec vec - The new vector with a copy of slice’s contents on success, or an uninitialized “none” variant if allocating fails.

§Safety

The caller of this function must ensure that slice’s data is valid for reads.

§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::<u128>();
const ALIGN: usize = core::mem::align_of::<u128>();

unsafe {
    let numbers = [59237u128, 13953u128, 50285u128];
    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::<u128>();
        let vv = nstd_vec_get(&vec, i).cast::<u128>();
        assert!(!sv.is_null() && !vv.is_null());
        assert!(*sv == *vv);
    }
}