Skip to content

Instantly share code, notes, and snippets.

@rctrj
Created October 23, 2023 04:05
Show Gist options
  • Save rctrj/4c89c67c8cf172a7e3d91e089339051c to your computer and use it in GitHub Desktop.
Save rctrj/4c89c67c8cf172a7e3d91e089339051c to your computer and use it in GitHub Desktop.
display using debug
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(DisplayUsingDebug)]
pub fn derive_display_using_debug(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let name = ast.ident;
TokenStream::from(quote! {
impl std::fmt::Display for #name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment