Skip to main content

derive_display_from_debug/
lib.rs

1//! A trivial Rust macro to derive the Display trait for any type with the Debug trait
2//!
3//! This is a very lightweight crate: check the README for more info
4
5
6
7extern crate proc_macro;
8
9use proc_macro::TokenStream;
10use quote::quote;
11
12#[proc_macro_derive(Display)]
13pub fn display_derive(input: TokenStream) -> TokenStream {
14
15    let ast = syn::parse(input).expect("TokenStream could not be parsed");
16    impl_display_derive(&ast)
17}
18
19
20fn impl_display_derive(ast: &syn::DeriveInput) -> TokenStream {
21    let name = &ast.ident;
22    let gen = quote! {
23
24	    impl Display for #name {
25			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26				write!(f, "{:?}", self)
27			}
28		}
29    };
30    gen.into()
31}