Skip to main content

Constructor

Derive Macro Constructor 

Source
#[derive(Constructor)]
{
    // Attributes available to this derive:
    #[constructor]
}
Expand description

Adds a constructor that accepts each field as a parameter.

The method is private and named new by default. Use #[constructor(create)] to set a custom name or #[constructor(pub(crate) create)] to also set its visibility.

ยงExample

#[derive(Debug, mkutils_macros::Constructor, PartialEq)]
#[constructor(pub(crate) create)]
struct MyStruct {
    name: String,
    count: i32,
}

// adds
// ```rust
// impl MyStruct {
//     pub(crate) fn create(name: String, count: i32) -> Self {
//         Self { name, count }
//     }
// }
// ```
// as can be seen in

let my_struct_literal = MyStruct { name: "hello".into(), count: 2 };
let my_struct_constructed = MyStruct::create("hello".into(), 2);

std::assert_eq!(my_struct_literal, my_struct_constructed);