nstd_shared_ptr_new

Function nstd_shared_ptr_new 

Source
#[unsafe(no_mangle)]
pub unsafe extern "C" fn nstd_shared_ptr_new( allocator: &NSTDAllocator, layout: NSTDAllocLayout, init: NSTDAny, ) -> NSTDOptionalSharedPtr<'_>
Available on crate feature shared_ptr only.
Expand description

Creates a new initialized instance of a shared pointer.

§Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

  • NSTDAllocLayout layout - The shared object’s memory layout.

  • NSTDAny init - A pointer to the object to initialize the shared pointer with.

§Returns

NSTDOptionalSharedPtr shared_ptr - The new shared pointer, or an uninitialized “none” variant if allocating fails.

§Safety

init must be a pointer to a value that is valid for reads based on layout.

§Example

use core::ptr::addr_of;
use nstd_sys::{
    alloc::NSTD_ALLOCATOR,
    core::alloc::nstd_core_alloc_layout_new,
    shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new},
};

unsafe {
    let v = i16::MIN;
    let size = core::mem::size_of::<i16>();
    let align = core::mem::align_of::<i16>();
    let layout = nstd_core_alloc_layout_new(size, align).unwrap();
    let shared_ptr = nstd_shared_ptr_new(&NSTD_ALLOCATOR, layout, addr_of!(v).cast()).unwrap();
    assert!(*nstd_shared_ptr_get(&shared_ptr).cast::<i16>() == v);
}