3 releases

0.1.2 Nov 4, 2020
0.1.1 Oct 16, 2020
0.1.0 Oct 16, 2020

#2613 in Rust patterns

MIT/Apache

30KB
164 lines

Scones

Scones on Crates.IO Documentation Examples

A crate for quick and powerful constructor/builder generation in Rust. Dual licensed under MIT OR Apache-2.0. Example:

use scones::{make_builder, make_constructor};

#[make_builder]
#[make_constructor]
struct Basic {
    int: i32,
    string: String,
}
let instance = Basic::new(int, string);
let instance = BasicBuilder::new().string("str".to_owned()).int(12345).build();
// Triggers a compile-time error because we have not specified all fields yet:
// let instance = BasicBuilder::new().build();

#[make_constructor]
#[make_constructor(pub new_identical(shared: i32))]
pub struct MultipleConstructors {
    #[value(shared for new_identical)]
    pub a: i32,
    #[value(shared for new_identical)]
    pub b: i32,
    #[value(shared for new_identical)]
    pub c: i32,

    #[value(true)]
    #[value(false for new)]
    pub identical: bool,
}
let instance = MultipleConstructors::new(1, 2, 3);
let instance = MultipleConstructors::new_identical(123);

#[make_constructor]
#[make_constructor(pub default_number)]
#[make_builder((field_1?))]
pub struct TupleStruct(
    #[value(30 for default_number)] i32,
    #[value("Unnamed".to_owned() for TupleStructBuilder)] String,
);
let instance = TupleStruct::default_number(field_1);
let instance = TupleStructBuilder::new().field_0(12345).build();

Dependencies

~1.5MB
~40K SLoC