-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.md.tpl
87 lines (66 loc) · 2.42 KB
/
README.md.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

[](https://crates.io/crates/compose-idents)
[](https://docs.rs/compose-idents)
# compose-idents
A procedural macro that allows to construct identifiers from one or more arbitrary parts.
## Motivation
Rust's declarative macros do not allow generating new identifiers, because they are designed to operate on
the syntactic level (as opposed to the lexical level) using simple pattern matching.
For example the following code won't work:
```rust,compile_fail
macro_rules! my_macro {
($name:ident) => {
my_$name_fn() -> u32 {
42
}
};
}
my_macro!(foo);
```
This is why there is a need for a macro that allows to construct new identifiers.
## Usage
Here is how the macro works:
```rust
{{ file.Read "snippets/usage.rs" -}}
```
Here is a more practical example for how to auto-generate names for macro-generated tests for different data types:
```rust
use std::ops::Add;
use compose_idents::compose_idents;
fn add<T: Add<Output = T>>(x: T, y: T) -> T {
x + y
}
macro_rules! generate_add_tests {
($($type:ty),*) => {
$(
compose_idents!(test_fn = [test_add_, $type]; {
fn test_fn() {
let actual = add(2 as $type, 2 as $type);
let expected = (2 + 2) as $type;
assert_eq!(actual, expected);
}
});
)*
};
}
generate_add_tests!(u8, u32, u64);
test_add_u8();
test_add_u32();
test_add_u64();
```
For more usage examples look into `tests/` directory of the repository.
## Alternatives
There some other tools and projects dedicated to identifier manipulation:
- A macro from Nightly Rust that allows to concatenate identifiers. It is limited in functionality and nowhere near
to be stabilized:
<https://doc.rust-lang.org/std/macro.concat_idents.html>
- A very similar macro that doesn't support multiple aliases and is not maintained:
<https://github.com/DzenanJupic/concat-idents>
- A macro that allows to define and refer to unique temporary variables:
<https://crates.io/crates/templest>
## Development
The following standards are followed to maintain the project:
- <https://www.conventionalcommits.org/en/v1.0.0/>
- <https://semver.org/>
- <https://keepachangelog.com/en/1.1.0/>
- <https://adr.github.io/madr/>