2 releases

Uses old Rust 2015

0.0.2 Oct 3, 2017
0.0.1 Oct 17, 2016

#1504 in Procedural macros

MIT license

17KB
230 lines

Build Status Latest Version

About

Smelter is an extremely lightweight rust proc_macro library to derive "Builder" methods for arbitrary rust structs.

Usage

Setup

[dependencies]
smelter = "*"

Add the following to the file in which you with to use smelter.

#![feature(proc_macro, custom_attribute)]

#[macro_use]
extern crate smelter;

Then just add #[derive(Builder)] above your struct,

Example

Smelter Generated Code

#![feature(proc_macro, custom_attribute)]

#[macro_use] extern crate smelter;

#[derive(PartialEq, Builder, Default, Debug, Clone)] #[smelter(prefix="with_")] pub struct User { pub uid: u64, pub email: String, pub alias: String, pub friends: Vec, }

# [ allow ( dead_code ) ]
impl User {
    pub fn with_uid(self, __value: u64) -> User {
        User { uid: __value, ..self }
    }
    pub fn with_email(self, __value: String) -> User {
        User { email: __value, ..self }
    }
    pub fn with_alias(self, __value: String) -> User {
        User { alias: __value, ..self }
    }
    pub fn with_friends(self, __value: Vec) -> User {
        User { friends: __value, ..self }
    }
    pub fn with_uid_mut(&mut self, __value: u64) -> &mut User {
        self.uid = __value;
        self
    }
    pub fn with_email_mut(&mut self, __value: String) -> &mut User {
        self.email = __value;
        self
    }
    pub fn with_alias_mut(&mut self, __value: String) -> &mut User {
        self.alias = __value;
        self
    }
    pub fn with_friends_mut(&mut self, __value: Vec) -> &mut User {
        self.friends = __value;
        self
    }
}

Invocation

// ... somewhere in your code
     let mut u1 = User::default();
// ...
     u1.with_email_mut("cardboardbox@example.com".to_string())
      .with_alias_mut("Cardboard box".to_string())
      .with_uid_mut(10u64);
      
// ... somewhere else
    let u2 = User::default()
                .with_email("filecabinate@example.com".to_string())
                .with_alias("File Cabinate".to_string())
                .with_uid(10u64);

More Examples

For more examples see the test.rs

Caveats

  • Will not work for enums, unit structs and tuple structs.

Dependencies

~1.7–2.7MB
~66K SLoC