safe-builder-derive

Crates.iosafe-builder-derive
lib.rssafe-builder-derive
version0.1.1
sourcesrc
created_at2017-04-21 22:11:39.215971
updated_at2017-04-21 22:18:16.062415
descriptionDerive a safe builder pattern
homepage
repositoryhttps://github.com/Matthew-Maclean/safe-builder-derive
max_upload_size
id11474
size16,125
Matthew Maclean (Matthew-Maclean)

documentation

README

You woudn't call unwrap on a constructer!

A proc-macro for deriving a type-safe builder pattern on any struct*

example:

extern crate safe_builder;
#[macro_use]
extern crate safe_builder_derive;

use safe_builder::*;

#[derive(SafeBuilder)]
struct Person
{
    name: String,
    age: usize,
    //        town    street  number
    address: (String, String, usize)
}

fn main()
{
    let me: Person = Person::build()
        .name("Matthew M.".to_owned())
        .age(18)
        .address(("Toronto".to_owned(), "Younge st.".to_owned(), 0));
    // no need to call unwrap - the compiler knows that the type is complete
    
    let you = Person::build()
        .age(0) // hello, world!
        .address(("City".to_owned(), "Street".to_owned(), 0))
        .name("You!".to_owned());
    // build your structs in any order!
    
    /*
    Wont compile: the type is incomplete
    let no_name: Person = Person::build()
        .name("Unkown".to_owned())
        .address((String::new(), String::new(), 0));
    */
}

Please use this crate on your most complicated structs, and open some issues when you find bugs!

*no generic struct or those with lifeties, yet

Commit count: 18

cargo fmt