Skip to content

Commit 157fca4

Browse files
committed
Move common proc_macro_derive code into a function.
1 parent 7368ec1 commit 157fca4

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

procedural-masquerade/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ license = "MIT/Apache-2.0"
99

1010
[lib]
1111
path = "lib.rs"
12+
doctest = false

procedural-masquerade/lib.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,32 +172,37 @@ macro_rules! define_proc_macros {
172172
$(
173173
$( #[$attr] )*
174174
#[proc_macro_derive($proc_macro_name)]
175-
pub fn $proc_macro_name(input: ::proc_macro::TokenStream) -> ::proc_macro::TokenStream {
176-
// Use another function to hide this one’s local variables from $block
177-
#[inline]
178-
fn implementation($input: &str) -> String $body
179-
180-
let input = input.to_string();
181-
let mut normalized = String::with_capacity(input.len());
182-
for piece in input.split_whitespace() {
183-
normalized.push_str(piece);
184-
normalized.push(' ');
185-
}
186-
187-
let prefix = "#[allow(unused)] enum ProceduralMasqueradeDummyType { Input = (0, stringify!(";
188-
let suffix = ")).0, } ";
189-
assert!(normalized.starts_with(prefix), "expected prefix not found in {:?}", input);
190-
assert!(normalized.ends_with(suffix), "expected suffix not found in {:?}", input);
191-
192-
let start = prefix.len();
193-
let end = normalized.len() - suffix.len();
194-
let output = implementation(&normalized[start..end]);
195-
output.parse().unwrap()
175+
pub fn $proc_macro_name(derive_input: ::proc_macro::TokenStream)
176+
-> ::proc_macro::TokenStream {
177+
let $input = derive_input.to_string();
178+
let $input: &str = &$crate::_extract_input(&$input);
179+
$body.parse().unwrap()
196180
}
197181
)+
198182
}
199183
}
200184

185+
/// Implementation detail of `define_proc_macros!`.
186+
///
187+
/// **This function is not part of the public API. It can change or be removed between any versions.**
188+
#[doc(hidden)]
189+
pub fn _extract_input(derive_input: &str) -> String {
190+
let mut normalized = String::with_capacity(derive_input.len());
191+
for piece in derive_input.split_whitespace() {
192+
normalized.push_str(piece);
193+
normalized.push(' ');
194+
}
195+
196+
let prefix = "#[allow(unused)] enum ProceduralMasqueradeDummyType { Input = (0, stringify!(";
197+
let suffix = ")).0, } ";
198+
assert!(normalized.starts_with(prefix), "expected prefix not found in {:?}", derive_input);
199+
assert!(normalized.ends_with(suffix), "expected suffix not found in {:?}", derive_input);
200+
201+
let start = prefix.len();
202+
let end = normalized.len() - suffix.len();
203+
normalized[start..end].to_owned()
204+
}
205+
201206
/// This macro expands to the definition of another macro (whose name is given as a parameter).
202207
///
203208
/// See crate documentation for details.

0 commit comments

Comments
 (0)