Support non-doc attrs in extension trait macro

The `define_extension_trait` macro originally didn't support `#[inline]`
or other attributes for simplicity. We still want them so this commit
adds basic support for it. It adds the `doc` attributes to trait
*definition* only and adds all other attributes to the *implementation*
only. This should support `#[inline]` and other attributes. The downside
is it doesn't support adding non-doc attributes to trait *definition*
but I can't think of any relevant ones that we would want and we can
find a solution later if we do.
This commit is contained in:
Martin Habovstiak 2024-08-11 14:57:47 +02:00
parent ca1735f24c
commit fcc3cb03f0
1 changed files with 44 additions and 3 deletions

View File

@ -214,6 +214,38 @@ macro_rules! impl_asref_push_bytes {
}
pub(crate) use impl_asref_push_bytes;
macro_rules! only_doc_attrs {
({}, {$($fun:tt)*}) => {
$($fun)*
};
({#[doc = $($doc:tt)*] $($all_attrs:tt)*}, {$($fun:tt)*}) => {
$crate::internal_macros::only_doc_attrs!({ $($all_attrs)* }, { #[doc = $($doc)*] $($fun)* });
};
({#[doc($($doc:tt)*)] $($all_attrs:tt)*}, {$($fun:tt)*}) => {
$crate::internal_macros::only_doc_attrs!({ $($all_attrs)* }, { #[doc($($doc)*)] $($fun)* });
};
({#[$($other:tt)*] $($all_attrs:tt)*}, {$($fun:tt)*}) => {
$crate::internal_macros::only_doc_attrs!({ $($all_attrs)* }, { $($fun)* });
};
}
pub(crate) use only_doc_attrs;
macro_rules! only_non_doc_attrs {
({}, {$($fun:tt)*}) => {
$($fun)*
};
({#[doc = $($doc:tt)*] $($all_attrs:tt)*}, {$($fun:tt)*}) => {
$crate::internal_macros::only_doc_attrs!({ $($all_attrs)* }, { #[doc = $($doc)*] $($fun)* });
};
({#[doc($($doc:tt)*)] $($all_attrs:tt)*}, {$($fun:tt)*}) => {
$crate::internal_macros::only_doc_attrs!({ $($all_attrs)* }, { $($fun)* });
};
({#[$($other:tt)*] $($all_attrs:tt)*}, {$($fun:tt)*}) => {
$crate::internal_macros::only_doc_attrs!({ $($all_attrs)* }, { #[$(other)*] $($fun)* });
};
}
pub(crate) use only_non_doc_attrs;
/// Defines an trait `$trait_name` and implements it for `ty`, used to define extension traits.
macro_rules! define_extension_trait {
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
@ -224,14 +256,23 @@ macro_rules! define_extension_trait {
}) => {
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name {
$(
$(#[$($fn_attrs)*])*
$crate::internal_macros::only_doc_attrs! {
{ $(#[$($fn_attrs)*])* },
{
fn $fn$(<$($gen: $gent),*>)?($($param_name: $param_type),*) $( -> $ret )?;
}
}
)*
}
impl $trait_name for $ty {
$(
$crate::internal_macros::only_non_doc_attrs! {
{ $(#[$($fn_attrs)*])* },
{
fn $fn$(<$($gen: $gent),*>)?($($param_name: $param_type),*) $( -> $ret )? $body
}
}
)*
}
};