dont_expand/
lib.rs

1/*!
2This library provides macro implementations for macros found in the rust std
3library. The purpose of the macros in this library is to do nothing. The 
4macros are intended to be imported as needed to shadow the std macros. This
5is useful when using `cargo expand` to see what a macro might be doing as it
6will effectively hide the typical expansion of the std macros which typically
7just pollute the output.
8
9Included macros are: 
10    assert, assert_eq, assert_ne,
11    cfg, column, compile_error, concat, dbg, 
12    debug_assert, debug_assert_eq, debug_assert_ne,
13    env, eprint, eprintln, file, format, format_args,
14    include, include_bytes, include_str, is_x86_feature_detected,
15    line, matches, module_path, option_env, panic, print, println,
16    thread_local, todo, unimplemented, unreachable,
17    vec, write, writeln
18
19And the following derive macros:
20    Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord
21
22The following example would only show the expansion of the `Default` 
23macro
24```
25use dont_expand::{Debug, Clone};
26
27#[derive(Debug, Clone, Default)]
28struct Foo;
29```
30
31Glob imports don't work with dont-expand as that throws an error by the 
32compiler since they shadow the std macros. 
33*/
34use proc_macro::TokenStream;
35use paste::paste;
36
37#[macro_use]
38mod macros {
39    macro_rules! gen_dummy_derive {
40        ($($name: ident),*) => {
41            paste! {
42                $(
43                    #[proc_macro_derive($name)]
44                    pub fn [< $name:lower _derive_macro >](_inp: TokenStream) -> TokenStream {
45                        TokenStream::new()
46                    }
47                )*
48            }
49        }
50    }
51
52    macro_rules! gen_dummy_proc_macro {
53        ($($name: ident),*) => {
54            paste! {
55                $(
56                    #[proc_macro]
57                    pub fn $name(inp: TokenStream) -> TokenStream {
58                        let mut out = String::new();
59                        out.push_str(r###"r##""###);
60                        out.push_str(stringify!([<$name:lower>]));
61                        out.push_str("!(");
62                        out.push_str(&inp.to_string());
63                        out.push_str(r###")"##"###);
64                        match out.parse() {
65                            Ok(s) => s,
66                            Err(_) => {
67                                use std::io::Write;
68                                let _ = std::io::stderr().write_all(b"failed to generate dummy string in hiding call to $name");
69                                TokenStream::new()
70                            }
71                        }
72                    }
73                )*
74            }
75        }
76    }
77}
78
79gen_dummy_derive!(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord);
80gen_dummy_proc_macro!(
81    assert, assert_eq, assert_ne,
82    cfg, column, compile_error, concat, dbg, 
83    debug_assert, debug_assert_eq, debug_assert_ne,
84    env, eprint, eprintln, file, format, format_args,
85    include, include_bytes, include_str, is_x86_feature_detected,
86    line, matches, module_path, option_env, panic, print, println,
87    thread_local, todo, unimplemented, unreachable,
88    vec, write, writeln
89);