nstd_vec_pop

Function nstd_vec_pop 

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

Removes the last value of a vector and returns a pointer to it.

§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.

§Returns

  • NSTDAny value - A pointer to the value that was popped off the stack, or null if the vector is empty.

§Example

use core::ptr::addr_of;
use nstd_sys::{
    alloc::NSTD_ALLOCATOR,
    core::slice::nstd_core_slice_new,
    vec::{nstd_vec_extend, nstd_vec_new, nstd_vec_pop},
};

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

unsafe {
    let mut vec = nstd_vec_new(&NSTD_ALLOCATOR, SIZE, ALIGN);
    let values: [f64; 3] = [9.4, 3.1, 6.0];
    let values_slice = nstd_core_slice_new(values.as_ptr().cast(), SIZE, ALIGN, 3).unwrap();
    nstd_vec_extend(&mut vec, &values_slice);
    for value in values.iter().rev() {
        assert!(*value == *nstd_vec_pop(&mut vec).cast::<f64>());
    }
}