Skip to content

Commit

Permalink
Merge pull request #458 from dtolnay/local-inner-macros
Browse files Browse the repository at this point in the history
Local inner macros
  • Loading branch information
dtolnay committed Aug 10, 2018
2 parents 2a62402 + 38de27e commit e40cbad
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
/// ]);
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! json {
// Hide distracting implementation details from the generated rustdoc.
($($json:tt)+) => {
Expand All @@ -81,7 +81,7 @@ macro_rules! json {
//
// Changes are fine as long as `json_internal!` does not call any new helper
// macros and can still be invoked as `json_internal!($($json)+)`.
#[macro_export]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! json_internal {
//////////////////////////////////////////////////////////////////////////
Expand All @@ -93,12 +93,12 @@ macro_rules! json_internal {

// Done with trailing comma.
(@array [$($elems:expr,)*]) => {
vec![$($elems,)*]
json_internal_vec![$($elems,)*]
};

// Done without trailing comma.
(@array [$($elems:expr),*]) => {
vec![$($elems),*]
json_internal_vec![$($elems),*]
};

// Next element is `null`.
Expand Down Expand Up @@ -265,7 +265,7 @@ macro_rules! json_internal {
};

([]) => {
$crate::Value::Array(vec![])
$crate::Value::Array(json_internal_vec![])
};

([ $($tt:tt)+ ]) => {
Expand All @@ -291,6 +291,17 @@ macro_rules! json_internal {
};
}

// The json_internal macro above cannot invoke vec directly because it uses
// local_inner_macros. A vec invocation there would resolve to $crate::vec.
// Instead invoke vec here outside of local_inner_macros.
#[macro_export]
#[doc(hidden)]
macro_rules! json_internal_vec {
($($content:tt)*) => {
vec![$($content)*]
};
}

#[macro_export]
#[doc(hidden)]
macro_rules! json_unexpected {
Expand Down

0 comments on commit e40cbad

Please sign in to comment.