Allow for auto display impl in declare wrapper type macro

This commit is contained in:
Omar Abdulla
2025-08-18 09:41:25 +03:00
parent 84026f9aee
commit fb3959d345
4 changed files with 30 additions and 13 deletions
@@ -1,3 +1,14 @@
#[macro_export]
macro_rules! impl_for_wrapper {
(Display, $ident: ident) => {
impl std::fmt::Display for $ident {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
};
}
/// Defines wrappers around types.
///
/// For example, the macro invocation seen below:
@@ -42,7 +53,13 @@
macro_rules! define_wrapper_type {
(
$(#[$meta: meta])*
$vis:vis struct $ident: ident($ty: ty);
$vis:vis struct $ident: ident($ty: ty)
$(
impl $($trait_ident: ident),*
)?
;
) => {
$(#[$meta])*
$vis struct $ident($ty);
@@ -98,9 +115,15 @@ macro_rules! define_wrapper_type {
value.0
}
}
$(
$(
$crate::macros::impl_for_wrapper!($trait_ident, $ident);
)*
)?
};
}
/// Technically not needed but this allows for the macro to be found in the `macros` module of the
/// crate in addition to being found in the root of the crate.
pub use define_wrapper_type;
pub use {define_wrapper_type, impl_for_wrapper};