aquarhead / handcraft

handcraft Rust structs!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

handcraft

A function-like proc macro that aids building nested structs by hand, when majority of the fields remain default.

Example

let crafted = handcraft!(Foo { a: 0 });

The macro basically inserts ..Default::default() for all nested structs. Thus the code above expands to:

let crafted = Foo {
  a: 0,
  ..Default::default()
};

See more in tests.

Known Issues

When using handcraft!, structs in nested macro calls such as vec! can't be handled by the outer macro, hence they'll error with "missing field".

This is illustrated by this fail-to-compile test.

Specifically for those vec! cases, it can be trivially avoided by changing vec![...] to [...].into(). As shown by the vec_fields_into test.

Alternatively, invoke further handcraft! inside nested macros also works, however this sort of defeats the goal of this macro :( Example is given by the vec_fields_nested test.

Misc

The idea forms when writing a Kubernetes operator with kube-rs, where I need to build several large, nested structs that maps to kubernetes resources.

The code is much inspired by autodefault but I wanted a different usage style (attribute vs. function-like macro).

I think autodefault makes more sense when a function needs to create many small structs. While handcraft is better when only a few structs are created but each are complex and/or deeply nested - like those exposed in k8s-openapi.

About

handcraft Rust structs!

License:MIT License


Languages

Language:Rust 100.0%