#structs #generate #persistent #helper #macro #function #nicer

macro persistent-structs

A Macro that generate helper functions to make non-mutable structs nicer to use

2 releases

0.1.1 Nov 12, 2022
0.1.0 Nov 12, 2022

#2022 in Procedural macros

27 downloads per month

MIT license

5KB
50 lines

Persistent Structs

A small derive Macro for structs, that generates a with_<name> and update_<name> method for each field of a struct, e.g:

use persistent_structs::PersistentStruct;

#[derive(PersistentStruct, PartialEq)]
struct Foo {
    pub foo: u8,
}

fn main() {
    let foo = Foo { foo: 1 };
    let foo = foo.with_foo(5);
    assert!(foo == Foo { foo: 5 });

    let foo = foo.update_foo(|x| x + 1);
    assert!(foo.foo == 6);
}

install via:

  cargo add persistent-structs

(requires cargo-add, otherwise figure out the current version number in at docs.rs, and add it manually)


lib.rs:

Persitent Structs

A small derive Macro for structs, that generates a with_<name> and update_<name> method for each field of a struct, e.g:

use persistent_structs::PersistentStruct;

#[derive(PersistentStruct, PartialEq)]
struct Foo {
    pub foo: u8,
}

fn main() {
    let foo = Foo { foo: 1 };
    let foo = foo.with_foo(5);
    assert!(foo == Foo { foo: 5 });

    let foo = foo.update_foo(|x| x + 1);
    assert!(foo.foo == 6);
}

Dependencies

~1.5MB
~34K SLoC