Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit e6d0b2c

Browse files
committed
Fix so error_chain! macro can be imported in new macro import style
1 parent 38f2324 commit e6d0b2c

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/error_chain.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// Prefer to use `error_chain` instead of this macro.
22
#[doc(hidden)]
3-
#[macro_export]
3+
#[macro_export(local_inner_macros)]
44
macro_rules! impl_error_chain_processed {
55
// Default values for `types`.
66
(
@@ -347,7 +347,7 @@ macro_rules! impl_error_chain_processed {
347347

348348
/// Internal macro used for reordering of the fields.
349349
#[doc(hidden)]
350-
#[macro_export]
350+
#[macro_export(local_inner_macros)]
351351
macro_rules! error_chain_processing {
352352
(
353353
({}, $b:tt, $c:tt, $d:tt)
@@ -400,7 +400,7 @@ macro_rules! error_chain_processing {
400400
}
401401

402402
/// Macro for generating error types and traits. See crate level documentation for details.
403-
#[macro_export]
403+
#[macro_export(local_inner_macros)]
404404
macro_rules! error_chain {
405405
( $( $block_name:ident { $( $block_content:tt )* } )* ) => {
406406
error_chain_processing! {

src/impl_error_chain_kind.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@
33
// - replace `impl Error` by `impl Item::description`
44
// - $imeta
55

6+
// Because of the `#[macro_export(local_inner_macros)]` usage on `impl_error_chain_kind` that macro
7+
// will only look inside this crate for macros to invoke. So using `stringify` or `write` from
8+
// the standard library will fail. Thus we here create simple wrappers for them that are not
9+
// exported as `local_inner_macros`, and thus they can in turn use the standard library macros.
610
#[macro_export]
11+
macro_rules! stringify_internal {
12+
($($t:tt)*) => { stringify!($($t)*) }
13+
}
14+
#[macro_export]
15+
macro_rules! write_internal {
16+
($dst:expr, $($arg:tt)*) => (write!($dst, $($arg)*))
17+
}
18+
19+
#[macro_export(local_inner_macros)]
720
#[doc(hidden)]
821
macro_rules! impl_error_chain_kind {
922
( $(#[$meta:meta])*
@@ -272,18 +285,18 @@ macro_rules! impl_error_chain_kind {
272285
{ display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*}
273286
) => {
274287
|impl_error_chain_kind!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter| {
275-
write!(f, $( $exprs )*)
288+
write_internal!(f, $( $exprs )*)
276289
}
277290
};
278291
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
279292
{ display($pattern:expr) $( $tail:tt )*}
280293
) => {
281-
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern) }
294+
|_, f: &mut ::std::fmt::Formatter| { write_internal!(f, $pattern) }
282295
};
283296
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
284297
{ display($pattern:expr, $( $exprs:tt )*) $( $tail:tt )*}
285298
) => {
286-
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern, $( $exprs )*) }
299+
|_, f: &mut ::std::fmt::Formatter| { write_internal!(f, $pattern, $( $exprs )*) }
287300
};
288301
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
289302
{ $t:tt $( $tail:tt )*}
@@ -296,7 +309,7 @@ macro_rules! impl_error_chain_kind {
296309
{ }
297310
) => {
298311
|self_: &$name, f: &mut ::std::fmt::Formatter| {
299-
write!(f, "{}", self_.description())
312+
write_internal!(f, "{}", self_.description())
300313
}
301314
};
302315
(FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident
@@ -317,7 +330,7 @@ macro_rules! impl_error_chain_kind {
317330
[$( $var:ident ),*]
318331
{ }
319332
) => {
320-
stringify!($item)
333+
stringify_internal!($item)
321334
};
322335
(ITEM_BODY $(#[$imeta:meta])* $item:ident: UNIT
323336
) => { };

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ macro_rules! bail {
783783
/// ```
784784
///
785785
/// See documentation for `bail!` macro for further details.
786-
#[macro_export]
786+
#[macro_export(local_inner_macros)]
787787
macro_rules! ensure {
788788
($cond:expr, $e:expr) => {
789789
if !($cond) {

0 commit comments

Comments
 (0)