#functional #getter #proc-macro #core #new

macro func_core

Procedural macro to create a functional core for named structs

1 unstable release

0.1.0 Apr 19, 2019

#2 in #procedural-macro

MIT license

5KB
58 lines

func-core

func-core is a Rust procedural macro inspired by Gary Bernhardt's screencast on Functional core, imperative shell.

This macro will act on named structs and create a method new(), that takes all struct members as parameters and return a new object/instance of that struct, and a getter method for each struct member, that takes a self reference (&self) and returns the value of that member.

Example

#[derive(FunctionalCore)]
struct Test {
    foo: u8,
    bar: f32
}

fn main() {
    let my_test = Test::new(1,2_f32);
    // each member has its specific getter that you can use
    assert_eq!(my_test.foo(), &1);
    assert_eq!(my_test.bar(), &2_f32);
}

Usage

Add func_core = "*" as a dependency to your Cargo.toml and run cargo build to download it. After that, put a use fun-core::FunctionalCore atop your Rust files to be able to use the derive macro.

Why?

Laziness.

Dependencies

~2MB
~48K SLoC