From c3fb85080805a8f9ea7619635cf822b65ef3e823 Mon Sep 17 00:00:00 2001 From: Trey Lowerison <19714082+tlowerison@users.noreply.github.com> Date: Mon, 28 Aug 2023 13:50:24 -0700 Subject: [PATCH 1/4] add support for specifying re-export visibility --- src/lib.rs | 86 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dc45452..7132c56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -395,14 +395,10 @@ struct Mejrs { /// /// #[macro_export] -macro_rules! derive_alias {( - $( - #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)]; - )* -) => ( - $crate::ඞ_with_dollar! {( $_:tt ) => ( - $crate::ඞ::paste! { - $( +macro_rules! derive_alias { + (#[derive($MacroName:ident !)] = #[derive($($derives:tt)*)];) => { + $crate::ඞ_with_dollar! {( $_:tt ) => ( + $crate::ඞ::paste! { // To avoid ambiguities with what the re-export // refers to, let's use a hopefully unused name. // @@ -420,10 +416,42 @@ macro_rules! derive_alias {( )} #[allow(unused_imports)] pub(in crate) use [< $MacroName __derive_macro >] as $MacroName; - )* - } - )} -)} + } + )} + }; + (pub #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)];) => { + $crate::ඞ_with_dollar! {( $_:tt ) => ( + $crate::ඞ::paste! { + // To avoid ambiguities with what the re-export + // refers to, let's use a hopefully unused name. + // + // Indeed, eponymous derive macros in scope such as those + // from the prelude would otherwise cause trouble with the + // re-export line. + #[allow(nonstandard_style)] + #[macro_export] + macro_rules! [< $MacroName __derive_macro >] {( + $_($item:tt)* + ) => ( + $crate::ඞ_nested_derive! { + #[derive($($derives)*)] + $_($item)* + } + )} + #[allow(unused_imports)] + pub use [< $MacroName __derive_macro >] as $MacroName; + } + )} + }; + (#[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+) => { + $crate::derive_alias!(#[derive($($name_tt)+)] = #[derive($($derives)*)];); + $crate::derive_alias!($($tt)+); + }; + (pub #[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+) => { + $crate::derive_alias!(pub #[derive($($name_tt)+)] = #[derive($($derives)*)];); + $crate::derive_alias!($($tt)+); + }; +} /// Convenience macro to define new attribute aliases. @@ -467,12 +495,8 @@ pub mod __macro_internals { ``` */ /// #[macro_export] -macro_rules! attribute_alias {( - $( - #[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+; - )* -) => ( - $( +macro_rules! attribute_alias { + (#[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+;) => { $crate::ඞ_with_dollar! {( $_:tt ) => ( // Let's not do the paste + module + re-export dance here since it // is less likely for an attribute name to collide with a prelude item. @@ -484,8 +508,30 @@ macro_rules! attribute_alias {( #[allow(unused_imports)] pub(in crate) use $name; )} - )* -)} + }; + (pub #[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+;) => { + $crate::ඞ_with_dollar! {( $_:tt ) => ( + // Let's not do the paste + module + re-export dance here since it + // is less likely for an attribute name to collide with a prelude item. + #[allow(nonstandard_style)] + #[macro_export] + macro_rules! $name {( $_($item:tt)* ) => ( + $( #[$($attrs)*] )+ + $_($item)* + )} + #[allow(unused_imports)] + pub use $name; + )} + }; + (#[apply($($name_tt:tt)+)] = $( #[$($attrs:tt)*] )+; $($tt:tt)+) => { + $crate::attribute_alias!(#[apply($($name_tt)+)] = $(#[$($attrs)*])+;); + $crate::attribute_alias!($($tt)+); + }; + (pub #[apply($($name_tt:tt)+)] = $( #[$($attrs:tt)*] )+; $($tt:tt)*) => { + attribute_alias!(pub #[apply($($name_tt)+)] = $(#[$($attrs)*])+;); + attribute_alias!($($tt)+); + }; +} #[doc(hidden)] /** Not part of the public API*/ #[macro_export] macro_rules! ඞ_with_dollar {( $($rules:tt)* ) => ( From b9f52d3beea5ef40c6f331b1a8236e3994aedac2 Mon Sep 17 00:00:00 2001 From: Chris Olstrom Date: Fri, 27 Sep 2024 09:03:37 -0700 Subject: [PATCH 2/4] bump version to 0.3.0 and update Cargo.lock --- Cargo.lock | 46 +++++++++++++++++++-------------------- Cargo.toml | 4 ++-- src/proc_macro/Cargo.toml | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f1eec0..331253b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "macro_rules_attribute" -version = "0.2.1-rc1" +version = "0.3.0" dependencies = [ "macro_rules_attribute-proc_macro", "once_cell", @@ -15,58 +15,58 @@ dependencies = [ [[package]] name = "macro_rules_attribute-proc_macro" -version = "0.2.1-rc1" +version = "0.3.0" [[package]] name = "once_cell" -version = "1.10.0" +version = "1.17.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" +checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b" [[package]] name = "paste" -version = "1.0.7" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pin-project-lite" -version = "0.2.8" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "proc-macro2" -version = "1.0.37" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.18" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] [[package]] name = "serde" -version = "1.0.136" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.136" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -75,17 +75,17 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.91" +version = "2.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" +checksum = "6e2415488199887523e74fd9a5f7be804dfd42d868ae0eca382e3917094d210e" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "unicode-ident" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" diff --git a/Cargo.toml b/Cargo.toml index 263c151..8c3bfac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "macro_rules_attribute" -version = "0.2.1-rc1" # Keep in sync +version = "0.3.0" # Keep in sync authors = ["Daniel Henry-Mantilla "] edition = "2018" @@ -19,7 +19,7 @@ readme = "README.md" paste.version = "1.0.7" [dependencies.macro_rules_attribute-proc_macro] -version = "=0.2.1-rc1" # Keep in sync +version = "=0.3.0" # Keep in sync path = "src/proc_macro" [features] diff --git a/src/proc_macro/Cargo.toml b/src/proc_macro/Cargo.toml index e9fd8e5..c936695 100644 --- a/src/proc_macro/Cargo.toml +++ b/src/proc_macro/Cargo.toml @@ -4,7 +4,7 @@ path = "mod.rs" [package] name = "macro_rules_attribute-proc_macro" -version = "0.2.1-rc1" # Keep in sync +version = "0.3.0" # Keep in sync authors = ["Daniel Henry-Mantilla "] edition = "2018" From d1d28e934c1a2d17bdb07b8ee3a79a47cc4ccfd5 Mon Sep 17 00:00:00 2001 From: Chris Olstrom Date: Fri, 27 Sep 2024 16:17:35 -0700 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Daniel Henry-Mantilla --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7132c56..8f454f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -419,7 +419,10 @@ macro_rules! derive_alias { } )} }; - (pub #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)];) => { + ( + $( #[doc $($doc:tt)*] )* + pub #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)]; + ) => ( $crate::ඞ_with_dollar! {( $_:tt ) => ( $crate::ඞ::paste! { // To avoid ambiguities with what the re-export @@ -429,6 +432,7 @@ macro_rules! derive_alias { // from the prelude would otherwise cause trouble with the // re-export line. #[allow(nonstandard_style)] + #[doc(hidden)] #[macro_export] macro_rules! [< $MacroName __derive_macro >] {( $_($item:tt)* @@ -438,11 +442,13 @@ macro_rules! derive_alias { $_($item)* } )} + $( #[doc $($doc)*] )* + #[doc(inline)] #[allow(unused_imports)] pub use [< $MacroName __derive_macro >] as $MacroName; } )} - }; + ); (#[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+) => { $crate::derive_alias!(#[derive($($name_tt)+)] = #[derive($($derives)*)];); $crate::derive_alias!($($tt)+); From 6bc694caa55834cd2dd8569badf36dcc172a0590 Mon Sep 17 00:00:00 2001 From: Chris Olstrom Date: Thu, 3 Oct 2024 14:34:03 -0700 Subject: [PATCH 4/4] normalize macro spacing for more ergonomic code reviews --- src/lib.rs | 276 +++++++++++++++++++++++++++++------------------------ 1 file changed, 153 insertions(+), 123 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8f454f2..d552623 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,7 @@ #![cfg_attr(feature = "better-docs", cfg_attr(all(), doc = include_str!("../README.md")) )] -#![cfg_attr(feature = "better-docs", - feature(doc_auto_cfg), -)] +#![cfg_attr(feature = "better-docs", feature(doc_auto_cfg))] #![no_std] #![forbid(unsafe_code)] @@ -396,69 +394,80 @@ struct Mejrs { /// #[macro_export] macro_rules! derive_alias { - (#[derive($MacroName:ident !)] = #[derive($($derives:tt)*)];) => { - $crate::ඞ_with_dollar! {( $_:tt ) => ( - $crate::ඞ::paste! { - // To avoid ambiguities with what the re-export - // refers to, let's use a hopefully unused name. - // - // Indeed, eponymous derive macros in scope such as those - // from the prelude would otherwise cause trouble with the - // re-export line. - #[allow(nonstandard_style)] - macro_rules! [< $MacroName __derive_macro >] {( - $_($item:tt)* - ) => ( - $crate::ඞ_nested_derive! { - #[derive($($derives)*)] - $_($item)* - } - )} - #[allow(unused_imports)] - pub(in crate) use [< $MacroName __derive_macro >] as $MacroName; - } - )} - }; - ( - $( #[doc $($doc:tt)*] )* - pub #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)]; - ) => ( - $crate::ඞ_with_dollar! {( $_:tt ) => ( - $crate::ඞ::paste! { - // To avoid ambiguities with what the re-export - // refers to, let's use a hopefully unused name. - // - // Indeed, eponymous derive macros in scope such as those - // from the prelude would otherwise cause trouble with the - // re-export line. - #[allow(nonstandard_style)] - #[doc(hidden)] - #[macro_export] - macro_rules! [< $MacroName __derive_macro >] {( - $_($item:tt)* - ) => ( - $crate::ඞ_nested_derive! { - #[derive($($derives)*)] - $_($item)* - } - )} - $( #[doc $($doc)*] )* - #[doc(inline)] - #[allow(unused_imports)] - pub use [< $MacroName __derive_macro >] as $MacroName; - } - )} - ); - (#[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+) => { - $crate::derive_alias!(#[derive($($name_tt)+)] = #[derive($($derives)*)];); - $crate::derive_alias!($($tt)+); - }; - (pub #[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+) => { - $crate::derive_alias!(pub #[derive($($name_tt)+)] = #[derive($($derives)*)];); - $crate::derive_alias!($($tt)+); - }; -} + ( + #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)]; + ) => { + $crate::ඞ_with_dollar! { + ( $_:tt ) => ( + $crate::ඞ::paste! { + // To avoid ambiguities with what the re-export + // refers to, let's use a hopefully unused name. + // + // Indeed, eponymous derive macros in scope such as those + // from the prelude would otherwise cause trouble with the + // re-export line. + #[allow(nonstandard_style)] + macro_rules! [< $MacroName __derive_macro >] { + ( $_($item:tt)* ) => ( + $crate::ඞ_nested_derive! { + #[derive($($derives)*)] + $_($item)* + } + ) + } + #[allow(unused_imports)] + pub(in crate) use [< $MacroName __derive_macro >] as $MacroName; + } + ) + } + }; + ( + $( #[doc $($doc:tt)*] )* + pub #[derive($MacroName:ident !)] = #[derive($($derives:tt)*)]; + ) => ( + $crate::ඞ_with_dollar! { + ( $_:tt ) => ( + $crate::ඞ::paste! { + // To avoid ambiguities with what the re-export + // refers to, let's use a hopefully unused name. + // + // Indeed, eponymous derive macros in scope such as those + // from the prelude would otherwise cause trouble with the + // re-export line. + #[allow(nonstandard_style)] + #[doc(hidden)] + #[macro_export] + macro_rules! [< $MacroName __derive_macro >] { + ( $_($item:tt)* ) => ( + $crate::ඞ_nested_derive! { + #[derive($($derives)*)] + $_($item)* + } + ) + } + + $( #[doc $($doc)*] )* + #[doc(inline)] + #[allow(unused_imports)] + pub use [< $MacroName __derive_macro >] as $MacroName; + } + ) + } + ); + ( + #[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+ + ) => { + $crate::derive_alias!(#[derive($($name_tt)+)] = #[derive($($derives)*)];); + $crate::derive_alias!($($tt)+); + }; + ( + pub #[derive($($name_tt:tt)+)] = #[derive($($derives:tt)*)]; $($tt:tt)+ + ) => { + $crate::derive_alias!(pub #[derive($($name_tt)+)] = #[derive($($derives)*)];); + $crate::derive_alias!($($tt)+); + }; +} /// Convenience macro to define new attribute aliases. /// @@ -502,48 +511,68 @@ pub mod __macro_internals { /// #[macro_export] macro_rules! attribute_alias { - (#[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+;) => { - $crate::ඞ_with_dollar! {( $_:tt ) => ( - // Let's not do the paste + module + re-export dance here since it - // is less likely for an attribute name to collide with a prelude item. - #[allow(nonstandard_style)] - macro_rules! $name {( $_($item:tt)* ) => ( - $( #[$($attrs)*] )+ - $_($item)* - )} - #[allow(unused_imports)] - pub(in crate) use $name; - )} - }; - (pub #[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+;) => { - $crate::ඞ_with_dollar! {( $_:tt ) => ( - // Let's not do the paste + module + re-export dance here since it - // is less likely for an attribute name to collide with a prelude item. - #[allow(nonstandard_style)] - #[macro_export] - macro_rules! $name {( $_($item:tt)* ) => ( - $( #[$($attrs)*] )+ - $_($item)* - )} - #[allow(unused_imports)] - pub use $name; - )} - }; - (#[apply($($name_tt:tt)+)] = $( #[$($attrs:tt)*] )+; $($tt:tt)+) => { - $crate::attribute_alias!(#[apply($($name_tt)+)] = $(#[$($attrs)*])+;); - $crate::attribute_alias!($($tt)+); - }; - (pub #[apply($($name_tt:tt)+)] = $( #[$($attrs:tt)*] )+; $($tt:tt)*) => { - attribute_alias!(pub #[apply($($name_tt)+)] = $(#[$($attrs)*])+;); - attribute_alias!($($tt)+); - }; + ( + #[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+; + ) => { + $crate::ඞ_with_dollar! { + ( $_:tt ) => ( + // Let's not do the paste + module + re-export dance here since it + // is less likely for an attribute name to collide with a prelude item. + #[allow(nonstandard_style)] + macro_rules! $name { + ( $_($item:tt)* ) => ( + $( #[$($attrs)*] )+ + $_($item)* + ) + } + #[allow(unused_imports)] + pub(in crate) use $name; + ) + } + }; + ( + pub #[apply($name:ident $(!)?)] = $( #[$($attrs:tt)*] )+; + ) => { + $crate::ඞ_with_dollar! { + ( $_:tt ) => ( + // Let's not do the paste + module + re-export dance here since it + // is less likely for an attribute name to collide with a prelude item. + #[allow(nonstandard_style)] + #[macro_export] + macro_rules! $name { + ( $_($item:tt)* ) => ( + $( #[$($attrs)*] )+ + $_($item)* + ) + } + #[allow(unused_imports)] + pub use $name; + ) + } + }; + ( + #[apply($($name_tt:tt)+)] = $( #[$($attrs:tt)*] )+; $($tt:tt)+ + ) => { + $crate::attribute_alias!(#[apply($($name_tt)+)] = $(#[$($attrs)*])+;); + $crate::attribute_alias!($($tt)+); + }; + ( + pub #[apply($($name_tt:tt)+)] = $( #[$($attrs:tt)*] )+; $($tt:tt)* + ) => { + attribute_alias!(pub #[apply($($name_tt)+)] = $(#[$($attrs)*])+;); + attribute_alias!($($tt)+); + }; } -#[doc(hidden)] /** Not part of the public API*/ #[macro_export] -macro_rules! ඞ_with_dollar {( $($rules:tt)* ) => ( +#[doc(hidden)] +/** Not part of the public API*/ +#[macro_export] +macro_rules! ඞ_with_dollar { + ( $($rules:tt)* ) => ( macro_rules! __emit__ { $($rules)* } __emit__! { $ } -)} + ) +} /// Like #\[[macro_rules_derive]\], but for allowing to be /// used to shadow [the "built-in" `#[derive]` attribute][1] (on Rust ≥ 1.57.0). @@ -583,38 +612,39 @@ pub use ::macro_rules_attribute_proc_macro::derive; pub use ::macro_rules_attribute_proc_macro::Custom; attribute_alias! { - #[apply(this_macro_is_private!)] = - #[doc(hidden)] - /// Not part of the public API - #[macro_export] - ; + #[apply(this_macro_is_private!)] = + #[doc(hidden)] + /// Not part of the public API + #[macro_export] + ; } mod nested_derive { //! Inlined mini-version of `::nested_derive`. #[crate::apply(this_macro_is_private!)] macro_rules! ඞ_nested_derive { - ( - #[derive( $($Derives:tt)* )] - $($rest:tt)* - ) => ( - #[$crate::derive( $($Derives)* )] - #[$crate::apply($crate::ඞ_dalek_EXTERMINATE!)] - $($rest)* - ); - } + ( + #[derive( $($Derives:tt)* )] + $($rest:tt)* + ) => ( + #[$crate::derive( $($Derives)* )] + #[$crate::apply($crate::ඞ_dalek_EXTERMINATE!)] + $($rest)* + ); + } // Ideally this would have been `dalek_☃_EXTERMINATE`, since the snowman // ressembles a Dalek more, which is a paramount aspect of this hidden macro // but for some reason Rust despises snowmen even though there are // ඞ-infected identifiers among (s)us… #[crate::apply(this_macro_is_private!)] - macro_rules! ඞ_dalek_EXTERMINATE {( $it:item ) => ()} + macro_rules! ඞ_dalek_EXTERMINATE { + ( $it:item ) => {}; + } } -#[doc(hidden)] /** Not part of the public API */ pub -mod ඞ { - pub use { - ::paste::paste, - }; +#[doc(hidden)] +/** Not part of the public API */ +pub mod ඞ { + pub use ::paste::paste; }