INew – a library for generating constructors

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

How to add the library to your project?

Just add to Cargo.toml toml [dependencies] inew = "0.1.1"

Мinimum supported Rust version

The library requires a minimum Rust version of 1.56.0 and utilizes this version for executing tests within the CI environment.

Example

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;

[derive(New)]

struct MyStruct { x: u32, y: u16, z: String, field: String, another_field: String } ```

That's it, just add the New annotation

Default fields and custom functions for generating fields

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;

[derive(New)]

struct MyAwesomeStruct { name: String, #[new(default)] entries: Vec, #[new(default)] somevalues: std::collections::HashSet, #[new(default = customfunc)] custom_value: u32 }

fn custom_func() -> u32 { 42u32 }

fn main() { MyAwesomeStruct::new("123".to_owned()) }

``` Unfortunately, at the moment, functions with an explicit path are not supported, they need to be imported into the scope explicitly. That's why path::to::custom_func will not work.

Generics and lifetimes

Generics and lifetimes are supported and work

Generics

```rust use inew::New;

[derive(New)]

struct MyStruct { x: u32, y: A, z: B, }

fn main() { MyStruct::new(1u32, 2u64, 3u16) } ```

Lifetimes

```rust use inew::New;

[derive(New)]

struct MyStruct<'a> { x: u32, y: &'a u16, }

fn main() { let y = 1u16; MyStruct::new(x, &y) } ```

Special thanks to

Licensing

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Contribution

Any contribution is welcome. Just write tests and submit merge requests

Related projects

rust

rust-derive-builder

java

lombok

Non Library

Functionality is also built into the Scala, Kotlin, and Java languages for entities such as case class, data class, record