tia; trait, impl accessors | automatic
This is a syntax sugar proc-macro crate for trait
, impl
accessors patterns. tia
generate to an accessor impl
s of an indivisual trait
s for any struct
|enun
|union
s.
Features
tia
will be generateimpl
codes automatically if you set the#[tia(...)]
directives.- Target types:
struct
|enum
|union
- Setting levels: for all fields | per field
trait
supporting: Can be generate for multipletrait
s.#[tia(MyTrait1: rg, MyTrait2: si)]
(See also the Example-3 in below.)
- Generative accessors: Getter-like, Setter-like
gm
for get/move pattern; for move-semantics tunings.g
for get/implicit copy pattern; for primitive types such asu8
.rg
for get/reference pattern; for reference usages. (See also the Example-1 in below.)rmg
for get/reference mut pattern; for r/w as object.s
for set/implicit move/copy pattern; for primitive types such asu8
andCopy
-ables.rs
for set/explicit copy with Clone::clone_from pattern; forClone
-able without type converting.rsi
for set/explicit copy with Into::into pattern; forInto
-able input types with type converting if required.
- Useful +features:
print
, show a generated codes in building-time via stdcerr.file
, output a generated codes to files such assrc/.tia/{MyStructSymbol}.rs
.file-pretty
,file
+ formatting withrustfmt
command.disable
, disable all tia features in temporary.
- Naming patters:
- Default, will generate a
get_xxxx
|set_xxxx
prefixed accessor. - Spedified prefix,
rg="awesome"
, will generateawesome_xxxx
prefixed accessor. - Spedified suffix,
rg+="awesome"
, will generatexxxx_awesome
suffixed accessor. - Fullname,
rg="awesome"
will generateawesome
fullname defined accessor.
- Default, will generate a
Example
Example-1; The introduction of tia
It is minimal, very simple version. Without trait
s complex.
use Tia; // 1. use
// 2. derive
// 3. tia directives
cargo run
, then you will get the output:
foo=123 bar=Hello
- (1,2) are preparing.
- (3) is tia directive for the struct-level.
- (4,5) are an automatic generated accessors by
tia
.
The automatic generated code is:
It could be output to src/.tia/MyStruct
if use file-pretty
features in Cargo.toml
:
[]
={ ="*", =["file-pretty"] }
Example-2; A little complex/practical usage
use Tia; // use
// derive
// <-- tia directives, for all fields
cargo run
:
&foo = 42
&mys = MyStruct
a: mys.bar = Hello,
b: mys.bar = tia.
c: mys.bar = tia.
x = 0
&mys = MyStruct
y =
Example-3; trait
usage
use Tia;
//include!(".tia/MyStruct.rs");
// derive
Then cargo run
:
123, meow
32
The generated code with print
, file
or file-pretty
features:
Example-4; Misc.
The include
feature:
[]
={ ="*", =["file-pretty","include"] }
tia
will be:
- Generate codes if not exists.
- Generate
include!
such asinclude!("src/.tia/MyStruct")
instead.
Note
tia
provide a useful syntax sugar, it will helpful if you should impl many interface-like specifications. For eg, something designed for object-oriented paradigm mainly languages such as C#, Java, C++, or complex data definition based by UML such as XMLSchema. But, it is just a syntax sugar. Please do not overdose tia
unnecessarily.
LICENSE
Author
- USAGI.NETWORK / Usagi Ito https://github.com/usagi/