Impl New

A procedural macro to generate a new function for your struct.

Add to your project

Add this to your Cargo.toml: toml [dependencies] impl_new = "0.1.0" Or run this command in your workspace: bash cargo add impl_new

Usage

Is simple, just derive the impl_new::New proc macro on your struct and it will generate a new function for you.

For Named Fields

```rust

[derive(impl_new::New)]

struct Foo { name: String, age: usize, }

// The generated code will look like this: // impl Foo { // pub fn new(name: impl Into, age: Into) -> Self { // Self { name: name.into(), age: age.into() } // } // }

fn main() { let foo = Foo::new("Hello", 42usize); // Will use Into::into to convert the arguments to the fields types. asserteq!(foo.name, "Hello".tostring()); assert_eq!(foo.age, 42); } ```

With Attributes

```rust

[derive(impl_new::New)]

struct Foo { #[implnew(name = "username")] name: String, #[implnew(name = "userage")] age: usize, }

// The generated code will look like this: // impl Foo { // pub fn new(username: impl Into, userage: Into) -> Self { // Self { name: username.into(), age: userage.into() } // } // }

fn main() { let foo = Foo::new("Hello", 42usize); // Will use Into::into to convert the arguments to the fields types. asserteq!(foo.name, "Hello".tostring()); assert_eq!(foo.age, 42); } ```

For Unnamed Fields

Note: The #[impl_new(name = "name")] attribute is required for unnamed fields.

```rust

[derive(impl_new::New)]

struct Foo(#[implnew(name = "name")] String, #[implnew(name = "age")] usize);

// The generated code will look like this: // impl Foo { // pub fn new(name: impl Into, age: Into) -> Self { // Self(name.into(), age.into()) // } // }

fn main() { let foo = Foo::new("Hello", 42usize); // Will use Into::into to convert the arguments to the fields types. asserteq!(foo.0, "Hello".tostring()); assert_eq!(foo.1, 42); } ```

Attributes

License

This project is licensed under the MIT license.