#ident #concat #concatenate #names #combine #idents

macro concat-idents

Allows concatenating multiple identifiers and using them everywhere

7 stable releases

1.1.5 Jun 14, 2023
1.1.4 Oct 31, 2022
1.1.3 Jul 4, 2021
1.1.2 Jan 12, 2021
1.0.0 Aug 4, 2020

#40 in Procedural macros

Download history 3324/week @ 2023-11-24 3645/week @ 2023-12-01 4236/week @ 2023-12-08 5594/week @ 2023-12-15 4547/week @ 2023-12-22 3563/week @ 2023-12-29 4799/week @ 2024-01-05 4354/week @ 2024-01-12 4356/week @ 2024-01-19 3843/week @ 2024-01-26 4204/week @ 2024-02-02 4245/week @ 2024-02-09 5160/week @ 2024-02-16 6039/week @ 2024-02-23 5436/week @ 2024-03-01 4077/week @ 2024-03-08

21,592 downloads per month
Used in 86 crates (44 directly)

MIT/Apache

15KB
184 lines

concat-idents!

crates.io docs.rs licence

This crate provides a single, easy to use macro.

Usage

Basic usage

use concat_idents::concat_idents;

concat_idents!(fn_name = foo_, _, bar {
       fn fn_name() {
           // --snip--
       }
});

foo__bar();

Allowed identifier parts

concat_idents!(fn_name = _, __ { /* underscores */ });
concat_idents!(fn_name = foo, _bar { /* identifiers */ });
concat_idents!(fn_name = "foo", "bar" { /* strings */ });
concat_idents!(fn_name = 'f', 'o', 'o' { /* characters */ });
concat_idents!(fn_name = foo, 1, bar, 2 { /* integers */ });
concat_idents!(fn_name = true, false { /* booleans */ });
concat_idents!(fn_name = "enum", bar { /* quoted reserved keywords (recommended way) */ });
concat_idents!(fn_name = r#struct, bar { /* escaped reserved keywords (not recommended, since some keywords produce error) */ });

Generating Tests

macro_rules! generate_test {
   ($method:ident($lhs:ident, $rhs:ident)) => {
       concat_idents!(test_name = $method, _, $lhs, _, $rhs {
           #[test]
           fn test_name() {
               let _ = $lhs::default().$method($rhs::default());
           }
       });
   };
}

#[derive(Default)]
struct S(i32);

impl Add<i32> for S {
   type Output = S;
   fn add(self,rhs: i32) -> Self::Output { S(self.0 + rhs) }
}

impl Sub<i32> for S {
   type Output = S;
   fn sub(self,rhs: i32) -> Self::Output { S(self.0 - rhs) }
}

generate_test!(add(S, i32));
generate_test!(sub(S, i32));

Error

This macro will throw a compile error, if:

  1. an unexpected syntax is passed
concat_idents!({});
concat_idents!(ident {});
concat_idents!(ident =  {});
concat_idents!(= foo, bar {});
concat_idents!(ident = foo, bar);
...
  1. one of the identifiers is invalid
concat_idents!(ident = true {});        // identifiers cannot consist of only one bool 
concat_idents!(ident = 1 {});           // identifiers cannot consist of only one int
concat_idents!(ident = 1, foo {});      // identifiers cannot start with an int
concat_idents!(ident = foo, 1.0 {});    // identifiers cannot contain floats
concat_idents!(ident = "enum");         // identifiers cannot consist of only one reserved keyword
concat_idents!(ident = r#struct);       // identifiers cannot consist of only one reserved keyword
concat_idents!(ident = " space");       // identifiers cannot contain spaces
concat_idents!(ident = "foo-bar🧨");    // identifiers can only contain [a-zA-Z0-9_]
...

Dependencies

~330–780KB
~19K SLoC