Function nstd_sys::shared_ptr::nstd_shared_ptr_new
source · #[no_mangle]
pub unsafe extern "C" fn nstd_shared_ptr_new(
allocator: &NSTDAllocator,
element_size: NSTDUInt,
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. -
NSTDUInt element_size- The size of the shared object. -
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 of element_size bytes.
Example
use core::ptr::addr_of;
use nstd_sys::{
alloc::NSTD_ALLOCATOR,
shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new},
};
const SIZE: usize = core::mem::size_of::<i16>();
let v = i16::MIN;
unsafe {
let shared_ptr = nstd_shared_ptr_new(&NSTD_ALLOCATOR, SIZE, addr_of!(v).cast()).unwrap();
assert!(*nstd_shared_ptr_get(&shared_ptr).cast::<i16>() == v);
}