| Crates.io | impl-new-derive |
| lib.rs | impl-new-derive |
| version | 0.1.3 |
| created_at | 2024-10-06 19:30:02.112307+00 |
| updated_at | 2025-03-09 21:28:56.221861+00 |
| description | Derive macro for implementing the `new` method for structs |
| homepage | https://github.com/rust-dd/impl-new-derive.git |
| repository | https://github.com/rust-dd/impl-new-derive.git |
| max_upload_size | |
| id | 1399302 |
| size | 17,409 |
The impl-new-derive procedural macro generates a new constructor for Rust structs. This macro automatically creates a constructor that initializes:
new).#[default(...)], orDefault::default() if no custom default expression is provided.new constructor for structs.new function takes all public fields of the struct as arguments.#[default(...)] attribute, that expression is used for initialization.Default::default().Add the macro to your project by including it in your Cargo.toml:
[dependencies]
impl_new_derive = "0.1"
Annotate your struct with #[derive(ImplNew)] to automatically generate a new constructor.
Optionally, you can also derive Default if needed elsewhere in your code.
use impl_new_derive::ImplNew;
#[derive(ImplNew, Default)]
struct MyStruct {
pub name: String,
pub age: u32,
// Private field; no custom default attribute,
// so it will be initialized using Default::default()
secret: String,
}
fn main() {
// The generated constructor requires arguments
// for each PUBLIC field in the struct.
let my_struct = MyStruct::new("John".to_string(), 30);
println!("Name: {}, Age: {}", my_struct.name, my_struct.age);
// 'secret' is private and gets a default value of "" (the Default for String).
}
#[default(...)] for Private Fieldsuse impl_new_derive::ImplNew;
#[derive(ImplNew)]
struct Credentials {
pub username: String,
// Private field with a custom default value.
// This field doesn't appear as a parameter in the `new` method.
// Instead, it will be automatically set to "empty_token".to_string().
#[default(\"empty_token\".to_string())]
token: String,
}
fn main() {
// The `new` function is generated only for public fields,
// so we pass just `username`.
let creds = Credentials::new(\"alice\".to_string());
println!(\"Username: {}, Token: {}\", creds.username, creds.token);
// Prints: Username: alice, Token: empty_token
}
use impl_new_derive::ImplNew;
#[derive(ImplNew, Default)]
struct MyStruct<T> {
pub value: T,
// Private field without custom default, so it uses Default::default()
count: usize,
}
fn main() {
// In a generic struct, only the public fields appear in `new`.
let my_struct = MyStruct::new(42);
// 'count' is private, and we didn't give it a `#[default(...)]`,
// so it's initialized with `Default::default()`, i.e. 0.
println!(\"Value: {}, Count: {}\", my_struct.value, my_struct.count);
// Prints: Value: 42, Count: 0
}
When you annotate a struct with #[derive(ImplNew)], the macro performs the following actions:
new method.#[default(expr)] attribute is present:
expr to initialize that field.Default::default() to initialize that field.impl.Default and does not have a #[default(...)] attribute, the macro fails to compile.#[default(...)], the expression inside must be a valid Rust expression for that field's type.Feel free to open issues or pull requests if you have any suggestions or improvements.
This project is licensed under the MIT License.