Crates.io | concat-idents-bruce0203 |
lib.rs | concat-idents-bruce0203 |
version | |
source | src |
created_at | 2024-11-30 10:38:56.821457 |
updated_at | 2024-11-30 10:38:56.821457 |
description | Allows concatenating multiple identifiers and using them everywhere |
homepage | |
repository | https://github.com/Bruce0203/concat-idents |
max_upload_size | |
id | 1466494 |
Cargo.toml error: | TOML parse error at line 23, column 1 | 23 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This crate provides a single, easy to use macro.
use concat_idents::concat_idents;
concat_idents!(fn_name = foo_, _, bar {
fn fn_name() {
// --snip--
}
});
foo__bar();
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) */ });
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));
This macro will throw a compile error, if:
concat_idents!({});
concat_idents!(ident {});
concat_idents!(ident = {});
concat_idents!(= foo, bar {});
concat_idents!(ident = foo, bar);
...
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_]
...