In Rust, writing constructors is common but can be repetitive and boring. This library simplifies the process, making it more enjoyable and freeing up time for more interesting tasks.
The purpose of this library is to cover the most basic and frequent case. If you want more complex generation, you should probably take a look at rust-derive-builder
Just add to Cargo.toml
toml
[dependencies]
inew = "0.2.2"
The library requires a minimum Rust version of 1.56.0
and utilizes this version for executing tests within the CI
environment.
Suppose you have a structure and constructor, and we want to make a constructor for it. And it looks like this
```rust struct MyStruct { x: u32, y: u16, z: String, field: String, another_field: String }
impl MyStruct { pub fn new(x: u32, y: u16, z: String, field: String, anotherfield: String) -> Self { Self { x, y, z, field, anotherfield } } } ```
But everything here is very obvious, all fields and types are known to compiler. Therefore, we can hand over constructor generation to a macro
```rust use inew::New;
struct MyStruct { x: u32, y: u16, z: String, field: String, another_field: String } ```
That's it, just add the New annotation
If you don't want to pass all the fields, you can fill in some of the fields using annotations #[new(default)]
for
initialization with Default::default()
or #[new(default = my_func_name())]
for initialization by calling
myfuncname().
Example of usage
```rust use inew::New;
struct MyAwesomeStruct {
name: String,
#[new(default)]
entries: Vec
fn custom_func() -> u32 { 42u32 }
fn main() { MyAwesomeStruct::new("123".to_owned()) }
```
The #[new(default = ...)] attribute can take any valid Rust expression, such as 1 + 1 or vec![1], as its argument.
It is also possible to configure the privacy and rename the constructor using attributes.
```rust
struct MyStruct { x: u32, }
fn main() { MyStruct::new(1) // now it's a private function } ```
```rust
struct MyStruct { x: u32, }
fn main() { MyStruct::create(1) } ```
Generics and lifetimes are supported and work
```rust use inew::New;
struct MyStruct { x: u32, y: A, z: B, }
fn main() { MyStruct::new(1u32, 2u64, 3u16) } ```
```rust use inew::New;
struct MyStruct<'a> { x: u32, y: &'a u16, }
fn main() { let y = 1u16; MyStruct::new(x, &y) } ```
Unnamed structures are fully supported as well
```rust use inew::New;
struct MyStruct(u32);
fn main() { MyStruct::new(1) } ```
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Any contribution is welcome. Just write tests and submit merge requests
There is a very similar library with almost the same set of features and syntax. derive-new Below is a list of differences in the table.
| Feature | INew | derive-new | |-----------------------------------------|------|------------| | Default values support | Yes | Yes | | Generics and lifetimes support | Yes | Yes | | Enum support | No | Yes | | Constructor privacy settings | Yes | No | | Constructor renaming | Yes | No | | Unnamed structures support | Yes | Yes |
rust-derive-builder derive-new derive_more
Functionality is also built into the Scala, Kotlin, and Java languages for entities such
as case class
, data class
, record