Crates.io | macro_tt_utils |
lib.rs | macro_tt_utils |
version | 0.1.9 |
source | src |
created_at | 2019-12-01 18:22:08.010829 |
updated_at | 2019-12-01 18:22:08.010829 |
description | Procedural Macro Utilities |
homepage | https://crates.io/crates/macro_tt_utils |
repository | https://github.com/clucompany/macro_tt_utils.git |
max_upload_size | |
id | 185764 |
size | 75,658 |
Procedural Macro Utilities (tt_ident
, throw_diagnostics
, deref_span
).
// Experimental API
Manipulation of input names in macros, currently only name comparison is possible.
#![feature(proc_macro_hygiene)]
use macro_tt_utils::tt_ident;
use macro_tt_utils::throw_compile_warning;
use macro_tt_utils::throw_compile_error;
#[macro_export]
macro_rules! macros_generate {
( fn name_checker($i:ident) -> | $i2:ident | $b:block ) => {
throw_compile_warning!(@root "We are testing warning.");
tt_ident! {
if ($i == $i2 || $i == "__hidden_a" || $i2 == "__hidden_a") {
throw_compile_error!(@root "Perhaps undefined behavior");
}
}
}
}
fn main() {
macros_generate! {
fn name_checker(a) -> |a| {
ff
}
}
}
/*
warning: We are testing warning.
--> src/main.rs:21:2
|
21 | macros_generate! {
| _____^
22 | | fn name_checker(a) -> |a| {
23 | | ff
24 | | }
25 | | }
| |_____^
error: Perhaps undefined behavior
--> src/main.rs:21:2
|
21 | macros_generate! {
| _____^
22 | | fn name_checker(a) -> |a| {
23 | | ff
24 | | }
25 | | }
| |_____^
*/
Macros to throw compiler errors or warnings (throw_compile_error
, throw_compile_warning
, throw_compile_note
, throw_compile_help
).
#![feature(proc_macro_hygiene)]
use macro_tt_utils::throw_compile_warning;
#[macro_export]
macro_rules! macros_generate {
{ fn name_checker($i:ident) -> | $i2:ident | $b:block } => {
throw_compile_warning!("#1 We are testing warning.");
throw_compile_warning!(@root "#2 We are testing warning.");
}
}
fn main() {
macros_generate! {
fn name_checker(a) -> |a| {}
}
}
/*
warning: #1 We are testing warning.
--> src/main.rs:8:3
|
8 | throw_compile_warning!("#1 We are testing warning.");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
14 | macros_generate! {
| _____-
15 | | fn name_checker(a) -> |a| {}
16 | | }
| |_____- in this macro invocation
warning: #2 We are testing warning.
--> src/main.rs:14:2
|
14 | macros_generate! {
| _____^
15 | | fn name_checker(a) -> |a| {}
16 | | }
| |_____^
*/
Unpacks the current span.
#![feature(proc_macro_hygiene)]
use macro_tt_utils::deref_span;
#[macro_export]
macro_rules! macros_generate {
{ fn name_checker($i:ident) -> | $i2:ident | $b:block } => {
compile_error!("#1 Test");
deref_span! {
compile_error!("#2 Test");
}
}
}
fn main() {
macros_generate! {
fn name_checker(a) -> |a| {}
}
}
/*
error: #1 Test
--> src/main.rs:8:3
|
8 | compile_error!("#1 Test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
...
17 | macros_generate! {
| _____-
18 | | fn name_checker(a) -> |a| {}
19 | | }
| |_____- in this macro invocation
error: #2 Test
--> src/main.rs:17:2
|
17 | macros_generate! {
| _____^
18 | | fn name_checker(a) -> |a| {}
19 | | }
| |_____^
error: aborting due to 2 previous errors
*/
Copyright 2019 #UlinProject (Denis Kotlyarov) Денис Котляров
Licensed under the MIT License
Licensed under the Apache License, Version 2.0