Skip to main content

Constructor

Derive Macro Constructor 

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

Adds a new() constructor that accepts each field as a parameter. The method is private by default. Use #[new("pub")] or #[new("pub(crate)")] to set a custom visibility.

ยงExample

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

// adds
// ```rust
// impl MyStruct {
//     pub(crate) fn new(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::new("hello".into(), 2);

std::assert_eq!(my_struct_literal, my_struct_constructed);