cpp-like-new

Crates.iocpp-like-new
lib.rscpp-like-new
version0.1.0
sourcesrc
created_at2023-04-26 08:20:54.509795
updated_at2023-04-26 08:20:54.509795
descriptionA library that allows you to create/destroy objects in Rust in a similar way to using new/delete expressions in C++
homepage
repositoryhttps://github.com/xmh0511/cpp-like-new.git
max_upload_size
id849167
size6,560
(xmh0511)

documentation

README

Description

  1. Support to create a single object through new, as well as, destroy the object through delete
  2. Support to create an array object through new, as well as, destroy the array through delete []
  3. Support multiple dimension array with the syntax of C++
  4. For an array object, if the initializer expressions are less than the size of the created array, the remaining elements are default initialized
  5. Notice that all functions/macros in this library are unsafe(i.e. use them in unsafe context/block)

How to use this library

Create a single object like the way in C++

let ptr = new!{ i32 }; // with default initialization
let ptr_init = new!{ i32 {10} }; // with 10 as its initializer

// destroy the created objects
delete!{ ptr };  
delete!{ ptr_init }; 

Create an array object

let size = 2;
let arr = new!{ i32[size] };
let arr_init = new!{ i32[size]{0,1}};

let mul_dim_arr = new!{  i32[size][3][4] };
let two_dim_arr = new!{ i32[size][3] {[0,0,0], [1,2,3]} };

// destroy the created objects
delete!{ [] arr};
delete!{ [] arr_init};
delete!{ [] mul_dim_arr};
delete!{ [] two_dim_arr};
Commit count: 15

cargo fmt