Reuse(Struct, Enum)
It provides functional macros to reuse fields from Struct and Enum definition.
[]
= "0.0.0"
Examples
Struct
Say you build a demo web server to send private messages.
use ;
// Define your base public struct here.
public_struct!;
MessageBase!;
MessageBase!
Enum
Compare it with the code example from the Rust documenation for Enum.
use ;
// Define your base private enum here.
private_enum!;
WebEventBase!;
Rename
You can rename the struct and reuse the same fields. You can do the same with enum.
use ;
public_struct!;
UserBase!;
UserBase!
Details
-
Each struct and enum created from the macros are completely unrelevant to each other except they have the same fields you define.
-
When you use
private_struct!
andprivate_enum!
, you can't use pub keyword in it and others use them. It wouldn't be logical if a private struct or private enum can have public fields. -
nested_macro!
is required to use the other macros from this crate. It is used to make a macro that creates other macros.
Comparison with attribute macro
-
You can reuse the fields with attribute macros also. But, you need some dependencies.
-
If you want more, please read the official documenation about procedural macros.
How to test it
$git clone git@github.com:steadylearner/born.git && cargo test pass
$cargo test pass
to run passing tests.$cargo test fail
to run failing tests. You need to install trybuild first.
If you want to see how the macros from this package expand, use $cargo test macros
. You need to install rustfmt and cargo-expand to use it before.
$rustup component add rustfmt && cargo install cargo-expand
macrotest is based on trybuild. They are not that compatible to test with a single command and take long time.
They make cargo to redownload the dependendencies and recompile everytime. For that reason, there are commands to test them separately.