auto_new
This crate automatically generates new and new_arc functions for structs using the #[derive(new)] macro.
Why new_arc?
The new_arc function is designed to be more efficient than what most people would write by hand in most cases. This is because the rust compiler usually treats:
new
as two seprate memory allocations. This makes it hard for the optimizer to inline most of what should be inlined. Which is why we use Arc::new_uninit() and explictly write each of the feilds.
Usage
To use #[derive(new)], simply annotate your struct with it, and the macro will generate the new function for you. Here's an example:
use new;
The crate is fairly robust and works with all sorts of weird generics and lifetimes without issue.
use new;
Features
Customizing Visibility
You may not want the new functions to be public.
;
Allternativly it might be benificial to use a more complex qualifier.
;
Excluding new_arc for no_std Environments
If you don't need the new_arc function, you can exclude it with the #[no_new_arc] attribute:
;
Future Work
It might be nice to include more types like Box,Rc,RefCell,Mutex etc. There might also be a way to allow the user to write the new function and then "automagically" generate functions for all types based on that.
This is already somewhat achived by makeit