#structs #macro #write #auto #automatic #derive #generate

macro auto_new

basic macro for removing the need to write new(...) on structs

1 stable release

new 1.0.0 Jan 11, 2025

#325 in Procedural macros

Download history 112/week @ 2025-01-08

112 downloads per month

Apache-2.0

13KB
144 lines

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:

Arc::new(MyStruct{...})

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 auto_new::new;

#[derive(new)]
pub struct MyStruct {
    a: u32,
    b: String,
}

fn main() {
    let instance = MyStruct::new(42, String::from("Hello, world!"));
    println!("MyStruct: {}, {}", instance.a, instance.b);
}

The crate is fairly robust and works with all sorts of weird generics and lifetimes without issue.

use auto_new::new;

#[derive(new,Debug)]
pub struct GenericStruct<'a, T> where T:Copy {
    a: &'a T,
    b: Box<u32>,
}

fn main() {
    let value = 42;
    let instance = GenericStruct::new_arc(&value, Box::new(value));
    println!("{:?}", instance);
}

Features

Customizing Visibility

You may not want the new functions to be public.

#[derive(new)]
#[new_visibility(/*private*/)]
struct MyStruct<'a, T:Debug>(&'a T);

Allternativly it might be benificial to use a more complex qualifier.

#[derive(new)]
#[new_visibility(pub(crate))]
struct MyStruct<'a, T:Debug>(&'a T);

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:

#[derive(new)]
#[no_new_arc]
struct MyStruct<T>(&'a T,&'b T);

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

Dependencies

~220–660KB
~16K SLoC