Provides the FieldNamesAsArray
procedural macro.
The macro adds the fn field_names_as_array()
to the struct the macro is derived on. It contains the field names of the given
struct, including the parents
Note: The macro can only be derived from named structs.
IMPORTANT This crate has a runtime overhead while it has limited options. If you do NOT intend to use it on a nested struct, you shall use this crate instead. See discussion
* Usage * Attributes * Field Attributes * Flatten
You can derive the FieldNamesAsArray
macro like this:
```rust use runtimestructfieldnamesas_array::FieldNamesAsArray;
struct Foo { bar: String, baz: String, bat: String, }
asserteq!(Foo::fieldnamesasarray(), ["bar", "baz", "bat"]); ```
The FieldNamesAsArray
macro supports the
field_names_as_array
attribute.
field_names_as_array
can be applied to a field with only the flatten
attribute
Container attributes are global attributes that change the behavior of the whole field names array, rather than that of a single field.
Field attributes can be added to the fields of a named struct and change the behavior of a single field.
The flatten
attribute will add the parent fields. Option struct are also supported. If the attribute is not added on a struct type, it will be considered as a regular field.
```rust use runtimestructfieldnamesas_array::FieldNamesAsArray;
struct Parent { foo: String, }
struct Foo {
bar: String,
baz: String,
#[fieldnamesasarray(flatten)]
parent: Parent,
#[fieldnamesasarray(flatten)]
parentoption: Option
asserteq!(Foo::fieldnamesasarray(), ["bar", "baz", "parent.foo", "parentoption.foo", "anotherparent"]); ```