Expand description
§TRL - type reflection library
This library provides auto generation of some common methods based on Rust macros
All future examples will use this test struct:
struct User {
id: u32,
name: String,
email: String,
// public fields are ignored by default
pub phone_number: u64,
}This library contains 2 types of macros:
- Struct level - macros applied to a struct
- Field level - macros applied to a single field
Both of them have addictional arguments
§Struct level macros
Struct level macros are: getters and setters. They generates getters/setters for all the fields of the struct
They have common arguments:
- include=[…]
- exclude=[…]
- pub
- prefix=…
§
- include=[…] - generate getter/setters only for the listed fields. For example:
#[derive(trl)]
#[getters(include=[name, email])]
#[setters(include=[name, email])]
struct Test {/* ... */}Would generate getters/setters for b and c fields.
- exclude=[…] - generate getters/setters for all fields except the listed. For example
#[derive(trl)]
#[getters(exclude=[a, b])]
#[setters(exclude=[a, b])]
struct Test { /* ... */ }Would generate getters/setters only for the c field.
-
pub - include public fields. By default public fields are ignored, but you can specify the
pubargument to generate getters/setters for them too -
prefix=… - generates getters/setters with specified prefix. For example
#[derive(trl)]
#[getters(prefix=get_)]
#[setters(prefix=set_)]
struct Test { /* ... */ }Would generate getters:
get_id(),get_name()and setters:set_id(),set_name()
Default value for getters is empty string, and for setters is set_
§Field level macros
Field level macros are get and set. They generates a getter/setter for a single field.
They have common arguments:
- name = … - generate a getter/setter with the specified name
- prefix = … - generate a getter/setter with the specified prefix
Attribute Macros§
- getters
- Generate getters for a struct fields
- setters
- Generate setters for a struct fields /// ### Common arguments
Derive Macros§
- trl
- Default macro which is required by all others