Skip to content

Commit c75c562

Browse files
committed
Add do_rewrite_in_macro_context
1 parent 908cccd commit c75c562

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/macros.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ pub fn rewrite_macro(
149149
shape: Shape,
150150
position: MacroPosition,
151151
) -> Option<String> {
152-
let old_value = context.inside_macro.replace(true);
153-
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position);
154-
if result.is_none() {
155-
context.macro_rewrite_failure.replace(true);
156-
}
157-
context.inside_macro.replace(old_value);
158-
result
152+
context.do_rewrite_in_macro_context(|| {
153+
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position);
154+
if result.is_none() {
155+
context.macro_rewrite_failure.replace(true);
156+
}
157+
result
158+
})
159159
}
160160

161161
pub fn rewrite_macro_inner(

src/rewrite.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@ impl<'a> RewriteContext<'a> {
6565
pub fn is_if_else_block(&self) -> bool {
6666
*self.is_if_else_block.borrow()
6767
}
68+
69+
pub fn do_rewrite_in_macro_context<F>(&self, f: F) -> Option<String>
70+
where
71+
F: Fn() -> Option<String>,
72+
{
73+
let inside_macro = self.inside_macro.replace(true);
74+
let result = f();
75+
self.inside_macro.replace(inside_macro);
76+
result
77+
}
6878
}

0 commit comments

Comments
 (0)