| 1 | # concat-idents! |
| 2 | |
| 3 | [](https://crates.io/crates/concat-idents) |
| 4 | [](https://docs.rs/concat-idents/) |
| 5 | [](https://github.com/DzenanJupic/concat-idents) |
| 6 | |
| 7 | This crate provides a single, easy to use macro. |
| 8 | |
| 9 | ## Usage |
| 10 | |
| 11 | ### Basic usage |
| 12 | |
| 13 | ```rust |
| 14 | use concat_idents::concat_idents; |
| 15 | |
| 16 | concat_idents!(fn_name = foo_, _, bar { |
| 17 | fn fn_name() { |
| 18 | // --snip-- |
| 19 | } |
| 20 | }); |
| 21 | |
| 22 | foo__bar(); |
| 23 | ``` |
| 24 | |
| 25 | ### Allowed identifier parts |
| 26 | |
| 27 | ```rust |
| 28 | concat_idents!(fn_name = _, __ { /* underscores */ }); |
| 29 | concat_idents!(fn_name = foo, _bar { /* identifiers */ }); |
| 30 | concat_idents!(fn_name = "foo", "bar" { /* strings */ }); |
| 31 | concat_idents!(fn_name = 'f', 'o', 'o' { /* characters */ }); |
| 32 | concat_idents!(fn_name = foo, 1, bar, 2 { /* integers */ }); |
| 33 | concat_idents!(fn_name = true, false { /* booleans */ }); |
| 34 | concat_idents!(fn_name = "enum", bar { /* quoted reserved keywords (recommended way) */ }); |
| 35 | concat_idents!(fn_name = r#struct, bar { /* escaped reserved keywords (not recommended, since some keywords produce error) */ }); |
| 36 | ``` |
| 37 | |
| 38 | ### Generating Tests |
| 39 | |
| 40 | ```rust |
| 41 | macro_rules! generate_test { |
| 42 | ($method:ident($lhs:ident, $rhs:ident)) => { |
| 43 | concat_idents!(test_name = $method, _, $lhs, _, $rhs { |
| 44 | #[test] |
| 45 | fn test_name() { |
| 46 | let _ = $lhs::default().$method($rhs::default()); |
| 47 | } |
| 48 | }); |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | #[derive(Default)] |
| 53 | struct S(i32); |
| 54 | |
| 55 | impl Add<i32> for S { |
| 56 | type Output = S; |
| 57 | fn add(self,rhs: i32) -> Self::Output { S(self.0 + rhs) } |
| 58 | } |
| 59 | |
| 60 | impl Sub<i32> for S { |
| 61 | type Output = S; |
| 62 | fn sub(self,rhs: i32) -> Self::Output { S(self.0 - rhs) } |
| 63 | } |
| 64 | |
| 65 | generate_test!(add(S, i32)); |
| 66 | generate_test!(sub(S, i32)); |
| 67 | ``` |
| 68 | |
| 69 | ## Error |
| 70 | This macro will throw a compile error, if: |
| 71 | 1. an unexpected syntax is passed |
| 72 | |
| 73 | ```rust |
| 74 | concat_idents!({}); |
| 75 | concat_idents!(ident {}); |
| 76 | concat_idents!(ident = {}); |
| 77 | concat_idents!(= foo, bar {}); |
| 78 | concat_idents!(ident = foo, bar); |
| 79 | ... |
| 80 | ``` |
| 81 | 2. one of the identifiers is invalid |
| 82 | |
| 83 | ```rust |
| 84 | concat_idents!(ident = true {}); // identifiers cannot consist of only one bool |
| 85 | concat_idents!(ident = 1 {}); // identifiers cannot consist of only one int |
| 86 | concat_idents!(ident = 1, foo {}); // identifiers cannot start with an int |
| 87 | concat_idents!(ident = foo, 1.0 {}); // identifiers cannot contain floats |
| 88 | concat_idents!(ident = "enum"); // identifiers cannot consist of only one reserved keyword |
| 89 | concat_idents!(ident = r#struct); // identifiers cannot consist of only one reserved keyword |
| 90 | concat_idents!(ident = " space"); // identifiers cannot contain spaces |
| 91 | concat_idents!(ident = "foo-bar🧨"); // identifiers can only contain [a-zA-Z0-9_] |
| 92 | ... |
| 93 | ``` |