Crate trl

Crate trl 

Source
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 pub argument 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_

  • option - valid return type of getters for fields of type &Option and &mut Option The problem: Basically this library generates getters with this pattern:
pub fn FIELD_NAME(SELF) -> MODIIER FIELD_TYPE {&SELF.FIELD_NAME}

If the field type is Option and the getter return reference or mutable reference, then the return type will be &Option of &mut Option respectively. But this doesn’t make sense and is even unsafe for &mut Option - it gives someone the opportunity to change Some() to None which you usually don’t want.

Solution: getters and get parameter option scans for each field for which the getter will be generated and changes the return type and the body of the corresponding getter so now it return Option<&T> or Option<&mut T>

§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

§Constructor

Constructor is a struct-level macro which generates the default constructor:

#[derive(trl)]
#[constructor]
struct Test {/* ... */}

Would genetate:

pub fn new(id: u32, name: String, email: String, phone_number: u64) -> Self {
    Self {
        id,
        name,
        email,
        phone_number,
    }
}
  • name - generate constructor with specified name
  • visibility - generate constructor with specified visibility modifier

Possible visibilities (must be specified as string literals)

  • "pub" - public visibility
  • "pub(path)" - restricted public visibility (e.g. “pub(crate)”, “pub(super)”, “pub(in some::module)”)
  • "private" - private visibility (not actually a Rust keyword, but used here for convenience)

For example this:

#[derive(trl)]
#[constructor(name = new_test, visibility = "pub(crate)")]
struct Test { /* ... */ }

Would generate:

pub(crate) fn new_test(id: u32, name: String, email: String, phone_number: u64) -> Self {
    Self {
        id,
        name,
        email,
        phone_number,
    }
}

Modules§

prelude
Module prelude reexports all commonly used attributes.

Attribute Macros§

constructor
Generate default constructor for a struct
getters
Generate getters for a struct fields
setters
Generate setters for a struct fields

Derive Macros§

Trl
New version of Trl derive
trl
Default macro which is required by all others