4 stable releases

1.1.1 Dec 30, 2020
1.1.0 Aug 8, 2020
1.0.1 Jun 30, 2020
1.0.0 Jun 25, 2020

#67 in No standard library

Download history 2483/week @ 2024-06-14 2664/week @ 2024-06-21 2391/week @ 2024-06-28 2194/week @ 2024-07-05 2462/week @ 2024-07-12 2703/week @ 2024-07-19 2772/week @ 2024-07-26 2135/week @ 2024-08-02 1822/week @ 2024-08-09 1725/week @ 2024-08-16 1689/week @ 2024-08-23 2109/week @ 2024-08-30 2369/week @ 2024-09-06 2208/week @ 2024-09-13 2157/week @ 2024-09-20 1601/week @ 2024-09-27

8,712 downloads per month
Used in 53 crates (6 directly)

MIT license

8KB
89 lines

Build, Test

Assign

Mutate instances with declarative flavor!

This module provides macro assign! to allow mutating instance with declarative flavor

The motivation of this macro is to enable programmer to document a sequence of mutations instance fields as initialization by writing it in a declarative way. assign! macro also allows programmer to skip defining fields that has default value. Such case are used when a dependency is exposing an non-exhaustive struct

Usage

#[macro_use]
extern crate assign;

fn main() {
    struct SomeStruct {
        a: u32,
        b: Option<f32>,
        c: String,
    }
    impl SomeStruct {
        fn new() -> SomeStruct {
            SomeStruct {
                a: 1u32,
                b: None,
                c: String::from("old"),
            }
        }
    }

    // In order to treat the mutation of field `a` and `c` as an initialization,
    // Use assign to mutate field in declarative flavor, thus avoiding the risk inserting code
    // between the line that defines a field and the line that defines the other
    // Note that field `b` is skipped
    let instance = assign!(SomeStruct::new(), {
      a: 2u32,
      c: String::from("new"),
    });

    // Equivalent
    let instance2 = {
        let mut item = SomeStruct::new();
        item.a = 2u32;
        item.c = String::from("new");
        item
    };

    assert_eq!(instance.a, instance2.a);
    assert_eq!(&instance.c, &instance2.c);
    assert_eq!(instance.b, instance2.b);
}

License

MIT

No runtime deps