Function nstd_sys::vec::nstd_vec_from_slice
source · Available on crate feature
nstd_vec only.Expand description
Creates a new vector from a slice.
Parameters:
const NSTDSlice *slice- The slice to copy data from.
Returns
NSTDVec vec - The new vector with a copy of slice’s contents.
Panics
This operation will panic if the slice’s stride is 0 or allocating fails.
Safety
The caller of this function must ensure that slice’s data is valid for reads.
Example
use nstd_sys::{
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>();
let numbers = [59237u128, 13953u128, 50285u128];
let numbers = nstd_core_slice_new(numbers.as_ptr().cast(), SIZE, 3);
unsafe {
let mut vec = nstd_vec_from_slice(&numbers);
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);
}
}