placenew
A rust crate providing safe placement-new semantics for rust
This crate is a procedural macro crate that provides the proc macro: place_boxed
The macro takes a struct initializer as an input, and generates code that initalizes the items in-place in heap memory
Usecase
In some cases, you may want to initialize memory in-place on the heap in order to avoid an expensive copy operation, such is the case in this code:
let mut res = Boxnew
In some cases the rust compiler will optomize away the copy, but its not guaranteed, and assuming it is can often lead to undefined behavior, that is what this crate aims to solve
Examples
Example 1
The macro will then generate code that looks like:
Limitations
The syntax:
place_boxed!
is not allowed, this is because there isnt really a good way to figure out which values are initalized by the Rest statement, and which are not if a good method can be figure out, I will add it to the crate and remove this limitation